This commit is contained in:
Felix Van der Jeugt 2021-12-13 09:32:03 +01:00
parent 1feda86093
commit c8a480818f
No known key found for this signature in database
GPG Key ID: 58B209295023754D
2 changed files with 109 additions and 0 deletions

59
day13/part1.lua Executable file
View File

@ -0,0 +1,59 @@
#!/usr/bin/env luajit
require("utils")
require("set")
local file = io.open(arg[3])
local coords = {}
local maxr, maxc = 0, 0
local line = file:read("*l")
while line ~= "" do
local c, r = unpack(split(line, ","))
r, c = tonumber(r), tonumber(c)
if r > maxr then maxr = r end
if c > maxc then maxc = c end
table.insert(coords, { r, c })
line = file:read("*l")
end
local width, height = maxc + 1, maxr + 1
foreach(coords, function(p) return p[1] * width + p[2] end)
coords = set(coords)
local folds = {}
line = file:read("*l")
while line ~= nil do
local dir, v = unpack(split(line, "="))
table.insert(folds, { dir == "fold along y", tonumber(v) })
line = file:read("*l")
end
for i, fold in pairs(folds) do
local dir, v = unpack(fold)
local ncoords = set()
for p in coords:iter() do
local r, c = math.floor(p / width), p % width
if dir and r > v then
r = 2 * v - r
elseif not dir and c > v then
c = 2 * v - c
end
ncoords:add(r * width + c)
end
coords = ncoords
break
end
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")
print(coords.size)

50
day13/part2.lua Executable file
View File

@ -0,0 +1,50 @@
#!/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")