Compare commits

...

2 Commits

Author SHA1 Message Date
Felix Van der Jeugt 6ef26926ad
day 22 part 2 out of memory 2021-12-22 12:48:13 +01:00
Felix Van der Jeugt 9e4c9c4d03
day 22 part 1 2021-12-22 12:47:58 +01:00
2 changed files with 126 additions and 0 deletions

31
day22/part1.lua Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env luajit
require("utils")
local ranges = {}
for line in io.lines(arg[3]) do
local b, x0, x1, y0, y1, z0, z1 = line:match("(.*) x=(.*)%.%.(.*),y=(.*)%.%.(.*),z=(.*)%.%.(.*)")
if b == "on" then b = true else b = false end
table.insert(ranges, {
b=b,
x0=tonumber(x0),
x1=tonumber(x1),
y0=tonumber(y0),
y1=tonumber(y1),
z0=tonumber(z0),
z1=tonumber(z1)
})
end
local count = 0
for x = -50, 50 do for y = -50, 50 do for z = -50, 50 do
local on = false
for _, r in pairs(ranges) do
if r.x0 <= x and x <= r.x1 and r.y0 <= y and y <= r.y1 and r.z0 <= z and z <= r.z1 then
on = r.b
end
end
if on then
count = count + 1
end
end end end
print(count)

95
day22/part2.lua Executable file
View File

@ -0,0 +1,95 @@
#!/usr/bin/env luajit
require("utils")
local function r(s, e)
-- Upper borders are exclusive here!
return { s=s, e=e }
end
local ranges = {}
for line in io.lines(arg[3]) do
local b, x0, x1, y0, y1, z0, z1 = line:match("(.*) x=(.*)%.%.(.*),y=(.*)%.%.(.*),z=(.*)%.%.(.*)")
if b == "on" then b = true else b = false end
table.insert(ranges, {
b=b,
x=r(tonumber(x0), tonumber(x1) + 1),
y=r(tonumber(y0), tonumber(y1) + 1),
z=r(tonumber(z0), tonumber(z1) + 1),
})
end
local function intersect1d(a, b)
if a.e <= b.s or b.e <= a.s then
-- aaa bbb
-- bbb aaa
return { a }, {}, { b }
elseif a.s <= b.s and b.s <= a.e and a.e <= b.e then
-- aaa222bbb
return { r(a.s, b.s) }, { r(b.s, a.e) }, { r(a.e, b.e) }
elseif a.s <= b.s and b.e <= a.e then
-- aaa222aaa
return { r(a.s, b.s), r(b.e, a.e) }, { b }, {}
elseif b.s <= a.s and a.e <= b.e then
-- bbb222bbb
return {}, { a }, { r(b.s, a.s), r(a.e, b.e) }
elseif b.s <= a.s and b.e <= a.e then
-- bbb222aaa
return { r(b.e, a.e) }, { r(a.s, b.e) }, { r(b.s, a.s) }
else
error("unreachable?")
end
end
local function cut3d(a, b)
local aparts = {}
local xas, xabs, xbs = intersect1d(a.x, b.x)
local yas, yabs, ybs = intersect1d(a.y, b.y)
local zas, zabs, zbs = intersect1d(a.z, b.z)
local nobscombos = { { xas, yas, zas }
, { xas, yas, zabs }
, { xas, yabs, zas }
, { xas, yabs, zabs }
, { xabs, yas, zas }
, { xabs, yas, zabs }
, { xabs, yabs, zas }
}
for _, combo in pairs(nobscombos) do
local xs, ys, zs = unpack(combo)
for _, x in pairs(xs) do
for _, y in pairs(ys) do
for _, z in pairs(zs) do
table.insert(aparts, { b = a.b, x=x, y=y, z=z })
end
end
end
end
return aparts
end
local fixed = { ranges[1] }
table.remove(ranges, 1)
for _, range in pairs(ranges) do
local newfixed = {}
--printtable(range)
if range.b then
table.insert(newfixed, range)
end
for _, f in pairs(fixed) do
for _, fpart in pairs(cut3d(f, range)) do
--io.write(" ")
--printtable(fpart)
table.insert(newfixed, fpart)
end
end
fixed = newfixed
end
local count = 0
for _, r in pairs(fixed) do
-- printtable(r)
count = count + (r.x.e - r.x.s)*(r.y.e - r.y.s)*(r.z.e - r.z.s)
end
io.write(string.format("%d\n", count))