adventofcode-2021/day13/part2.lua

51 lines
1.0 KiB
Lua
Raw Normal View History

2021-12-13 09:32:03 +01:00
#!/usr/bin/env luajit
require("utils")
require("set")
local file = io.open(arg[3])
local coords = {}
local line = file:read("*l")
while line ~= "" do
local c, r = unpack(split(line, ","))
r, c = tonumber(r), tonumber(c)
table.insert(coords, { r, c })
line = file:read("*l")
end
local folds = {}
line = file:read("*l")
while line ~= nil do
local dir, v = unpack(split(line, "="))
v = tonumber(v)
if dir == "fold along y" then
for i, p in pairs(coords) do
if p[1] > v then p[1] = 2 * v - p[1] end
end
else
for i, p in pairs(coords) do
if p[2] > v then p[2] = 2 * v - p[2] end
end
end
line = file:read("*l")
end
local width, height = 0, 0
for i, p in pairs(coords) do
if p[1] + 1 > height then height = p[1] + 1 end
if p[2] + 1 > width then width = p[2] + 1 end
end
foreach(coords, function(p) return p[1] * width + p[2] end)
coords = set(coords)
for r = 0, height - 1 do
for c = 0, width - 1 do
if coords:has(r * width + c) then
io.write("#")
else
io.write(".")
end
end
io.write("\n")
end
io.write("\n")