adventofcode-2021/day06/part2.lua

27 lines
522 B
Lua
Raw Normal View History

2021-12-06 08:38:46 +01:00
#!/usr/bin/env luajit
require("utils")
2021-12-06 22:02:21 +01:00
require("deque")
2021-12-06 08:38:46 +01:00
2021-12-06 22:02:21 +01:00
local fishes = deque()
2021-12-06 08:38:46 +01:00
for timer = 0, 9 do
2021-12-06 22:02:21 +01:00
fishes:append(0)
2021-12-06 08:38:46 +01:00
end
for _, timer in pairs(split(io.open(arg[3]):read("*a"), ",")) do
2021-12-06 22:02:21 +01:00
fishes:set(tonumber(timer), fishes:get(tonumber(timer)) + 1)
2021-12-06 08:38:46 +01:00
end
for day = 1, 256 do
2021-12-06 22:02:21 +01:00
local breeding = fishes:popleft() or 0
fishes:append(0)
fishes:set(8, (fishes:get(8) or 0) + breeding)
fishes:set(6, (fishes:get(6) or 0) + breeding)
2021-12-06 08:38:46 +01:00
end
local sum = 0
2021-12-06 22:02:21 +01:00
while fishes.size > 0 do
sum = sum + (fishes:pop() or 0)
2021-12-06 08:38:46 +01:00
end
print(sum)