-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc23_day23_1.py
executable file
·66 lines (52 loc) · 1.54 KB
/
aoc23_day23_1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
import sys
def read_maze():
res = []
while True:
try:
res.append(input().strip())
except EOFError:
break
return res
def _dfs(pos, target, maze, visited, depth, longest):
if pos in visited:
return
visited.add(pos)
if pos == target:
if depth > longest[0]:
longest[0] = depth
for step in ((1, 0), (-1, 0), (0, 1), (0, -1)):
new_r = pos[0] + step[0]
new_c = pos[1] + step[1]
if new_r >= 0 and new_r < len(maze) \
and new_c >= 0 and new_c < len(maze[0]) \
and maze[new_r][new_c] != '#':
new_depth = depth + 1
if maze[new_r][new_c] == 'v':
new_r += 1
new_depth += 1
elif maze[new_r][new_c] == '>':
new_c += 1
new_depth += 1
elif maze[new_r][new_c] == '<':
new_c -= 1
new_depth += 1
elif maze[new_r][new_c] == '^':
new_r -= 1
new_depth += 1
_dfs((new_r, new_c), target, maze, visited, new_depth, longest)
visited.remove(pos)
def dfs(start, target, maze):
visited = set()
longest = [0]
_dfs(start, target, maze, visited, 0, longest)
return longest[0]
def main():
maze = read_maze()
start = (0, 1)
target = (len(maze) - 1, len(maze[0]) - 2)
sys.setrecursionlimit(1000000)
res = dfs(start, target, maze)
print(res)
if __name__ == '__main__':
main()