-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16.py
More file actions
126 lines (108 loc) · 3.09 KB
/
Copy path16.py
File metadata and controls
126 lines (108 loc) · 3.09 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import sys
import pytest
from pathlib import Path
from textwrap import dedent
UP = "^"
DOWN = "v"
LEFT = "<"
RIGHT = ">"
def traverse(
matrix: list[list[str]],
start: tuple[int, int],
direction: str,
beans: set[tuple[int, int, str]],
):
i, j = start
if i < 0 or i >= len(matrix) or j < 0 or j >= len(matrix[i]):
return
if (i, j, direction) in beans:
return
beans.add((i, j, direction))
if matrix[i][j] == "|":
if direction in (LEFT, RIGHT):
traverse(matrix, (i - 1, j), UP, beans)
traverse(matrix, (i + 1, j), DOWN, beans)
if direction == UP:
traverse(matrix, (i - 1, j), UP, beans)
elif direction == DOWN:
traverse(matrix, (i + 1, j), DOWN, beans)
return
if matrix[i][j] == "-":
if direction in (UP, DOWN):
traverse(matrix, (i, j - 1), LEFT, beans)
traverse(matrix, (i, j + 1), RIGHT, beans)
if direction == LEFT:
traverse(matrix, (i, j - 1), LEFT, beans)
elif direction == RIGHT:
traverse(matrix, (i, j + 1), RIGHT, beans)
return
if matrix[i][j] == "\\":
if direction == UP:
traverse(matrix, (i, j - 1), LEFT, beans)
elif direction == DOWN:
traverse(matrix, (i, j + 1), RIGHT, beans)
elif direction == LEFT:
traverse(matrix, (i - 1, j), UP, beans)
elif direction == RIGHT:
traverse(matrix, (i + 1, j), DOWN, beans)
return
if matrix[i][j] == "/":
if direction == UP:
traverse(matrix, (i, j + 1), RIGHT, beans)
elif direction == DOWN:
traverse(matrix, (i, j - 1), LEFT, beans)
elif direction == LEFT:
traverse(matrix, (i + 1, j), DOWN, beans)
elif direction == RIGHT:
traverse(matrix, (i - 1, j), UP, beans)
return
while (
i >= 0
and i < len(matrix)
and j >= 0
and j < len(matrix[i])
and matrix[i][j] == "."
):
beans.add((i, j, direction))
if direction == RIGHT:
j += 1
elif direction == LEFT:
j -= 1
elif direction == UP:
i -= 1
elif direction == DOWN:
i += 1
traverse(matrix, (i, j), direction, beans)
def solve(text: str) -> int:
matrix = [[c for c in line] for line in text.splitlines()]
beans = set()
traverse(matrix, (0, 0), RIGHT, beans)
unique_beans = set()
for i, j, _ in beans:
unique_beans.add((i, j))
return len(unique_beans)
if __name__ == "__main__":
p = Path(sys.argv[1])
print(solve(p.read_text()))
def test():
text = dedent(
r"""
.|...\....
|.-.\.....
.....|-...
........|.
..........
.........\
..../.\\..
.-.-/..|..
.|....-|.\
..//.|....
"""
).strip()
assert solve(text) == 46
def test_input():
p = Path("16.txt")
if not p.exists():
pytest.skip(f"{p} does not exist")
text = p.read_text()
assert solve(text) == 7046