Advertisement
Guest User

Advent of code day 17

a guest
Dec 17th, 2016
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import re, sys, itertools
  3. import hashlib
  4. import astar
  5. import resource
  6. from heapq import heappush, heappop
  7. import math
  8. import pprint
  9.  
  10.  
  11. def pathTo(node, cameFrom):
  12.     path = [node]
  13.     while node in cameFrom:
  14.         node = cameFrom[node]
  15.         path.append(node)
  16.     return list(reversed(path))
  17.  
  18. def search(start):
  19.     counter = itertools.count()
  20.     closed = set()
  21.     queue = [(start.heuristic(), next(counter), start)]
  22.     bestCostTo = {start: 0}
  23.     cameFrom = {}
  24.  
  25.     while len(queue) > 0:
  26.         _, _, node = heappop(queue)
  27.  
  28.         if node.isGoal():
  29.             return bestCostTo[node], pathTo(node, cameFrom)
  30.         closed.add(node)
  31.  
  32.         for edgeCost, nextNode in node.neighbors():
  33.             if nextNode in closed:
  34.                 continue
  35.             if nextNode.isFailure():
  36.                 continue
  37.  
  38.             nextCost = bestCostTo[node] + edgeCost
  39.             if nextCost >= bestCostTo.get(nextNode, float('inf')):
  40.                 continue
  41.  
  42.             cameFrom[nextNode] = node
  43.             bestCostTo[nextNode] = nextCost
  44.             heappush(queue,
  45.                     (nextCost + nextNode.heuristic(), next(counter), nextNode))
  46.  
  47.     return "failure", tuple()
  48. resource.setrlimit(resource.RLIMIT_AS, (1024**3 - 1024, 1024**3))
  49. hashes = {}
  50. def ha(s):
  51.     if s in hashes:
  52.         return hashes[s]
  53.     return hashlib.md5(bytes(s, encoding='ascii')).hexdigest()
  54.  
  55. pw = "veumntbg"
  56. class Node:
  57.     def __init__(self, x, y, path):
  58.         self.x = x
  59.         self.y = y
  60.         self.path = path
  61.  
  62.     def isGoal(self):
  63.         return self.x == 3 and self.y == 3
  64.  
  65.     def isFailure(self):
  66.         return False
  67.  
  68.     def neighbors(self):
  69.         h = ha(pw + self.path)
  70.         for dx, dy, d, i in ((0,-1,"U",0), (0,1,"D",1),(-1, 0, "L",2) ,(1,0,"R",3)):
  71.             if self.x + dx >= 0 and self.x + dx < 4 and self.y + dy >= 0 and self.y + dy < 4:
  72.                 if ord(h[i]) >= ord('b'):
  73.                     yield -1, Node(self.x + dx, self.y + dy, self.path + d)
  74.  
  75.     def heuristic(self):
  76.         if self.isGoal():
  77.             return 0
  78.         return -10000
  79.         #return 6 - self.x - self.y
  80.  
  81.     def __eq__(self, other):
  82.         return isinstance(other, Node) and \
  83.                 self.x == other.x and self.y == other.y and self.path == other.path
  84.  
  85.     def __hash__(self):
  86.         return hash((self.x, self.y, self.path))
  87.  
  88.     def __repr__(self):
  89.         return str(self.x) + " " + str(self.y) + " " + str(self.path)
  90.  
  91. c, path = astar.search(Node(0,0,""))
  92. print(-c)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement