Browse Source

Optimise junction handling slightly.

Junctions always involve a 90 degree turn, so we only need
to check two directions.
master
Chris Smith 6 years ago
parent
commit
83358f80a8
1 changed files with 3 additions and 4 deletions
  1. 3
    4
      19.py

+ 3
- 4
19.py View File

@@ -1,9 +1,8 @@
1 1
 import operator
2 2
 
3 3
 
4
-def junction_route(maze, pos, ignore_heading):
5
-    headings = [(-1, 0), (1, 0), (0, -1), (0, 1)]
6
-    headings.remove(ignore_heading)
4
+def junction_route(maze, pos, original_heading):
5
+    headings = [(-1, 0), (1, 0)] if original_heading[0] == 0 else [(0, -1), (0, 1)]
7 6
     for heading in headings:
8 7
         new_pos = tuple(map(operator.add, pos, heading))
9 8
         try:
@@ -28,7 +27,7 @@ with open('data/19.txt') as file:
28 27
         cell = maze[pos]
29 28
         steps += 1
30 29
         if cell == '+':
31
-            heading = junction_route(maze, pos, (-heading[0], -heading[1]))
30
+            heading = junction_route(maze, pos, heading)
32 31
         elif cell.isalpha():
33 32
             letters += cell
34 33
 

Loading…
Cancel
Save