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
 import operator
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
     for heading in headings:
6
     for heading in headings:
8
         new_pos = tuple(map(operator.add, pos, heading))
7
         new_pos = tuple(map(operator.add, pos, heading))
9
         try:
8
         try:
28
         cell = maze[pos]
27
         cell = maze[pos]
29
         steps += 1
28
         steps += 1
30
         if cell == '+':
29
         if cell == '+':
31
-            heading = junction_route(maze, pos, (-heading[0], -heading[1]))
30
+            heading = junction_route(maze, pos, heading)
32
         elif cell.isalpha():
31
         elif cell.isalpha():
33
             letters += cell
32
             letters += cell
34
 
33
 

Loading…
Cancel
Save