day 25 part 1

This commit is contained in:
Felix Van der Jeugt 2021-12-25 11:42:17 +01:00
parent 3a13913e74
commit 60ed6981b8
No known key found for this signature in database
GPG Key ID: 58B209295023754D
2 changed files with 62 additions and 0 deletions

56
day25/part1.lua Executable file
View File

@ -0,0 +1,56 @@
#!/usr/bin/env luajit
require("utils")
require("grid")
local f = io.open(arg[3])
local line = f:read("*l")
local g = grid(0, 0, #line, 0)
local r = 0
while line ~= nil do
g:grow()
local c = 0
for char in chars(line) do
g:set(r, c, char)
c = c + 1
end
line = f:read("*l")
r = r + 1
end
local changed = true
local step = 0
while changed do
changed = false
step = step + 1
for r = g.top, g.bottom do
for c = g.left, g.right do
if g:get(r, c) == ">" and g:get(r, (c + 1) % g.width) == "." then
g:set(r, c, ">>")
end
end
for c = g.left, g.right do
if g:get(r, c) == ">>" then
changed = true
g:set(r, c, ".")
g:set(r, (c + 1) % g.width, ">")
end
end
end
for c = g.left, g.right do
for r = g.top, g.bottom do
if g:get(r, c) == "v" and g:get((r + 1) % g.height, c) == "." then
g:set(r, c, "vv")
end
end
for r = g.top, g.bottom do
if g:get(r, c) == "vv" then
changed = true
g:set(r, c, ".")
g:set((r + 1) % g.height, c, "v")
end
end
end
end
print(step)

View File

@ -64,6 +64,11 @@ local function write(self, format)
end end
end end
local function grow(self)
self.height = self.height + 1
self.bottom = self.bottom + 1
end
function grid(top, left, width, height, default) function grid(top, left, width, height, default)
return { return {
top = top, top = top,
@ -79,5 +84,6 @@ function grid(top, left, width, height, default)
iter = iter, iter = iter,
neigh9 = neigh9, neigh9 = neigh9,
write = write, write = write,
grow = grow,
} }
end end