day 16: cleaned up a bit

This commit is contained in:
sejo 2023-12-16 08:47:13 +01:00
parent 20578ca8de
commit 14d3e749cd
1 changed files with 20 additions and 46 deletions

View File

@ -1,4 +1,3 @@
io.input("test")
io.input("input")
-- parse map
@ -53,6 +52,13 @@ function count_energized(e)
return count
end
function valid_beam(x, y, dx, dy)
if x>=1 and x<=width and y>=1 and y<=height then
return com(x,y,dx,dy)
end
return nil
end
function next_beams(xs,ys,dx,dy,char,conditions)
if not char then return nil end
local cond = table.concat(com(xs,ys,dx,dy),",")
@ -60,58 +66,31 @@ function next_beams(xs,ys,dx,dy,char,conditions)
else conditions[cond] = true end
local beams = {}
local nx, ny, ndx, ndy
if char=="|" then -- splitters
if dx~=0 then -- duplicate
if ys+1<=height then
table.insert(beams, com(xs, ys+1, 0, 1))
end
if ys-1>=1 then
table.insert(beams, com(xs, ys-1, 0, -1))
end
table.insert(beams, valid_beam(xs, ys+1, 0, 1))
table.insert(beams, valid_beam(xs, ys-1, 0, -1))
elseif dy~= 0 then -- passthrough
ny = ys+dy
if ny>=1 and ny<=height then
table.insert(beams, com(xs, ys+dy, dx, dy))
end
table.insert(beams, valid_beam(xs, ys+dy, dx, dy))
end
elseif char=="-" then
if dx~=0 then -- passthrough
nx = xs + dx
if nx>=1 and nx<=width then
table.insert(beams, com(xs+dx, ys, dx, dy))
end
table.insert(beams, valid_beam(xs+dx, ys, dx, dy))
elseif dy~= 0 then -- duplicate
if xs+1<=width then
table.insert(beams, com(xs+1, ys, 1, 0))
end
if xs-1>=1 then
table.insert(beams, com(xs-1, ys, -1, 0))
end
table.insert(beams, valid_beam(xs+1, ys, 1, 0))
table.insert(beams, valid_beam(xs-1, ys, -1, 0))
end
elseif char=="/" then --mirrors
if dx~=0 then
ny = ys - dx
if ny>=1 and ny<=height then
table.insert(beams, com(xs, ny, 0, -dx))
end
table.insert(beams, valid_beam(xs, ys-dx, 0, -dx))
elseif dy~=0 then
nx = xs - dy
if nx>=1 and nx<=width then
table.insert(beams, com(nx, ys, -dy, 0))
end
table.insert(beams, valid_beam(xs-dy, ys, -dy, 0))
end
elseif char=="\\" then
if dx~=0 then
ny = ys+dx
if ny>=1 and ny<=height then
table.insert(beams, com(xs, ny, 0, dx))
end
table.insert(beams, valid_beam(xs, ys+dx, 0, dx))
elseif dy~=0 then
nx = xs + dy
if nx>=1 and nx<=width then
table.insert(beams, com(nx, ys, dy, 0))
end
table.insert(beams, com(xs+dy, ys, dy, 0))
end
end
return beams
@ -131,11 +110,10 @@ function activate(x, y, dx, dy)
local x,y,dx,dy = exp(beam)
--print("\nbeam", x, y, dx, dy)
local xf, yf = 0,0
local charf = nil
local found = nil
--search next
if dx~= 0 then
local xm = x
local found = nil
repeat
if map[y][xm] then
found = map[y][xm]
@ -144,10 +122,8 @@ function activate(x, y, dx, dy)
end
until found or xm<1 or xm>width
xf, yf = xm, y
charf = found
elseif dy~= 0 then
local ym = y
local found = nil
repeat
if map[ym][x] then
found = map[ym][x]
@ -156,13 +132,12 @@ function activate(x, y, dx, dy)
end
until found or ym<1 or ym>height
xf,yf = x,ym
charf = found
end
--print("found", charf, xf, yf)
--print("found", found, xf, yf)
-- mark energized
mark_energized(energized,x,y,xf,yf)
local nb = next_beams(xf, yf, dx, dy, charf, conditions)
local nb = next_beams(xf, yf, dx, dy, found, conditions)
if nb then
for _, b in ipairs(nb) do
table.insert(beams, b)
@ -213,6 +188,5 @@ for y=1,height do
end
end
print(table.concat(max_conds,","))
print("part 2", max)