adventofcode-2021/day25/part1.lua

57 lines
1.0 KiB
Lua
Executable File

#!/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)