Advertisement
fatboychummy

anotherthingbytesthedust.lua

Apr 27th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.11 KB | None | 0 0
  1. -- very helpful library
  2. local _ = require("luadash")
  3.  
  4. -- helper split function.
  5. -- returns an iterator, like pairs(...) / ipairs(...)
  6. function string:split(pat)
  7.     pat = pat or '%s+'
  8.     local st, g = 1, self:gmatch("()("..pat..")")
  9.     local function getter(segs, seps, sep, cap1, ...)
  10.         st = sep and seps + #sep
  11.         return self:sub(segs, (seps or 0) - 1), cap1 or sep, ...
  12.     end
  13.     return function() if st then return getter(st, g()) end end
  14. end
  15.  
  16. local input = {}
  17.  
  18. -- read and parse each line of the input
  19. for line in io.lines("input.txt") do
  20.     -- rip leading "[", and split by "] " to remove brackets
  21.     local m = line:sub(2):split("] ")
  22.     local timestamp, command = m(), m()
  23.  
  24.     -- timestamp ==> relative time
  25.     m = timestamp:split(" ")
  26.     local date, clock = m(), m()
  27.  
  28.     m = clock:split(":")
  29.     local hour, minute = tonumber((m())), tonumber((m()))
  30.  
  31.     -- add command to input list
  32.     input[#input + 1] = {
  33.         ["date"] = date .. " " .. clock,
  34.         ["minute"] = minute,
  35.         ["command"] = command
  36.     }
  37. end
  38.  
  39. -- sort input by date (string comparison ftw, "1518-11-23 00:20" < "1518-11-23 00:27")
  40. input =_.sort_by(input, function(log)
  41.     return log.date
  42. end)
  43.  
  44. local guard = -1
  45. local sleep = {}
  46.  
  47. for k, log in pairs(input) do
  48.     local m = log.command:split(" ")
  49.     local sw = m()
  50.  
  51.     if sw == "Guard" then
  52.         guard = tonumber(m():sub(2))
  53.         if not sleep[guard] then
  54.             sleep[guard] = {
  55.                 ["guard"] = guard,
  56.                 ["sleep"] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  57.                 ["asleep"] = false
  58.             }
  59.         end
  60.     elseif sw == "falls" then
  61.         if not sleep[guard].asleep then
  62.             for i = log.minute+1, 60 do
  63.                 sleep[guard].sleep[i] = sleep[guard].sleep[i] + 1
  64.             end
  65.             sleep[guard].asleep = true
  66.         end
  67.     elseif sw == "wakes" then
  68.         if sleep[guard].asleep then
  69.             for i = log.minute+1, 60 do
  70.                 sleep[guard].sleep[i] = math.max(sleep[guard].sleep[i] - 1, 0)
  71.             end
  72.         end
  73.         sleep[guard].asleep = false
  74.     end
  75. end
  76.  
  77. -- part 1
  78.  
  79. local flat = {}
  80.  
  81. for k,v in pairs(sleep) do
  82.     flat[#flat+1] = v
  83. end
  84.  
  85. local sorted = _.sort_by(flat, function(guard)
  86.     local sum = 0
  87.     for k, sleep in pairs(guard.sleep) do
  88.         sum = sum + sleep
  89.     end
  90.  
  91.     return sum
  92. end)
  93.  
  94. local target = sorted[#sorted]
  95.  
  96. local best_key = 0
  97. local best_val = -1
  98.  
  99. for k, v in pairs(target.sleep) do
  100.     if v > best_val then
  101.         best_key = k
  102.         best_val = v
  103.     end
  104. end
  105.  
  106. print("Solution #1:", target.guard * (best_key - 1))
  107.  
  108. -- part 2
  109.  
  110. local best_guard = -1
  111. local best_key = -1
  112. local best_val = -1
  113.  
  114. for k, guard in pairs(sleep) do
  115.     for minute = 1,60 do
  116.         if guard.sleep[minute] > best_val then
  117.             best_guard = guard.guard
  118.             best_key = minute
  119.             best_val = guard.sleep[minute]
  120.         end
  121.     end
  122. end
  123.  
  124. print("Solution #2:", sleep[best_guard].guard * (best_key - 1))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement