@@ -33,28 +33,48 @@ def out_of_bounds(area, cursor) -> bool:
33
33
)
34
34
35
35
36
- def count_paces (area_str : str ) -> int :
37
- area_list_of_lists = [list (line ) for line in area_str .splitlines ()[1 :]]
38
- area = np .array (area_list_of_lists )
39
- area = np .pad (area ,1 , "constant" ,constant_values = '_' )
40
-
41
- (cursor ,) = np .argwhere (area == "^" )
36
+ def has_loop (area ) -> int :
42
37
direction = np .array ([- 1 , 0 ])
43
38
44
39
total_visited = 0
45
40
newly_visited = True
41
+ visited_set = set ()
42
+ (cursor ,) = np .argwhere (area == "^" )
43
+
46
44
47
- while not area [tuple (cursor )] == "_" :
45
+
46
+ while not (area [tuple (cursor )] == "_" or (tuple (cursor ),tuple (direction )) in visited_set ):
47
+ visited_set .add ((tuple (cursor ),tuple (direction )))
48
48
total_visited += newly_visited
49
49
cursor , direction , newly_visited = walk (area , cursor , direction )
50
50
51
- return total_visited
51
+ return (tuple (cursor ),tuple (direction )) in visited_set
52
+
53
+ def iterate_obstacles (area_str : str ):
54
+ area_list_of_lists = [list (line ) for line in area_str .splitlines ()[1 :]]
55
+ area = np .array (area_list_of_lists )
56
+ area_padded = np .pad (area ,1 , "constant" ,constant_values = '_' )
57
+ loops = 0
58
+
59
+ for x_pos in range (1 , area_padded .shape [0 ]- 1 ):
60
+ for y_pos in range (1 , area_padded .shape [1 ]- 1 ):
61
+ area_copy = area_padded .copy ()
62
+ # print(x_pos, y_pos)
63
+ if area_padded [(x_pos , y_pos )] == "^" :
64
+ print ("HELP ME" )
65
+ else :
66
+ area_copy [(x_pos ,y_pos )] = "#"
67
+ if has_loop (area_copy ):
68
+ loops += 1
69
+ print (x_pos )
70
+
71
+ return loops
52
72
53
73
54
74
def main ():
55
75
with open ("input" ,"r" ) as f :
56
76
data = f .read ()
57
- print (count_paces (data ))
77
+ print (iterate_obstacles (data ))
58
78
59
79
60
80
if __name__ == "__main__" :
0 commit comments