daily pastebin goal
33%
SHARE
TWEET

Day13 Attempt

a guest Dec 14th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import sys
  2.  
  3. tracks = []
  4. carts = []
  5.  
  6. _input = open('2018/Day13/Day13.txt', 'r').readlines()
  7.  
  8. _input1 = [
  9.   "/->-\       ",
  10.   "|   |  /----\\",
  11.   "| /-+--+-\ |",
  12.   "| | |  | v  |",
  13.   "\-+-/  \-+--/",
  14.   "  \------/   ",
  15. ]
  16.  
  17. _input2 = [
  18.   "/>-<\ ",
  19.   "|   |  ",
  20.   "| /<+-\\",
  21.   "| | | v",
  22.   "\>+</ |",
  23.   "  |   ^",
  24.   "  \<->/",
  25. ]
  26.  
  27. for a in _input:
  28.   if a:
  29.     tracks.append([t for t in a])
  30.  
  31. DX = [1,0,-1,0]
  32. DY = [0,-1,0,1]
  33.  
  34. class Cart(object):
  35.   def __init__(self, x, y, d, t):
  36.     self.x = x
  37.     self.y = y
  38.     self.d = d
  39.     self.t = t
  40.     self.crashed = False
  41.   def pretty(self):
  42.     return "c: {};{} - d: {} - t: {} - crashed: {}".format(self.x, self.y, self.d, self.t, self.crashed)
  43.   def right(self):
  44.     self.d = (self.d - 1) % 4
  45.   def left(self):
  46.     self.d = (self.d - 1) % 4
  47.   def intersect(self):
  48.     if self.t == 0:
  49.       self.left()
  50.     if self.t == 2:
  51.       self.right()
  52.     self.t = (self.t + 1) % 3
  53.   def specialT(self, t):
  54.     if t == '/':
  55.       self.d = {0:1, 1:0, 2:3, 3:2}[self.d]
  56.     elif t == '\\':
  57.       self.d = {0:3, 1:2, 2:1, 3:0}[self.d]
  58.     elif t == '+':
  59.       self.intersect()
  60.   def prune(self):
  61.     carts.remove(self)
  62.   def fwd(self):
  63.     newX = self.x + DX[self.d]
  64.     newY = self.y + DY[self.d]
  65.     # If crashes
  66.     if (newX, newY) in [(a.x,a.y) for a in carts]:
  67.       # print('Crash in: {},{}'.format(newX, newY))
  68.       for k in carts:
  69.         if newX == k.x and newY == k.y:
  70.           k.crashed = True
  71.           self.crashed = True
  72.           return False, k
  73.     else:
  74.       # If special track
  75.       self.specialT(tracks[newY][newX])
  76.       # Assign new coordinates
  77.       self.x, self.y = newX, newY
  78.       return True, self
  79.  
  80. def showTracks():
  81.   for r in range(len(tracks)):
  82.     for c in range(len(tracks[r])):
  83.       isCart = False
  84.       for cart in carts:
  85.         if cart.crashed: continue
  86.         if (cart.x, cart.y) == (c,r):
  87.           print({0: '>', 1:'^', 2:'<', 3:'v'}[cart.d], end='')
  88.           isCart = True
  89.       if not isCart:
  90.         print(tracks[r][c], end = '')
  91.     print()
  92.  
  93. for r in range(len(tracks)):
  94.   for c in range(len(tracks[r])):
  95.     if tracks[r][c] == '>':
  96.       tracks[r][c] = '-'
  97.       carts.append(Cart(c,r,0,0))
  98.     if tracks[r][c] == '^':
  99.       tracks[r][c] = '|'
  100.       carts.append(Cart(c,r,1,0))
  101.     if tracks[r][c] == '<':
  102.       tracks[r][c] = '-'
  103.       carts.append(Cart(c,r,2,0))
  104.     if tracks[r][c] == 'v':
  105.       tracks[r][c] = '|'
  106.       carts.append(Cart(c,r,3,0))
  107.  
  108. cpt = -1
  109. while 1:
  110.   #   # showTracks()
  111.   if len(carts) == 1:
  112.     print('Part 2: {},{}'.format(carts[0].x, carts[0].y))
  113.     sys.exit(0)
  114.   newCarts = []
  115.   for c in carts:
  116.     if c.crashed: continue
  117.     # print(c.pretty(), end = ' --> ')
  118.     survived, cart = c.fwd()
  119.     if survived:
  120.      # print(c.pretty())
  121.       newCarts.append(cart)
  122.     else:
  123.       print("Crashed in {};{}".format(cart.x, cart.y))
  124.   carts = sorted([c for c in newCarts if not c.crashed], key=lambda cart:(cart.y, cart.x))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top