|
|
|
@ -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)
|