This commit is contained in:
Felix Van der Jeugt 2021-12-09 09:21:55 +01:00
parent 3f81bb1d8b
commit 9c8cd8e7a9
No known key found for this signature in database
GPG Key ID: 58B209295023754D
2 changed files with 97 additions and 0 deletions

26
day09/part1.lua Executable file
View File

@ -0,0 +1,26 @@
#!/usr/bin/env luajit
require("utils")
local hm = {}
for line in io.lines(arg[3]) do
local row = {}
for c in chars(line) do
table.insert(row, tonumber(c))
end
table.insert(hm, row)
end
local function get(r, c)
return (hm[r] or {})[c] or math.huge
end
local lowsum = 0
for r, row in pairs(hm) do
for c, col in pairs(row) do
if col < get(r - 1, c) and col < get(r, c - 1) and col < get(r + 1, c) and col < get(r, c + 1) then
lowsum = lowsum + col + 1
end
end
end
print(lowsum)

71
day09/part2.lua Executable file
View File

@ -0,0 +1,71 @@
#!/usr/bin/env luajit
require("utils")
local hm = {}
for line in io.lines(arg[3]) do
local row = {}
for c in chars(line) do
table.insert(row, tonumber(c))
end
table.insert(hm, row)
end
local function get(r, c)
return (hm[r] or {})[c] or 9
end
local union = {}
for r, row in pairs(hm) do
for c, col in pairs(row) do
if col ~= 9 then
if get(r - 1, c) ~= 9 then
if union[r] == nil then union[r] = {} end
union[r][c] = { r - 1, c }
end
if get(r, c - 1) ~= 9 then
if union[r] == nil then union[r] = {} end
if union[r][c] ~= nil then
local r0, c0 = r, c - 1
while type(union[r0][c0]) == "table" do
r0, c0 = unpack(union[r0][c0])
if union[r0] == nil then union[r0] = {} end
end
local r1, c1 = r, c
while type(union[r1][c1]) == "table" do
r1, c1 = unpack(union[r1][c1])
if union[r1] == nil then union[r1] = {} end
end
if r0 ~= r1 or c0 ~= c1 then
union[r1][c1] = { r0, c0 }
end
else
union[r][c] = { r, c - 1 }
end
end
end
end
end
local sums = {}
for r, row in pairs(hm) do
for c, col in pairs(row) do
if col ~= 9 then
local r0, c0 = r, c
while type(union[r0][c0]) == "table" do
r0, c0 = unpack(union[r0][c0])
end
if sums[r0] == nil then sums[r0] = {} end
sums[r0][c0] = (sums[r0][c0] or 0) + 1
end
end
end
local sizes = {}
for r, row in pairs(sums) do
for c, col in pairs(row) do
table.insert(sizes, col)
end
end
table.sort(sizes)
print(sizes[#sizes] * sizes[#sizes - 1] * sizes[#sizes - 2])