From 29847c588c3b9265e29fdeb234e01e8fe89720c4 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Thu, 24 Dec 2015 21:03:08 +0100 Subject: [PATCH] Replace PriorityQueue with heapq. Heapq is compatible with both python 3 and 2.7. It is also slightly faster because it doesn't use locking. --- day-22/solution.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/day-22/solution.py b/day-22/solution.py index fe4d599..d0b0221 100644 --- a/day-22/solution.py +++ b/day-22/solution.py @@ -1,5 +1,5 @@ from __future__ import print_function, division -from queue import PriorityQueue +import heapq def applyEffects(current): if current['recharge'] > 0: @@ -15,15 +15,14 @@ def applyEffects(current): def dijkstraMagic(initialState, hard): - todo = PriorityQueue() - todo.put((0, 0, initialState.copy())) + todo = [(0, 0, initialState.copy())] visited = {tuple(initialState.values()): 0} iteration = 0 - while not todo.empty(): + while todo: iteration += 1 - top = todo.get() + top = heapq.heappop(todo) current = top[2].copy() manaSpent = top[0] @@ -81,7 +80,7 @@ def dijkstraMagic(initialState, hard): if key not in visited or visited[key] > newMana: # Insert the route in to the visited and queue visited[key] = newMana - todo.put((newMana, iteration, newState)) + heapq.heappush(todo, (newMana, iteration, newState)) return None