Compare commits

...

2 Commits

Author SHA1 Message Date
sejo 6fa9d65e9e ignore swp 2023-12-10 07:17:27 +01:00
sejo af20879760 day 10, part 1! 2023-12-10 07:16:47 +01:00
4 changed files with 113 additions and 0 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*/*/input
.*.swp

102
12023/10/10.lua Normal file
View File

@ -0,0 +1,102 @@
io.input("input")
function coord(j,i) return {["j"]=j, ["i"]=i} end
function same(c1, c2)
if c1 and c2 then return c1.j==c2.j and c1.i==c2.i end
return false
end
function visited(c) return map[c.j][c.i].steps>=0 end
function two_connections(j, i) -- returns two coordinates of connected tiles
if j<1 or j>nrows or i<1 or i>ncols then
return nil,nil
end
local co = coord(j,i)
local tile = map[j][i]
local c = tile.char
if c=="S" then --find
local matches = {}
local x, y = two_connections(j-1, i) -- north
if same(co,x) or same(co,y) then
print("N!")
table.insert(matches, coord(j-1, i))
end
x, y = two_connections(j+1, i) -- south
if same(co,x) or same(co,y) then
print("S!")
table.insert(matches, coord(j+1, i))
end
x, y = two_connections(j, i-1) -- west
if same(co,x) or same(co,y) then
print("W!")
table.insert(matches, coord(j, i-1))
end
x, y = two_connections(j, i+1) -- east
if same(co,x) or same(co,y) then
print("E!")
table.insert(matches, coord(j, i+1))
end
return table.unpack(matches)
elseif c=="|" then -- north and south
return coord(j-1,i), coord(j+1,i)
elseif c=="-" then -- west and east
return coord(j,i-1), coord(j,i+1)
elseif c=="L" then -- north and east
return coord(j-1,i), coord(j,i+1)
elseif c=="J" then -- north and west
return coord(j-1,i), coord(j,i-1)
elseif c=="7" then -- south and west
return coord(j+1,i), coord(j,i-1)
elseif c=="F" then -- south and east
return coord(j+1,i), coord(j,i+1)
end
return nil, nil
end
map = {}
start = {}
j = 1
for line in io.lines() do
row = {}
i = 1
for c in line:gmatch("[%|%-LJ7F%.S]") do
tile = { char = c, steps = -1 }
if c=="S" then
start = coord(j, i)
tile.steps = 0
end
table.insert(row, tile)
i = i + 1
end
ncols = i-1
table.insert(map, row)
j = j + 1
end
nrows = j-1
print(map[start.j][start.i].char, start.j, start.i)
checking = {start}
maxsteps = 0
repeat
check = table.remove(checking,1)
steps = map[check.j][check.i].steps
if steps>maxsteps then maxsteps=steps end
-- print(steps, "checking", check.j, check.i, map[check.j][check.i].char)
x, y = two_connections(check.j,check.i)
if not visited(x) then
map[x.j][x.i].steps = steps+1
table.insert(checking,x)
end
if not visited(y) then
map[y.j][y.i].steps = steps+1
table.insert(checking,y)
end
-- print(map[x.j][x.i].char, map[y.j][y.i].char)
until #checking==0
print("part 1", maxsteps, steps)

5
12023/10/test Normal file
View File

@ -0,0 +1,5 @@
-L|F7
7S-7|
L|7||
-L-J|
L|-JF

5
12023/10/test2 Normal file
View File

@ -0,0 +1,5 @@
7-F7-
.FJ|7
SJLL7
|F--J
LJ.LJ