adventofcode-2021/day20/part2.lua
Felix Van der Jeugt fadc49c09d
day 20
2021-12-20 13:13:11 +01:00

69 lines
1.2 KiB
Lua
Executable File

#!/usr/bin/env luajit
require("utils")
require("grid")
local f = io.open(arg[3])
local line = f:read("*l")
local enhancer = {}
for char in chars(line) do
if char == "#" then
table.insert(enhancer, 1)
else
table.insert(enhancer, 0)
end
end
f:read("*l")
line = f:read("*l")
local image = grid(1, 1, #line, #line, 0)
local r, c = 0, 0
while line ~= nil do
r = r + 1
c = 0
for char in chars(line) do
c = c + 1
if char == "#" then
image:set(r, c, 1)
end
end
line = f:read("*l")
end
function zoom(image, enhancer)
local newdefault = enhancer[image.default * (#enhancer - 1) + 1]
local newimage = grid(image.top - 1, image.left - 1,
image.height + 2, image.width + 2, newdefault)
for v, r, c in image:iter() do
newimage:set(r, c, v)
end
return newimage
end
function enhance(image, enhancer)
local newimage = zoom(image, enhancer)
for _, r, c in newimage:iter() do
local index = 0
for v, _, _ in image:neigh9(r, c) do
index = index * 2 + v
end
newimage:set(r, c, enhancer[index + 1])
end
return newimage
end
local function pprint(c)
if c == 0
then return "."
else return "#"
end
end
for i = 1, 50 do
image = enhance(image, enhancer)
end
local counter = 0
for v in image:iter() do
counter = counter + v
end
print(counter)