day 13
This commit is contained in:
parent
1feda86093
commit
c8a480818f
59
day13/part1.lua
Executable file
59
day13/part1.lua
Executable 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
50
day13/part2.lua
Executable 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")
|
Loading…
Reference in New Issue
Block a user