-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday11.lua
executable file
·55 lines (46 loc) · 1.1 KB
/
day11.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env lua
function add(stone, n, stones)
stones[stone] = (stones[stone] or 0) + n
end
function count(stones)
local total = 0
for stone, n in pairs(stones) do
total = total + n
end
return total
end
function simulate(stones, blinks)
for i = 1, blinks do
local new_stones = {}
for stone, n in pairs(stones) do
if stone == 0 then
add(1, n, new_stones)
else
raw = tostring(stone)
if #raw % 2 == 0 then
add(tonumber(raw:sub(0, #raw / 2)), n, new_stones)
add(tonumber(raw:sub(#raw / 2 + 1, #raw)), n, new_stones)
else
add(stone * 2024, n, new_stones)
end
end
end
stones = new_stones
end
return stones
end
if #arg < 1 then
print("Usage: day11 <path to input>")
os.exit(1)
end
local stones = {}
for line in io.lines(arg[1]) do
for raw in line:gmatch("%d+") do
local stone = tonumber(raw)
add(stone, 1, stones)
end
end
part1 = count(simulate(stones, 25))
print('Part 1: ' .. tostring(part1))
part2 = count(simulate(stones, 75))
print('Part 2: ' .. string.format("%.0f", part2))