Advertisement
Guest User

Advent Of Code Day 5b

a guest
Dec 5th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.99 KB | None | 0 0
  1. def getPositionValue(location):
  2.     return int(program[int(program[location])])
  3.  
  4. def getImmediateValue(location):
  5.     return int(program[location])
  6.  
  7. def add(instructionPointer, parameterMode):
  8.     parameterMode = parameterMode.zfill(3)
  9.     operand1 = getValue[parameterMode[2]](instructionPointer + 1)
  10.     operand2 = getValue[parameterMode[1]](instructionPointer + 2)
  11.  
  12.     resultLocation = getImmediateValue(instructionPointer + 3)
  13.     result = operand1 + operand2
  14.     program[resultLocation] = result
  15.     return instructionPointer + 4
  16.  
  17. def multiply(instructionPointer, parameterMode):
  18.     parameterMode = parameterMode.zfill(3)
  19.     operand1 = getValue[parameterMode[2]](instructionPointer + 1)
  20.     operand2 = getValue[parameterMode[1]](instructionPointer + 2)
  21.     resultLocation = getImmediateValue(instructionPointer + 3)
  22.  
  23.     result = operand1 * operand2
  24.     program[resultLocation] = result
  25.  
  26.     return instructionPointer + 4
  27.  
  28. def getInput(instructionPointer, parameterMode):
  29.     parameterMode = parameterMode.zfill(1)
  30.     location = getImmediateValue(instructionPointer + 1)
  31.     program[location] = inputValue
  32.  
  33.     return instructionPointer + 2
  34.  
  35. def showOutput(instructionPointer, parameterMode):
  36.     parameterMode = parameterMode.zfill(1)
  37.     location = getImmediateValue(instructionPointer + 1)
  38.     outputValue = program[location]
  39.  
  40.     print(f'Output: {outputValue}')
  41.     return instructionPointer + 2
  42.  
  43. def jumpIfTrue(instructionPointer, parameterMode):
  44.     parameterMode = parameterMode.zfill(2)
  45.     operand = getValue[parameterMode[1]](instructionPointer + 1)
  46.     if operand != 0:
  47.         return getValue[parameterMode[0]](instructionPointer + 2)
  48.  
  49.     return instructionPointer + 3
  50.  
  51. def jumpIfFalse(instructionPointer, parameterMode):
  52.     parameterMode = parameterMode.zfill(2)
  53.     operand = getValue[parameterMode[1]](instructionPointer + 1)
  54.     if operand == 0:
  55.         return getValue[parameterMode[0]](instructionPointer + 2)
  56.  
  57.     return instructionPointer + 3
  58.  
  59. def lessThan(instructionPointer, parameterMode):
  60.     parameterMode = parameterMode.zfill(3)
  61.     operand1 = getValue[parameterMode[2]](instructionPointer + 1)
  62.     operand2 = getValue[parameterMode[1]](instructionPointer + 2)
  63.     location = getImmediateValue(instructionPointer + 3)
  64.     if operand1 < operand2:
  65.         program[location] = 1
  66.     else:
  67.         program[location] = 0
  68.  
  69.     return instructionPointer + 4
  70.  
  71. def equals(instructionPointer, parameterMode):
  72.     parameterMode = parameterMode.zfill(3)
  73.     operand1 = getValue[parameterMode[2]](instructionPointer + 1)
  74.     operand2 = getValue[parameterMode[1]](instructionPointer + 2)
  75.     location = getImmediateValue(instructionPointer + 3)
  76.  
  77.     if operand1 == operand2:
  78.         program[location] = 1
  79.     else:
  80.         program[location] = 0
  81.  
  82.     return instructionPointer + 4
  83.  
  84. def runProgram(program):
  85.     instructionPointer = 0
  86.     opcodeString = program[instructionPointer]
  87.     opcode = int(opcodeString[-2:])
  88.     parameterMode = opcodeString[0:-2]
  89.  
  90.     while opcode != 99:
  91.         try:
  92.             operation = ops[opcode]
  93.         except KeyError:
  94.              raise Exception("Unknown Opcode")
  95.  
  96.         instructionPointer = operation(instructionPointer, parameterMode)
  97.  
  98.         opcodeString = str(program[instructionPointer])
  99.         opcode = int(opcodeString[-2:])
  100.         parameterMode = opcodeString[0:-2]
  101.  
  102.     return getImmediateValue(0)
  103.  
  104. with open('5.txt', 'r') as fp:
  105.     line = fp.readline()
  106. #line = "3,9,8,9,10,9,4,9,99,-1,8"
  107. #line = "3,9,7,9,10,9,4,9,99,-1,8"
  108. #line = "3,3,1108,-1,8,3,4,3,99"
  109. #line = "3,3,1107,-1,8,3,4,3,99"
  110. #line = "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9"
  111. #line = "3,3,1105,-1,9,1101,0,0,12,4,12,99,1"
  112.  
  113. getValue = {
  114.     '0' : getPositionValue,
  115.     '1' : getImmediateValue
  116. }
  117.  
  118. ops = {
  119.     1 : add,
  120.     2 : multiply,
  121.     3 : getInput,
  122.     4 : showOutput,
  123.     5 : jumpIfTrue,
  124.     6 : jumpIfFalse,
  125.     7 : lessThan,
  126.     8 : equals
  127. }
  128.  
  129. program = line.split(',')
  130.  
  131. inputValue = 5
  132. runProgram(program)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement