day 25 part 1
This commit is contained in:
parent
3a13913e74
commit
60ed6981b8
56
day25/part1.lua
Executable file
56
day25/part1.lua
Executable 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)
|
6
grid.lua
6
grid.lua
@ -64,6 +64,11 @@ local function write(self, format)
|
||||
end
|
||||
end
|
||||
|
||||
local function grow(self)
|
||||
self.height = self.height + 1
|
||||
self.bottom = self.bottom + 1
|
||||
end
|
||||
|
||||
function grid(top, left, width, height, default)
|
||||
return {
|
||||
top = top,
|
||||
@ -79,5 +84,6 @@ function grid(top, left, width, height, default)
|
||||
iter = iter,
|
||||
neigh9 = neigh9,
|
||||
write = write,
|
||||
grow = grow,
|
||||
}
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user