def changeRelativeBase(self): firstArgumentValue = self.getValueByParameterMode(1) conditionalPrint( f"opcodeIndex : {self.opcodeIndex}>>crb{self.readArray(self.opcodeIndex,self.opcodeIndex+2)}\t\t({self.relativeBase}>{firstArgumentValue})>>{self.relativeBase+firstArgumentValue}" ) self.relativeBase += firstArgumentValue return self.opcodeIndex + 2
def takeInput(computerProgram, opcodeIndex, firstArgumentValue): computerProgram[computerProgram[opcodeIndex + 1]] = firstArgumentValue conditionalPrint( f"opcodeIndex : {opcodeIndex}>>inp{computerProgram[opcodeIndex:opcodeIndex+2]}\t\t({firstArgumentValue})" ) # conditionalPrint(f"Input : {computerProgram[computerProgram[opcodeIndex+1]]}") return opcodeIndex + 2
def giveOutput(computerProgram, opcodeIndex): firstArgumentValue = getValueByParameterMode(computerProgram, opcodeIndex, 1) conditionalPrint( f"opcodeIndex : {opcodeIndex}>>out{computerProgram[opcodeIndex:opcodeIndex+2]}\t\t({firstArgumentValue})" ) return opcodeIndex + 2, firstArgumentValue
def lessThan(self): firstArgumentValue = self.getValueByParameterMode(1) secondArgumentValue = self.getValueByParameterMode(2) output = 1 if firstArgumentValue < secondArgumentValue else 0 outputAddress = self.setValueByParameterMode(3, output) conditionalPrint(f"opcodeIndex : {self.opcodeIndex}>>lt {self.readArray(self.opcodeIndex,self.opcodeIndex+4)}\t[{outputAddress}]{output}=({firstArgumentValue},{secondArgumentValue})") return self.opcodeIndex+4
def mul(self): firstArgumentValue = self.getValueByParameterMode(1) secondArgumentValue = self.getValueByParameterMode(2) output = firstArgumentValue*secondArgumentValue outputAddress = self.setValueByParameterMode(3, output) conditionalPrint(f"opcodeIndex : {self.opcodeIndex}>>mul{self.readArray(self.opcodeIndex,self.opcodeIndex+4)}\t[{outputAddress}]{output}=({firstArgumentValue},{secondArgumentValue})") return self.opcodeIndex+4
def doPrint(self,startPanelColor): printedPanels = {(0, 0): startPanelColor} x = 0 y = 0 direction = "^" while True: opCode = self.readMemory(self.opcodeIndex) % 100 if opCode == 99: self.isHaulted = True conditionalPrint(f"opcodeIndex : {self.opcodeIndex}>>hlt[99]") return printedPanels elif opCode == 3: panelColorCode = printedPanels.get((x, y), 0) self.opcodeIndex = self.takeInput(panelColorCode) elif opCode == 4: self.opcodeIndex, output = self.giveOutput() self.outputSequence.append(output) if len(self.outputSequence) % 2 == 1: printedPanels[(x, y)] = output else: # print(f"output>>{[(direction, x, y), printedPanels[(x, y)], output]}") if output == 0: direction, x, y = turnLeft[direction](x, y) else: direction, x, y = turnRight[direction](x, y) elif opCode in self.instructionMap.keys(): self.opcodeIndex = self.instructionMap[opCode]()
def jumpIfTrue(self): firstArgumentValue = self.getValueByParameterMode(1) secondArgumentValue = self.getValueByParameterMode(2) newOpcodeAddress = secondArgumentValue if firstArgumentValue != 0 else self.opcodeIndex + 3 conditionalPrint( f"opcodeIndex : {self.opcodeIndex}>>jnz{self.readArray(self.opcodeIndex,self.opcodeIndex+3)}\t\t[{newOpcodeAddress}]=({firstArgumentValue},{secondArgumentValue})" ) return newOpcodeAddress
def mul(computerProgram, opcodeIndex): firstArgumentValue = getValueByParameterMode(computerProgram, opcodeIndex, 1) secondArgumentValue = getValueByParameterMode(computerProgram, opcodeIndex, 2) conditionalPrint( f"opcodeIndex : {opcodeIndex}>>mul{computerProgram[opcodeIndex:opcodeIndex+4]}\t({firstArgumentValue},{secondArgumentValue})" ) computerProgram[computerProgram[ opcodeIndex + 3]] = firstArgumentValue * secondArgumentValue return opcodeIndex + 4
def jumpIfFalse(computerProgram, opcodeIndex): firstArgumentValue = getValueByParameterMode(computerProgram, opcodeIndex, 1) secondArgumentValue = getValueByParameterMode(computerProgram, opcodeIndex, 2) conditionalPrint( f"opcodeIndex : {opcodeIndex}>>jsz{computerProgram[opcodeIndex:opcodeIndex+3]}\t\t({firstArgumentValue},{secondArgumentValue})" ) if firstArgumentValue == 0: return secondArgumentValue return opcodeIndex + 3
def isEquals(computerProgram, opcodeIndex): firstArgumentValue = getValueByParameterMode(computerProgram, opcodeIndex, 1) secondArgumentValue = getValueByParameterMode(computerProgram, opcodeIndex, 2) conditionalPrint( f"opcodeIndex : {opcodeIndex}>>eql{computerProgram[opcodeIndex:opcodeIndex+4]}\t({firstArgumentValue},{secondArgumentValue})" ) computerProgram[computerProgram[ opcodeIndex + 3]] = 1 if firstArgumentValue == secondArgumentValue else 0 return opcodeIndex + 4
def runIntComputerForOutput(self): while True: opCode = self.readMemory(self.opcodeIndex) % 100 if opCode == 99: self.isHaulted = True conditionalPrint(f"opcodeIndex : {self.opcodeIndex}>>hlt[99]") return "HLT", self.outputSequence elif opCode == 3: if len(self.inputSequence) > self.nextInputIndex: self.opcodeIndex = self.takeInput( self.inputSequence[self.nextInputIndex]) self.nextInputIndex += 1 else: return "INP" elif opCode == 4: self.opcodeIndex, output = self.giveOutput() self.outputSequence.append(output) elif opCode in self.instructionMap.keys(): self.opcodeIndex = self.instructionMap[opCode]()
def doPrint(self): printedPanels = {i: [] for i in range(5)} while True: opCode = self.readMemory(self.opcodeIndex) % 100 if opCode == 99: self.isHaulted = True conditionalPrint(f"opcodeIndex : {self.opcodeIndex}>>hlt[99]") return printedPanels elif opCode == 3: panelColorCode = 0 self.opcodeIndex = self.takeInput(panelColorCode) elif opCode == 4: self.opcodeIndex, output = self.giveOutput() self.outputSequence.append(output) if len(self.outputSequence) == 3: printedPanels[output].append((self.outputSequence[0], self.outputSequence[1])) self.outputSequence.clear() elif opCode in self.instructionMap.keys(): self.opcodeIndex = self.instructionMap[opCode]()
def giveOutput(self): firstArgumentValue = self.getValueByParameterMode(1) conditionalPrint( f"opcodeIndex : {self.opcodeIndex}>>out{self.readArray(self.opcodeIndex,self.opcodeIndex+2)}\t\t\t\t>>({firstArgumentValue})" ) return self.opcodeIndex + 2, firstArgumentValue
def takeInput(self, inputValue): self.setValueByParameterMode(1, inputValue) conditionalPrint( f"opcodeIndex : {self.opcodeIndex}>>inp{self.readArray(self.opcodeIndex,self.opcodeIndex+2)}\t\t\t\t>>{inputValue}" ) return self.opcodeIndex + 2