Advertisement
Guest User

day11p1

a guest
Dec 13th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.23 KB | None | 0 0
  1. base = 0
  2. def getValue(pDict,pKey):
  3.     if pKey in pDict.keys():
  4.         return pDict[pKey]
  5.     else:
  6.         return 0
  7. def getParameters(inst):
  8.     pS = [0,0,0]
  9.     s = len(inst) -3
  10.     z = 0
  11.     while s >= 0:
  12.         pS[z] = int(inst[s])
  13.         s -= 1
  14.         z += 1
  15.     return pS
  16. def getV(pDict, para,i):
  17.     v = getValue(pDict,i)
  18.     if para == 1:
  19.         return v
  20.     else:
  21.         if para == 2:
  22.             v += base
  23.         return getValue(pDict,v)
  24. def assign(pDict, para, i):
  25.     if(para == 1):
  26.         return i
  27.     else:
  28.         v = getValue(pDict,i)
  29.         if(para == 2):
  30.             v += base
  31.         return v
  32. def addToLoc(dLoc, cur):
  33.     if cur in dLoc.keys():
  34.         dLoc[cur] += 1
  35.     else:
  36.         dLoc[cur] = 1
  37.     return dLoc
  38. def findFace(face,direction):
  39.     if face == 'U':
  40.         if direction == 0:
  41.             return 'L'
  42.         else:
  43.             return 'R'
  44.     elif face == 'D':
  45.         if direction == 0:
  46.             return 'R'
  47.         else:
  48.             return 'L'
  49.     elif face == 'L':
  50.         if direction == 0:
  51.             return 'D'
  52.         else:
  53.             return 'U'
  54.     else:
  55.         if direction == 0:
  56.             return 'U'
  57.         else:
  58.             return 'D'
  59.    
  60. def locationFinder(currentLoc, face,direction):
  61.     if face == 'U':
  62.         if direction == 0:
  63.             currentLoc[0] -= 1
  64.             return currentLoc
  65.         else:
  66.             currentLoc[0] += 1
  67.             return currentLoc
  68.     elif face == 'D':
  69.         if direction == 0:
  70.             currentLoc[0] += 1
  71.             return currentLoc
  72.         else:
  73.             currentLoc[0] -= 1
  74.             return currentLoc
  75.     elif face == 'L':
  76.         if direction == 0:
  77.             currentLoc[1] -= 1
  78.             return currentLoc
  79.         else:
  80.             currentLoc[1] += 1
  81.             return currentLoc
  82.     elif face == 'R':
  83.         if direction == 0:
  84.             currentLoc[1] += 1
  85.             return currentLoc
  86.         else:
  87.             currentLoc[1] -= 1
  88.             return currentLoc
  89.  
  90. posDict = {}
  91. inp = '3,8,1005,8,298,1106,0,11,0,0,0,104,1,104,0,3,8,1002,8,-1,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,28,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,1,10,4,10,1002,8,1,51,1006,0,37,1006,0,65,1,4,9,10,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,0,10,4,10,102,1,8,83,2,3,9,10,1006,0,39,1,1,0,10,1,104,11,10,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1002,8,1,120,2,104,13,10,1,1007,18,10,1006,0,19,1,107,2,10,3,8,102,-1,8,10,101,1,10,10,4,10,1008,8,0,10,4,10,1001,8,0,157,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,0,10,4,10,1001,8,0,179,2,108,16,10,2,1108,14,10,1006,0,70,3,8,102,-1,8,10,1001,10,1,10,4,10,108,1,8,10,4,10,101,0,8,211,3,8,1002,8,-1,10,1001,10,1,10,4,10,1008,8,1,10,4,10,101,0,8,234,3,8,102,-1,8,10,1001,10,1,10,4,10,1008,8,0,10,4,10,102,1,8,256,3,8,1002,8,-1,10,101,1,10,10,4,10,1008,8,1,10,4,10,1002,8,1,278,101,1,9,9,1007,9,957,10,1005,10,15,99,109,620,104,0,104,1,21101,387508441896,0,1,21101,0,315,0,1105,1,419,21101,666412880532,0,1,21102,1,326,0,1106,0,419,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,3,10,104,0,104,1,3,10,104,0,104,0,3,10,104,0,104,1,21101,106341436456,0,1,21101,373,0,0,1106,0,419,21101,46211886299,0,1,21101,384,0,0,1106,0,419,3,10,104,0,104,0,3,10,104,0,104,0,21101,0,838433923860,1,21102,1,407,0,1105,1,419,21102,1,988224946540,1,21102,1,418,0,1106,0,419,99,109,2,21201,-1,0,1,21101,40,0,2,21102,1,450,3,21101,440,0,0,1105,1,483,109,-2,2106,0,0,0,1,0,0,1,109,2,3,10,204,-1,1001,445,446,461,4,0,1001,445,1,445,108,4,445,10,1006,10,477,1101,0,0,445,109,-2,2105,1,0,0,109,4,1201,-1,0,482,1207,-3,0,10,1006,10,500,21101,0,0,-3,21201,-3,0,1,21202,-2,1,2,21101,1,0,3,21102,1,519,0,1105,1,524,109,-4,2106,0,0,109,5,1207,-3,1,10,1006,10,547,2207,-4,-2,10,1006,10,547,22102,1,-4,-4,1106,0,615,21202,-4,1,1,21201,-3,-1,2,21202,-2,2,3,21102,1,566,0,1105,1,524,21201,1,0,-4,21101,0,1,-1,2207,-4,-2,10,1006,10,585,21101,0,0,-1,22202,-2,-1,-2,2107,0,-3,10,1006,10,607,22101,0,-1,1,21102,1,607,0,105,1,482,21202,-2,-1,-2,22201,-4,-2,-4,109,-5,2105,1,0'
  92. rlist = inp.split(',')
  93. i = 0
  94. rLength = len(rlist)
  95.  
  96. while i < rLength:
  97.     posDict[i] = int(rlist[i])
  98.     i += 1
  99.  
  100. x = 0
  101. dLength = len(posDict)
  102. newInput = 0
  103. locations = {}
  104. paint = True
  105. facing = 'U'
  106. currentLocation = [0,0]
  107. locations[str(currentLocation)] = 1
  108. while True:
  109.     instruction = posDict[x]
  110.     op = 0
  111.     pList = [0,0,0]
  112.     if instruction < 100:
  113.         op = instruction
  114.     else:
  115.         fullInst = str(instruction)
  116.         op = int(fullInst[-2] + fullInst[-1])
  117.         pList = getParameters(fullInst)
  118.     if op == 1 or op == 2 or op == 7 or op == 8:
  119.  
  120.         v1 = getV(posDict, pList[0],x+1)
  121.         v2 = getV(posDict, pList[1],x+2)
  122.         v3 = assign(posDict,pList[2],x+3)
  123.  
  124.         if op == 1:
  125.             posDict[v3] = v1 + v2
  126.         elif op == 2:
  127.             posDict[v3] = v1 * v2
  128.         elif op == 7:
  129.             if v1 < v2:
  130.                 posDict[v3] = 1
  131.             else:
  132.                 posDict[v3] = 0
  133.         elif op == 8:
  134.             if v1 == v2:
  135.                 posDict[v3] = 1
  136.             else:
  137.                 posDict[v3] = 0
  138.         x += 4
  139.     elif op == 3 or op == 4 or op == 9:
  140.         if op == 3:
  141.             v1 = assign(posDict, pList[0],x+1)
  142.             posDict[v1] = newInput
  143.         elif op == 4:
  144.             v1 = assign(posDict, pList[0],x+1)
  145.             if paint:
  146.                 newInput = posDict[v1]
  147.                 paint = False
  148.             else:
  149.                 direction = posDict[v1]
  150.                 currentLocation = locationFinder(currentLocation, facing, direction)
  151.                 facing = findFace(facing, direction)
  152.                 paint = True
  153.                 addToLoc(locations,str(currentLocation))
  154.         elif op == 9:
  155.             v1 = assign(posDict, pList[0],x+1)
  156.             base += posDict[v1]
  157.         x += 2
  158.     elif op == 5 or op == 6:
  159.         v1 = getV(posDict, pList[0],x+1)
  160.         v2 = assign(posDict, pList[1],x+2)
  161.         if(op == 5):
  162.             if v1 != 0:
  163.                 x = posDict[v2]
  164.             else:
  165.                 x+=3
  166.         elif(op == 6):
  167.             if v1 == 0:
  168.                 x = posDict[v2]
  169.             else:
  170.                 x+=3
  171.     elif op == 99:
  172.         break
  173.         x = dLength
  174. print(len(locations))
  175. print(locations)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement