Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- base = 0
- def getValue(pDict,pKey):
- if pKey in pDict.keys():
- return pDict[pKey]
- else:
- return 0
- def getParameters(inst):
- pS = [0,0,0]
- s = len(inst) -3
- z = 0
- while s >= 0:
- pS[z] = int(inst[s])
- s -= 1
- z += 1
- return pS
- def getV(pDict, para,i):
- v = getValue(pDict,i)
- if para == 1:
- return v
- else:
- if para == 2:
- v += base
- return getValue(pDict,v)
- def assign(pDict, para, i):
- if(para == 1):
- return i
- else:
- v = getValue(pDict,i)
- if(para == 2):
- v += base
- return v
- def addToLoc(dLoc, cur):
- if cur in dLoc.keys():
- dLoc[cur] += 1
- else:
- dLoc[cur] = 1
- return dLoc
- def findFace(face,direction):
- if face == 'U':
- if direction == 0:
- return 'L'
- else:
- return 'R'
- elif face == 'D':
- if direction == 0:
- return 'R'
- else:
- return 'L'
- elif face == 'L':
- if direction == 0:
- return 'D'
- else:
- return 'U'
- else:
- if direction == 0:
- return 'U'
- else:
- return 'D'
- def locationFinder(currentLoc, face,direction):
- if face == 'U':
- if direction == 0:
- currentLoc[0] -= 1
- return currentLoc
- else:
- currentLoc[0] += 1
- return currentLoc
- elif face == 'D':
- if direction == 0:
- currentLoc[0] += 1
- return currentLoc
- else:
- currentLoc[0] -= 1
- return currentLoc
- elif face == 'L':
- if direction == 0:
- currentLoc[1] -= 1
- return currentLoc
- else:
- currentLoc[1] += 1
- return currentLoc
- elif face == 'R':
- if direction == 0:
- currentLoc[1] += 1
- return currentLoc
- else:
- currentLoc[1] -= 1
- return currentLoc
- posDict = {}
- 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'
- rlist = inp.split(',')
- i = 0
- rLength = len(rlist)
- while i < rLength:
- posDict[i] = int(rlist[i])
- i += 1
- x = 0
- dLength = len(posDict)
- newInput = 0
- locations = {}
- paint = True
- facing = 'U'
- currentLocation = [0,0]
- locations[str(currentLocation)] = 1
- while True:
- instruction = posDict[x]
- op = 0
- pList = [0,0,0]
- if instruction < 100:
- op = instruction
- else:
- fullInst = str(instruction)
- op = int(fullInst[-2] + fullInst[-1])
- pList = getParameters(fullInst)
- if op == 1 or op == 2 or op == 7 or op == 8:
- v1 = getV(posDict, pList[0],x+1)
- v2 = getV(posDict, pList[1],x+2)
- v3 = assign(posDict,pList[2],x+3)
- if op == 1:
- posDict[v3] = v1 + v2
- elif op == 2:
- posDict[v3] = v1 * v2
- elif op == 7:
- if v1 < v2:
- posDict[v3] = 1
- else:
- posDict[v3] = 0
- elif op == 8:
- if v1 == v2:
- posDict[v3] = 1
- else:
- posDict[v3] = 0
- x += 4
- elif op == 3 or op == 4 or op == 9:
- if op == 3:
- v1 = assign(posDict, pList[0],x+1)
- posDict[v1] = newInput
- elif op == 4:
- v1 = assign(posDict, pList[0],x+1)
- if paint:
- newInput = posDict[v1]
- paint = False
- else:
- direction = posDict[v1]
- currentLocation = locationFinder(currentLocation, facing, direction)
- facing = findFace(facing, direction)
- paint = True
- addToLoc(locations,str(currentLocation))
- elif op == 9:
- v1 = assign(posDict, pList[0],x+1)
- base += posDict[v1]
- x += 2
- elif op == 5 or op == 6:
- v1 = getV(posDict, pList[0],x+1)
- v2 = assign(posDict, pList[1],x+2)
- if(op == 5):
- if v1 != 0:
- x = posDict[v2]
- else:
- x+=3
- elif(op == 6):
- if v1 == 0:
- x = posDict[v2]
- else:
- x+=3
- elif op == 99:
- break
- x = dLength
- print(len(locations))
- print(locations)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement