advent-of-code/12022/18/18.lua

54 lines
1.1 KiB
Lua

io.input("test")
io.input("input")
function s2p( s )
local nx, ny, nz = string.match(s, "(%d+),(%d+),(%d+)")
return tonumber(nx), tonumber(ny), tonumber(nz)
end
function p2s( x, y, z )
return string.format("%d,%d,%d", x, y, z )
end
local cubes = {}
local potneigh = {}
local ncubes = 0
for line in io.lines() do
local s = line
local x, y, z = s2p(s)
ncubes = ncubes + 1
cubes[ s ] = true
local dirs = {}
table.insert( dirs, p2s( x, y, z+1) )
table.insert( dirs, p2s( x, y, z-1) )
table.insert( dirs, p2s( x, y+1, z) )
table.insert( dirs, p2s( x, y-1, z) )
table.insert( dirs, p2s( x+1, y, z) )
table.insert( dirs, p2s( x-1, y, z) )
for _, d in ipairs(dirs) do
potneigh[d] = (potneigh[d] or 0) + 1
end
end
local neigh = 0
local sub2 = 0
for d, sum in pairs(potneigh) do
if cubes[d] then
neigh = neigh + sum
end
-- part 2
if not cubes[d] and sum == 6 then --trapped
sub2 = sub2 + 6
end
end
local result1 = ncubes*6 - neigh
print("part 1", result1)
-- incomplete (3178, too high)
local result2 = result1 - sub2
print("part 2", result2)