Browse Source

Little performance enhancement for day 22

master
Chris Smith 5 years ago
parent
commit
528d631b73
1 changed files with 12 additions and 9 deletions
  1. 12
    9
      day22.nim

+ 12
- 9
day22.nim View File

@@ -90,18 +90,21 @@ while not distances.hasKey(targetNode):
90 90
     
91 91
     distances[node] = distance
92 92
 
93
-    # At each node we can switch tools once, with a cost of 7 minutes
94
-    for tool in tools[erosions[node.pos] mod 3]:
95
-        if tool != node.tool and not distances.hasKey((node.pos, tool)):
96
-            stack.insert(((node.pos, tool)), distance + 7)
97
-
98 93
     # Up to four possible moves from the current node, depending on
99 94
     # terrain and tools.
100 95
     for newPos in node.pos.moves:
101
-        if erosions.hasKey(newPos) and
102
-                node.tool in tools[erosions[newPos] mod 3] and
103
-                not distances.hasKey((newPos, node.tool)):
104
-            stack.insert(((newPos, node.tool)), distance + 1)
96
+        if erosions.hasKey(newPos) and not distances.hasKey((newPos, node.tool)):
97
+            let targetTools = tools[erosions[newPos] mod 3]
98
+            if node.tool in targetTools:
99
+                # We have a tool that lets us pass, there's no point trying to
100
+                # swap.
101
+                stack.insert(((newPos, node.tool)), distance + 1)
102
+            else:
103
+                # See if we can switch to a compatible tool and treat it as
104
+                # a single step.
105
+                for newTool in targetTools:
106
+                    if newTool in tools[erosions[node.pos] mod 3]:
107
+                        stack.insert(((newPos, newTool)), distance + 8)
105 108
 
106 109
 echo dangerSum
107 110
 echo distances[targetNode]