1
-
2
-
3
1
# This v1 works for part 1, not part 2
4
2
# Since it's also quite slow, I've done a v2 that should be better
5
3
6
4
7
-
8
-
9
-
10
-
11
5
# -------------------------------- Input data -------------------------------- #
12
6
import os , pathfinding , re
13
7
14
8
test_data = {}
15
9
16
10
test = 1
17
- test_data [test ] = {"input" : """/->-\\
11
+ test_data [test ] = {
12
+ "input" : """/->-\\
18
13
| | /----\\
19
14
| /-+--+-\ |
20
15
| | | | v |
21
16
\-+-/ \-+--/
22
17
\------/ """ ,
23
- "expected" : [' Unknown' , ' Unknown' ],
24
- }
18
+ "expected" : [" Unknown" , " Unknown" ],
19
+ }
25
20
26
21
test += 1
27
- test_data [test ] = {"input" : r"""/>-<\
22
+ test_data [test ] = {
23
+ "input" : r"""/>-<\
28
24
| |
29
25
| /<+-\
30
26
| | | v
31
27
\>+</ |
32
28
| ^
33
29
\<->/""" ,
34
- "expected" : ['Unknown' , 'Unknown' ],
35
- }
36
-
37
- test = 'real'
38
- input_file = os .path .join (os .path .dirname (__file__ ), 'Inputs' , os .path .basename (__file__ ).replace ('.py' , '.txt' ))
39
- test_data [test ] = {"input" : open (input_file , "r+" ).read (),
40
- "expected" : ['124,130' , '99, 96' ],
41
- }
30
+ "expected" : ["Unknown" , "Unknown" ],
31
+ }
32
+
33
+ test = "real"
34
+ input_file = os .path .join (
35
+ os .path .dirname (__file__ ),
36
+ "Inputs" ,
37
+ os .path .basename (__file__ ).replace (".py" , ".txt" ),
38
+ )
39
+ test_data [test ] = {
40
+ "input" : open (input_file , "r+" ).read (),
41
+ "expected" : ["124,130" , "99, 96" ],
42
+ }
42
43
43
44
# -------------------------------- Control program execution -------------------------------- #
44
45
45
- case_to_test = ' real'
46
+ case_to_test = " real"
46
47
part_to_test = 2
47
48
48
49
# -------------------------------- Initialize some variables -------------------------------- #
49
50
50
- puzzle_input = test_data [case_to_test ][' input' ]
51
- puzzle_expected_result = test_data [case_to_test ][' expected' ][part_to_test - 1 ]
52
- puzzle_actual_result = ' Unknown'
51
+ puzzle_input = test_data [case_to_test ][" input" ]
52
+ puzzle_expected_result = test_data [case_to_test ][" expected" ][part_to_test - 1 ]
53
+ puzzle_actual_result = " Unknown"
53
54
54
55
55
56
# -------------------------------- Actual code execution -------------------------------- #
56
57
57
58
58
- def grid_to_vertices (self , grid , wall = '#' ):
59
+ def grid_to_vertices (self , grid , wall = "#" ):
59
60
self .vertices = []
60
61
track = {}
61
62
y = 0
62
63
63
64
for line in grid .splitlines ():
64
- line = line .replace ('^' , '|' ).replace ('v' , '|' ).replace ('>' , '-' ).replace ('<' , '-' )
65
+ line = (
66
+ line .replace ("^" , "|" ).replace ("v" , "|" ).replace (">" , "-" ).replace ("<" , "-" )
67
+ )
65
68
for x in range (len (line )):
66
69
if line [x ] != wall :
67
70
self .vertices .append ((x , y ))
68
71
track [(x , y )] = line [x ]
69
72
70
73
y += 1
71
74
72
- directions = [(1 , 0 ), (- 1 , 0 ), (0 , 1 ), (0 , - 1 )]
73
- right , left , down , up = directions
75
+ north = 1j
76
+ south = - 1j
77
+ west = - 1
78
+ east = 1
79
+
80
+ directions = [north , south , west , east ]
74
81
75
- for coords in self .vertices :
82
+ for source in self .vertices :
76
83
for direction in directions :
77
- x , y = coords [ 0 ] + direction [ 0 ], coords [ 1 ] + direction [ 1 ]
84
+ target = source + direction
78
85
79
- if track [coords ] == '-' and direction in [up , down ]:
86
+ if track [source ] == "-" and direction in [north , south ]:
80
87
continue
81
- if track [coords ] == '|' and direction in [left , right ]:
88
+ if track [source ] == "|" and direction in [west , east ]:
82
89
continue
83
90
84
- if ( x , y ) in self .vertices :
85
- if track [coords ] in (' \\ ' , '/' ):
86
- if track [( x , y ) ] in (' \\ ' , '/' ):
91
+ if target in self .vertices :
92
+ if track [source ] in (" \\ " , "/" ):
93
+ if track [target ] in (" \\ " , "/" ):
87
94
continue
88
- if track [( x , y ) ] == '-' and direction in [up , down ]:
95
+ if track [target ] == "-" and direction in [north , south ]:
89
96
continue
90
- elif track [( x , y ) ] == '|' and direction in [left , right ]:
97
+ elif track [target ] == "|" and direction in [west , east ]:
91
98
continue
92
- if coords in self .edges :
93
- self .edges [(coords )].append (( x , y ) )
99
+ if source in self .edges :
100
+ self .edges [(source )].append (target )
94
101
else :
95
- self .edges [(coords )] = [( x , y ) ]
102
+ self .edges [(source )] = [target ]
96
103
97
104
return True
98
105
99
- pathfinding .Graph .grid_to_vertices = grid_to_vertices
100
106
101
- def turn_left (direction ):
102
- return (direction [1 ], - direction [0 ])
107
+ pathfinding .Graph .grid_to_vertices = grid_to_vertices
103
108
104
- def turn_right (direction ):
105
- return (- direction [1 ], direction [0 ])
106
109
110
+ def turn_left (direction ):
111
+ return (direction [1 ], - direction [0 ])
107
112
108
113
114
+ def turn_right (direction ):
115
+ return (- direction [1 ], direction [0 ])
109
116
110
117
111
118
# Analyze grid
112
119
grid = puzzle_input
113
120
graph = pathfinding .Graph ()
114
- graph .grid_to_vertices (puzzle_input , ' ' )
121
+ graph .grid_to_vertices (puzzle_input , " " )
115
122
116
- intersections = graph .grid_search (grid , '+' )[ '+' ]
123
+ intersections = graph .grid_search (grid , "+" )[ "+" ]
117
124
118
- directions = {'^' : (0 , - 1 ), '>' : (1 , 0 ), '<' : (- 1 , 0 ), 'v' : (0 , 1 )}
119
- dirs = {(0 , - 1 ): '^' , (1 , 0 ): '>' , (- 1 , 0 ): '<' , (0 , 1 ): 'v' }
125
+ directions = {"^" : (0 , - 1 ), ">" : (1 , 0 ), "<" : (- 1 , 0 ), "v" : (0 , 1 )}
126
+ dirs = {(0 , - 1 ): "^" , (1 , 0 ): ">" , (- 1 , 0 ): "<" , (0 , 1 ): "v" }
120
127
121
128
122
129
# Find carts
123
- list_carts = graph .grid_search (grid , ('^' , '<' , '>' , 'v' ))
130
+ list_carts = graph .grid_search (grid , ("^" , "<" , ">" , "v" ))
124
131
carts = []
125
132
cart_positions = []
126
133
for direction in list_carts :
@@ -143,15 +150,16 @@ def turn_right (direction):
143
150
pos , dir , choice = cart
144
151
new_pos = (pos [0 ] + dir [0 ], pos [1 ] + dir [1 ])
145
152
146
- print (pos , choice , dirs [dir ])
147
-
153
+ print (pos , choice , dirs [dir ])
148
154
149
155
# We need to turn
150
156
if new_pos not in graph .edges [pos ]:
151
- options = [((pos [0 ] + x [0 ], pos [1 ] + x [1 ]), x )
152
- for x in directions .values ()
153
- if x != (- dir [0 ], - dir [1 ])
154
- and (pos [0 ] + x [0 ], pos [1 ] + x [1 ]) in graph .edges [pos ]]
157
+ options = [
158
+ ((pos [0 ] + x [0 ], pos [1 ] + x [1 ]), x )
159
+ for x in directions .values ()
160
+ if x != (- dir [0 ], - dir [1 ])
161
+ and (pos [0 ] + x [0 ], pos [1 ] + x [1 ]) in graph .edges [pos ]
162
+ ]
155
163
new_pos , dir = options [0 ]
156
164
157
165
# Intersection
@@ -165,25 +173,20 @@ def turn_right (direction):
165
173
166
174
new_cart = (new_pos , dir , choice )
167
175
168
-
169
-
170
-
171
-
172
176
# Check collisions
173
177
if new_cart [0 ] in cart_positions :
174
178
if part_to_test == 1 :
175
179
puzzle_actual_result = new_cart [0 ]
176
180
break
177
181
else :
178
- print ( ' collision' , new_cart [0 ])
182
+ print ( " collision" , new_cart [0 ])
179
183
collision += 1
180
184
carts = [c for c in carts if c [0 ] != new_cart [0 ]]
181
185
cart_positions = [c [0 ] for c in carts ]
182
186
else :
183
187
carts .append (new_cart )
184
188
cart_positions .append (new_cart [0 ])
185
189
186
-
187
190
# Count ticks + sort carts
188
191
subtick += 1
189
192
if subtick == nb_carts - collision :
@@ -194,18 +197,14 @@ def turn_right (direction):
194
197
carts = sorted (carts , key = lambda x : (x [0 ][1 ], x [0 ][0 ]))
195
198
cart_positions = [c [0 ] for c in carts ]
196
199
197
- print ( ' End of tick' , tick , ' - Remaining' , len (carts ))
200
+ print ( " End of tick" , tick , " - Remaining" , len (carts ))
198
201
if len (carts ) == 1 :
199
202
break
200
203
201
204
if part_to_test == 2 :
202
205
puzzle_actual_result = carts
203
- #99, 96
206
+ # 99, 96
204
207
# -------------------------------- Outputs / results -------------------------------- #
205
208
206
- print ('Expected result : ' + str (puzzle_expected_result ))
207
- print ('Actual result : ' + str (puzzle_actual_result ))
208
-
209
-
210
-
211
-
209
+ print ("Expected result : " + str (puzzle_expected_result ))
210
+ print ("Actual result : " + str (puzzle_actual_result ))
0 commit comments