This commit is contained in:
Felix Van der Jeugt 2021-12-11 10:05:05 +01:00
parent bcd54e369b
commit bf39b92abb
No known key found for this signature in database
GPG Key ID: 58B209295023754D
2 changed files with 116 additions and 0 deletions

56
day11/part1.lua Executable file
View File

@ -0,0 +1,56 @@
#!/usr/bin/env luajit
require("utils")
local grid = {}
for line in io.lines(arg[3]) do
local row = {}
for char in chars(line) do
table.insert(row, tonumber(char))
end
table.insert(grid, row)
end
function printgrid()
for r, row in pairs(grid) do
for c, cell in pairs(row) do
if cell > 9 then
io.write('*')
else
io.write(cell)
end
end
io.write('\n')
end
end
printgrid()
function flash(grid, r, c)
grid[r][c] = grid[r][c] + 1
if grid[r][c] == 10 then
for dr = -1, 1 do if 0 < r + dr and r + dr <= #grid then
for dc = -1, 1 do if 0 < c + dc and c + dc <= #grid[r] then
flash(grid, r + dr, c + dc)
end end
end end
end
end
local flashes = 0
for step = 1, 100 do
for r, row in pairs(grid) do
for c, cell in pairs(row) do
flash(grid, r, c)
end
end
for r, row in pairs(grid) do
for c, cell in pairs(row) do
if cell > 9 then
grid[r][c] = 0
flashes = flashes + 1
end
end
end
print(flashes)
printgrid()
end

60
day11/part2.lua Executable file
View File

@ -0,0 +1,60 @@
#!/usr/bin/env luajit
require("utils")
local grid = {}
for line in io.lines(arg[3]) do
local row = {}
for char in chars(line) do
table.insert(row, tonumber(char))
end
table.insert(grid, row)
end
function printgrid()
for r, row in pairs(grid) do
for c, cell in pairs(row) do
if cell > 9 then
io.write('*')
else
io.write(cell)
end
end
io.write('\n')
end
end
printgrid()
function flash(grid, r, c)
grid[r][c] = grid[r][c] + 1
if grid[r][c] == 10 then
for dr = -1, 1 do if 0 < r + dr and r + dr <= #grid then
for dc = -1, 1 do if 0 < c + dc and c + dc <= #grid[r] then
flash(grid, r + dr, c + dc)
end end
end end
end
end
for step = 1, math.huge do
for r, row in pairs(grid) do
for c, cell in pairs(row) do
flash(grid, r, c)
end
end
local flashes = 0
for r, row in pairs(grid) do
for c, cell in pairs(row) do
if cell > 9 then
grid[r][c] = 0
flashes = flashes + 1
end
end
end
print(flashes)
printgrid()
if flashes == #grid * #grid[1] then
print(step)
break
end
end