Advertisement
petrlos

2018/13

Nov 23rd, 2021 (edited)
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.68 KB | None | 0 0
  1. #AoC 2018 Day 13: Cart class
  2.  
  3. #TODO: Minecarts: test data work, normal not - Carts definovane jako trida, trizene pole
  4.  
  5. class Cart:
  6.     def __init__(self, x,y, direction):
  7.         self.coords = (x,y)
  8.         self.coordsIndex = y * 10000 + x
  9.         self.direction = direction
  10.         self.directionIndex = 0
  11.  
  12.     def getCoordsIndex(self):
  13.         x = self.coords[0]
  14.         y = self.coords[1]
  15.         self.coordsIndex = y * 10000  + x
  16.  
  17. from collections import Counter
  18.  
  19. def tupleSum(a,b):
  20.     return tuple([x + y for x, y in zip(a,b)])
  21.  
  22. #MAIN
  23.  
  24. with open ("data.txt") as file:
  25.     lines = file.read().splitlines()
  26.  
  27. cartsChars = "^>v<"
  28. up, right, down, left = (0,-1), (1,0), (0,1), (-1,0)
  29. slash = {up: right, down: left, left: down, right: up}
  30. backslash = {up:left, down:right, left:up, right:down}
  31. turn = [
  32.     {up: left, left: down, down: right, right: up},
  33.     {up: up, left: left, right: right, down: down},
  34.     {up: right, down:left, left: up, right: down}
  35. ]
  36. carts = []; tracks = {}
  37.  
  38. for y, line in enumerate(lines):
  39.     for x, char in enumerate(line):
  40.         if char in ["\\", "/", "-", "|", "+"]:
  41.             tracks.setdefault((x,y), char)
  42.         if char in cartsChars:
  43.             if char == "<":
  44.                 direction = left
  45.                 tracks.setdefault((x,y), "-")
  46.             elif  char == ">":
  47.                 direction = right
  48.                 tracks.setdefault((x,y), "-")
  49.             elif char == "v":
  50.                 direction = down
  51.                 tracks.setdefault((x,y), "|")
  52.             elif char == "^":
  53.                 direction = up
  54.                 tracks.setdefault((x, y), "|")
  55.             newCart = Cart(x,y,direction)
  56.             carts.append(newCart)
  57.  
  58. collided = False
  59. counter = 0
  60. while not collided:
  61.     counter += 1
  62.     locations = []
  63.     #sort carts
  64.     carts.sort(key = lambda x: x.coordsIndex, reverse = True)
  65.     #move carts
  66.     for cart in carts:
  67.         if tracks[cart.coords] == "\\":
  68.             #turn on backslash
  69.             cart.direction = backslash[cart.direction]
  70.         elif tracks[cart.coords] == "/":
  71.             #turn on slash
  72.             cart.direction = slash[cart.direction]
  73.         elif tracks[cart.coords] == "+":
  74.             currentTurn = turn[cart.directionIndex]
  75.             cart.direction = currentTurn[cart.direction]
  76.             cart.directionIndex = (cart.directionIndex + 1) % 3
  77.             #change direction
  78.         cart.coords = tupleSum(cart.coords, cart.direction)
  79.         cart.getCoordsIndex()
  80.         locations.append(cart.coords)
  81.     collisions = [x for x, y in Counter(locations).items() if y > 1]
  82.     if len(collisions) > 0:
  83.         print(collisions, counter)
  84.         collided = True
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement