Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def getPositionValue(location):
- return int(program[int(program[location])])
- def getImmediateValue(location):
- return int(program[location])
- def add(instructionPointer, parameterMode):
- parameterMode = parameterMode.zfill(3)
- operand1 = getValue[parameterMode[2]](instructionPointer + 1)
- operand2 = getValue[parameterMode[1]](instructionPointer + 2)
- resultLocation = getImmediateValue(instructionPointer + 3)
- result = operand1 + operand2
- program[resultLocation] = result
- return instructionPointer + 4
- def multiply(instructionPointer, parameterMode):
- parameterMode = parameterMode.zfill(3)
- operand1 = getValue[parameterMode[2]](instructionPointer + 1)
- operand2 = getValue[parameterMode[1]](instructionPointer + 2)
- resultLocation = getImmediateValue(instructionPointer + 3)
- result = operand1 * operand2
- program[resultLocation] = result
- return instructionPointer + 4
- def getInput(instructionPointer, parameterMode):
- parameterMode = parameterMode.zfill(1)
- location = getImmediateValue(instructionPointer + 1)
- program[location] = inputValue
- return instructionPointer + 2
- def showOutput(instructionPointer, parameterMode):
- parameterMode = parameterMode.zfill(1)
- location = getImmediateValue(instructionPointer + 1)
- outputValue = program[location]
- print(f'Output: {outputValue}')
- return instructionPointer + 2
- def jumpIfTrue(instructionPointer, parameterMode):
- parameterMode = parameterMode.zfill(2)
- operand = getValue[parameterMode[1]](instructionPointer + 1)
- if operand != 0:
- return getValue[parameterMode[0]](instructionPointer + 2)
- return instructionPointer + 3
- def jumpIfFalse(instructionPointer, parameterMode):
- parameterMode = parameterMode.zfill(2)
- operand = getValue[parameterMode[1]](instructionPointer + 1)
- if operand == 0:
- return getValue[parameterMode[0]](instructionPointer + 2)
- return instructionPointer + 3
- def lessThan(instructionPointer, parameterMode):
- parameterMode = parameterMode.zfill(3)
- operand1 = getValue[parameterMode[2]](instructionPointer + 1)
- operand2 = getValue[parameterMode[1]](instructionPointer + 2)
- location = getImmediateValue(instructionPointer + 3)
- if operand1 < operand2:
- program[location] = 1
- else:
- program[location] = 0
- return instructionPointer + 4
- def equals(instructionPointer, parameterMode):
- parameterMode = parameterMode.zfill(3)
- operand1 = getValue[parameterMode[2]](instructionPointer + 1)
- operand2 = getValue[parameterMode[1]](instructionPointer + 2)
- location = getImmediateValue(instructionPointer + 3)
- if operand1 == operand2:
- program[location] = 1
- else:
- program[location] = 0
- return instructionPointer + 4
- def runProgram(program):
- instructionPointer = 0
- opcodeString = program[instructionPointer]
- opcode = int(opcodeString[-2:])
- parameterMode = opcodeString[0:-2]
- while opcode != 99:
- try:
- operation = ops[opcode]
- except KeyError:
- raise Exception("Unknown Opcode")
- instructionPointer = operation(instructionPointer, parameterMode)
- opcodeString = str(program[instructionPointer])
- opcode = int(opcodeString[-2:])
- parameterMode = opcodeString[0:-2]
- return getImmediateValue(0)
- with open('5.txt', 'r') as fp:
- line = fp.readline()
- #line = "3,9,8,9,10,9,4,9,99,-1,8"
- #line = "3,9,7,9,10,9,4,9,99,-1,8"
- #line = "3,3,1108,-1,8,3,4,3,99"
- #line = "3,3,1107,-1,8,3,4,3,99"
- #line = "3,12,6,12,15,1,13,14,13,4,13,99,-1,0,1,9"
- #line = "3,3,1105,-1,9,1101,0,0,12,4,12,99,1"
- getValue = {
- '0' : getPositionValue,
- '1' : getImmediateValue
- }
- ops = {
- 1 : add,
- 2 : multiply,
- 3 : getInput,
- 4 : showOutput,
- 5 : jumpIfTrue,
- 6 : jumpIfFalse,
- 7 : lessThan,
- 8 : equals
- }
- program = line.split(',')
- inputValue = 5
- runProgram(program)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement