@@ -13,28 +13,35 @@ def find_start(g: grid.Grid) -> coord.Coordinate:
13
13
if g [pos ] == "^" :
14
14
return pos
15
15
assert False , "no start point found"
16
-
17
-
16
+
17
+
18
18
def modplus (x : int ) -> int :
19
19
return (x + 1 ) % 4
20
20
21
-
22
- dirs = [coord .Direction .Up , coord .Direction .Right , coord .Direction .Down , coord .Direction .Left ]
21
+
22
+ dirs = [
23
+ coord .Direction .Up ,
24
+ coord .Direction .Right ,
25
+ coord .Direction .Down ,
26
+ coord .Direction .Left ,
27
+ ]
23
28
24
29
25
30
class LoopEncounteredException (Exception ):
26
31
pass
27
32
28
33
29
- def trace (g : grid .Grid , guard_pos : coord .Coordinate , guard_direction : int ) -> set [tuple [coord .Coordinate , int ]]:
34
+ def trace (
35
+ g : grid .Grid , guard_pos : coord .Coordinate , guard_direction : int
36
+ ) -> set [tuple [coord .Coordinate , int ]]:
30
37
visited_sequence = set ()
31
-
38
+
32
39
while guard_pos in g :
33
40
if (guard_pos , guard_direction ) in visited_sequence :
34
41
raise LoopEncounteredException
35
-
42
+
36
43
visited_sequence .add ((guard_pos , guard_direction ))
37
-
44
+
38
45
nc = coord .add (guard_pos , dirs [guard_direction % 4 ].delta ())
39
46
if nc in g and g [nc ] == "#" :
40
47
guard_direction = modplus (guard_direction )
@@ -51,11 +58,11 @@ def one(instr: str) -> int:
51
58
52
59
def two (instr : str ) -> int :
53
60
g = parse (instr )
54
-
61
+
55
62
start_pos = find_start (g )
56
63
seq = trace (g , start_pos , 0 )
57
64
known_blocks = set ()
58
-
65
+
59
66
for (pos , _ ) in tqdm (seq , file = sys .stderr ):
60
67
assert pos in g , "pos off the rails"
61
68
g [pos ] = "#"
@@ -64,7 +71,7 @@ def two(instr: str) -> int:
64
71
except LoopEncounteredException :
65
72
known_blocks .add (pos )
66
73
g [pos ] = "."
67
-
74
+
68
75
return len (known_blocks )
69
76
70
77
@@ -81,4 +88,4 @@ def _debug(*args, **kwargs):
81
88
if sys .argv [1 ] == "1" :
82
89
print (one (inp ))
83
90
else :
84
- print (two (inp ))
91
+ print (two (inp ))
0 commit comments