diff --git a/day25/part1.lua b/day25/part1.lua new file mode 100755 index 0000000..6d2b47c --- /dev/null +++ b/day25/part1.lua @@ -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) diff --git a/grid.lua b/grid.lua index 4eaeb61..5960a09 100644 --- a/grid.lua +++ b/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