adventofcode-2021/day09/part1.lua
Felix Van der Jeugt 9c8cd8e7a9
day 9
2021-12-09 09:21:55 +01:00

27 lines
495 B
Lua
Executable File

#!/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)