From 9c96d30ea65ce132a7cba28e2460683041c4c0a3 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Wed, 22 Dec 2021 17:44:52 +0100 Subject: [PATCH] day 22 part 2 --- day22/part2.lua | 48 +++++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/day22/part2.lua b/day22/part2.lua index 62cd4ce..f59eca9 100755 --- a/day22/part2.lua +++ b/day22/part2.lua @@ -57,39 +57,37 @@ local function cut3d(a, b) } 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 + for _, x in pairs(xs) do if x.s < x.e then + for _, y in pairs(ys) do if y.s < y.e then + for _, z in pairs(zs) do if z.s < z.e then table.insert(aparts, { b = a.b, x=x, y=y, z=z }) - end - end - end + end end + end 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) +local count = 0 +for i = 1, #ranges do + if ranges[i].b then + print(i, os.clock()) + local fixed = { ranges[i] } + for j = i + 1, #ranges do + local newfixed = {} + for _, f in pairs(fixed) do + for _, fpart in pairs(cut3d(f, ranges[j])) do + table.insert(newfixed, fpart) + end + end + fixed = newfixed + end + 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 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))