This commit is contained in:
Fedor Lyanguzov
2025-10-18 13:38:38 +03:00
parent 9a38d895f0
commit 8369cddcd8
6 changed files with 602 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
def f(inp):
def g(i, j, l):
old_i, old_j = i, j
visited.add((i, j))
if (i, j)==end:
paths.append(l)
else:
crossing = False
deadend = False
oi, oj = i, j
while not (crossing or deadend or (i, j)==end):
steps = []
for di, dj in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
if inp[i+di][j+dj] in '.v>':
steps.append((i+di, j+dj))
if len(steps)>2:
crossing = True
continue
if (oi, oj)!=steps[0]:
oi, oj = i, j
i, j = steps[0]
elif len(steps)==2:
oi, oj = i, j
i, j = steps[1]
else:
deadend = True
continue
l += 1
if deadend:
return
elif (i, j)==end:
g(i, j, l)
elif crossing and (i, j) not in visited:
for di, dj in [(0, 1), (1, 0), (0, -1), (-1, 0)]:
if di==1 and inp[i+di][j+dj]=='v':
g(i+di, j+dj, l+1)
elif dj==1 and inp[i+di][j+dj]=='>':
g(i+di, j+dj, l+1)
visited.remove((old_i, old_j))
visited = set()
paths = []
inp = ['#'*len(inp[0])] + inp + ['#'*len(inp[0])]
si = 1
sj = inp[1].index('.')
end = (len(inp)-2, inp[-2].index('.'))
g(si, sj, 0)
return sorted(paths, reverse=True)
inp = '''
#.#####################
#.......#########...###
#######.#########.#.###
###.....#.>.>.###.#.###
###v#####.#v#.###.#.###
###.>...#.#.#.....#...#
###v###.#.#.#########.#
###...#.#.#.......#...#
#####.#.#.#######.#.###
#.....#.#.#.......#...#
#.#####.#.#.#########v#
#.#...#...#...###...>.#
#.#.#v#######v###.###v#
#...#.>.#...>.>.#.###.#
#####v#.#.###v#.#.###.#
#.....#...#...#.#.#...#
#.#########.###.#.#.###
#...###...#...#...#.###
###.###.#.###v#####v###
#...#...#.#.>.>.#.>.###
#.###.###.#.###.#.#v###
#.....###...###...#...#
#####################.#
'''.strip().split('\n')
assert f(inp)==[94, 90, 86, 82, 82, 74]
inp = open('input.txt').read().strip().split('\n')
print(max(f(inp)))