Taddeus Kroes 7 jaren geleden
bovenliggende
commit
fa2b142d4c
4 gewijzigde bestanden met toevoegingen van 25 en 27 verwijderingen
  1. 9 10
      15_goblins.py
  2. 6 7
      20_regex.py
  3. 2 1
      21_underflow.py
  4. 8 9
      22_cave.py

+ 9 - 10
15_goblins.py

@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 import sys
 from operator import attrgetter
-from heapq import heapify, heappush, heappop
+from heapq import heappush, heappop
 
 SPACE, WALL = 0, 1
 GOBLINS, ELVES = 0, 1
@@ -61,15 +61,14 @@ class Unit:
 
         while Q:
             udist, u = heappop(Q)
-            if udist > dist[u]:
-                continue
-            for v in (u - w, u - 1, u + 1, u + w):
-                if grid[v] == SPACE:
-                    alt = udist + 1
-                    if alt < dist[v]:
-                        dist[v] = alt
-                        prev[v] = u
-                        heappush(Q, (alt, v))
+            if udist == dist[u]:
+                for v in (u - w, u - 1, u + 1, u + w):
+                    if grid[v] == SPACE:
+                        alt = udist + 1
+                        if alt < dist[v]:
+                            dist[v] = alt
+                            prev[v] = u
+                            heappush(Q, (alt, v))
 
         def adjacent_enemies(v):
             if dist[v] < inf and grid[v] == SPACE:

+ 6 - 7
20_regex.py

@@ -35,13 +35,12 @@ def shortest_paths(graph, source):
 
     while Q:
         udist, u = heappop(Q)
-        if udist > dist[u]:
-            continue
-        for v in graph[u]:
-            alt = udist + 1
-            if alt < dist[v]:
-                dist[v] = alt
-                heappush(Q, (alt, v))
+        if udist == dist[u]:
+            for v in graph[u]:
+                alt = udist + 1
+                if alt < dist[v]:
+                    dist[v] = alt
+                    heappush(Q, (alt, v))
 
     return dist
 

+ 2 - 1
21_underflow.py

@@ -40,7 +40,8 @@ def run():
             reg[ip] = 8
 
 def simulate():
-    r0 = r3 = r4 = 0
+    #r0 = 0
+    r3 = r4 = 0
     while True:
         r3 = r4 | 65536
         r4 = 4332021

+ 8 - 9
22_cave.py

@@ -37,15 +37,14 @@ def shortest_path(graph, source, target):
 
     while Q:
         udist, u = heappop(Q)
-        if udist > dist[u]:
-            continue
-        if u == target:
-            return udist
-        for v, weight in graph[u]:
-            alt = udist + weight
-            if alt < dist[v]:
-                dist[v] = alt
-                heappush(Q, (alt, v))
+        if udist == dist[u]:
+            if u == target:
+                return udist
+            for v, weight in graph[u]:
+                alt = udist + weight
+                if alt < dist[v]:
+                    dist[v] = alt
+                    heappush(Q, (alt, v))
 
 def rescue(grid, w):
     # approach: