def runFeedbackLoop(cmdinput, ampinputs, startValue): #create the amplifiers. ampList = [] ampList.append(Intcode.Intcode(cmdinput, name="Amplifier A")) ampList.append(Intcode.Intcode(cmdinput, name="Amplifier B")) ampList.append(Intcode.Intcode(cmdinput, name="Amplifier C")) ampList.append(Intcode.Intcode(cmdinput, name="Amplifier D")) ampList.append(Intcode.Intcode(cmdinput, name="Amplifier E")) #start the amplifiers with the ampinputs. The first input of the Intcode #is the ampinput. for i, j in enumerate(ampList): j.setInputs([ampinputs[i]]) j.run() i = 0 end = 0 #This is the feedback loop. getFeedback will apply each amplifiers outputs #to the next amplifier, A->B->C->D->E->A. When getFeedback returns '99' #(opcode for the program finishing), then the feedback loop is done. #return the startValue which at the end will be the output of Amplifier E. while end != 99: ampList, end = getFeedback(cmdinput, ampList, startValue) startValue = ampList[-1].outputs[-1] #failsafe for infitite loop i += 1 if i > 100: break return startValue
def Intcode_mp( progfunc, instanceId, inputQueue, outputQueue, haltQueue ): prog = progfunc() program = Intcode( prog, lambda : readInputQueue( instanceId, inputQueue ), lambda x: writeOutputQueue( instanceId, outputQueue, x ) ) try: program.Run() writeOutputQueue( instanceId+5, haltQueue, prog[0] ) except: print( program ) writeOutputQueue( instanceId+5, haltQueue, -1 )
def test_Day5Part2Example1(self): intcode = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8] computerTrue = Intcode.computer(intcode) userInput8 = 8 with patch('builtins.input', lambda *args: userInput8): computerTrue.run() self.assertEqual(1, computerTrue.outputs[0]) computerFalse = Intcode.computer(intcode) userInput5 = 5 with patch('builtins.input', lambda *args: userInput5): computerFalse.run() self.assertEqual(0, computerFalse.outputs[0])
def test_Day5Part2Example6(self): intcode = [3, 3, 1105, -1, 9, 1101, 0, 0, 12, 4, 12, 99, 1] computerInputZero = Intcode.computer(intcode) userInputZero = 0 with patch('builtins.input', lambda *args: userInputZero): computerInputZero.run() self.assertEqual(0, computerInputZero.outputs[0]) computerInputNonZero = Intcode.computer(intcode) userInputNonZero = 1 with patch('builtins.input', lambda *args: userInputNonZero): computerInputNonZero.run() self.assertEqual(1, computerInputNonZero.outputs[0])
def test_Day5Part2Example4(self): intcode = [3, 3, 1107, -1, 8, 3, 4, 3, 99] computerEqual = Intcode.computer(intcode) userInputEqual = 8 with patch('builtins.input', lambda *args: userInputEqual): computerEqual.run() self.assertEqual(0, computerEqual.outputs[0]) computerLess = Intcode.computer(intcode) userInputLess = 5 with patch('builtins.input', lambda *args: userInputLess): computerLess.run() self.assertEqual(1, computerLess.outputs[0])
def test_opcodeUnknown12345(self): intcode = [11145] # with self.assertRaises(NotImplementedError): # Intcode.computer(intcode).takeStep() message = Intcode.computer(intcode).takeStep() self.assertEqual( 'Uknown opcode encountered, 45 at position 0, exiting', message)
def test_example304099(self): intcode = [3, 0, 4, 0, 99] computer = Intcode.computer(intcode) inputValue = 123 computer.inputs = [inputValue] computer.run() self.assertEqual(inputValue, computer.outputs[0])
def test_code4output(self): intcode = [4, 0, 99] with patch('builtins.print') as p: computer = Intcode.computer(intcode) computer.run() p.assert_called_with(4) self.assertEqual(4, computer.outputs[0])
def test2(): state = {} state['output'] = list() code = [109,1,204,-1,1001,100,1,100,1008,100,16,101,1006,101,0,99] codeCopy = code.copy() output = Intcode.Intcode(codeCopy, outputFunc=lambda x : addToList(state, 'output', x) ).Run() print( state ) assert( state['output'] == code and output[0] == code[0] ) state['expected'] = 34915192 * 34915192 output = Intcode.Intcode([1102,34915192,34915192,7,4,7,99,0], outputFunc=lambda x : storeOutput( state, 'output', x ) ).Run() assert( state['expected'] == state['output'] ) code = [104,1125899906842624,99] output = Intcode.Intcode(code, outputFunc=lambda x : storeOutput( state, 'output', x ) ).Run() assert( state['output'] == code[1] )
def test_immediateModeOpcode4ParameterA(self): intcode = [104, 5, 99, 3, 4, 5] with patch('builtins.print') as p: computer = Intcode.computer(intcode) computer.run() p.assert_called_with(5) self.assertEqual(5, computer.outputs[0])
def test_opcode6ImediateModeJump(self): intcode = [1106, 0, 7, 4, 0, 4, 0, 99] computer = Intcode.computer(intcode) computer.run() self.assertEqual(len(computer.log), 2) self.assertEqual(computer.log[0], f'opcode 6 at position 0 processed') self.assertEqual(computer.log[1], f'opcode 99, exiting')
def part2(cmdinput): boost = Intcode.Intcode(cmdinput, debug=True) boost.setInputs(2) boost.run()
def test_opcode6OnZeroJump(self): intcode = [6, 6, 3, 7, 4, 4, 0, 99] computer = Intcode.computer(intcode) computer.run() self.assertEqual(len(computer.log), 2) self.assertEqual(computer.log[0], f'opcode 6 at position 0 processed') self.assertEqual(computer.log[1], f'opcode 99, exiting')
def test_opcode5OnNotZeroJump(self): intcode = [5, 7, 1, 1, 4, 4, 6, 99] computer = Intcode.computer(intcode) computer.run() self.assertEqual(len(computer.log), 2) self.assertEqual(computer.log[0], f'opcode 5 at position 0 processed') self.assertEqual(computer.log[1], f'opcode 99, exiting')
def test_runningIntcodeComputerExampleDay2(self): intcode = [1, 9, 10, 3, 2, 3, 11, 0, 99, 30, 40, 50] computer = Intcode.computer(intcode) computer.running = True while computer.running: computer.takeStep() self.assertEqual(70, computer.intcode[3]) self.assertEqual(3500, computer.intcode[0])
def test_opcode6OnNotZeroDoNothing(self): intcode = [6, 6, 7, 1, 4, 4, 1, 99] computer = Intcode.computer(intcode) computer.run() self.assertEqual(len(computer.log), 3) self.assertEqual(computer.log[0], f'opcode 6 at position 0 processed') self.assertEqual(computer.log[1], f'opcode 1 at position 3 processed') self.assertEqual(computer.log[2], f'opcode 99, exiting')
def __init__(self): self.cur_pos = np.array([0,0]) #Current position of the droid (2D coordinate) self.game = Intcode(input_code) #north (1), south (2), west (3), and east (4) self.in_to_dirs = {1:tuple([1,0]), 2:tuple([-1,0]), 3:tuple([0,-1]), 4:tuple([0,1])} self.dirs_to_in = {tuple(v): int(k) for k, v in self.in_to_dirs.items()} #Reverse dictionary self.unexplored = [] #Buffer list for unexplored tiles adjacent to original position self.anti_clockwise = {1:3, 3:2, 2:4, 4:1} self.clockwise = {1:4, 4:2, 2:3, 3:1} self.reverse_dir = {1:2, 2:1, 3:4, 4:3} #0 = wall, 1=empty, 2=oxygen self.panels = {tuple(self.cur_pos): 0} #Panels of which we know the identity self.grid_num = 0 #counter for plots
def test_immediateModeOpcode2ValueErrorParamater3(self): intcode = [10002, 0, 0, 99] # https://stackoverflow.com/questions/129507/how-do-you-test-that-a-python-function-throws-an-exception with self.assertRaises(ValueError) as e: computer = Intcode.computer(intcode) computer.run() self.assertEqual( 'Parameters that an instruction writes to will never be in immediate mode.', str(e.exception))
def test_opcode2with2003(self): intcode = [2, 0, 0, 3] computer = Intcode.computer(intcode) message = computer.takeStep() self.assertEqual('opcode 2 at position 0 processed', message) self.assertEqual(2, computer.intcode[0]) self.assertEqual(0, computer.intcode[1]) self.assertEqual(0, computer.intcode[2]) self.assertEqual(4, computer.intcode[3])
def test_opcode1with1111(self): intcode = [1, 1, 1, 1] computer = Intcode.computer(intcode) message = computer.takeStep() self.assertEqual('opcode 1 at position 0 processed', message) self.assertEqual(1, computer.intcode[0]) self.assertEqual(2, computer.intcode[1]) self.assertEqual(1, computer.intcode[2]) self.assertEqual(1, computer.intcode[3])
def test_ensureModesAre1or0(self): with self.assertRaises(ValueError) as e: intcode = [20001, 99] computer = Intcode.computer(intcode) computer.run() self.assertEqual(f'Parameter A should be 1 or 0, not 2', str(e.exception)) with self.assertRaises(ValueError) as e: intcode = [3001, 99] computer = Intcode.computer(intcode) computer.run() self.assertEqual(f'Parameter B should be 1 or 0, not 3', str(e.exception)) with self.assertRaises(ValueError) as e: intcode = [401, 99] computer = Intcode.computer(intcode) computer.run() self.assertEqual(f'Parameter C should be 1 or 0, not 4', str(e.exception))
def test_opcode6ImediateModeDontJump(self): intcode = [1106, 1, 7, 3, 0, 3, 0, 99] computer = Intcode.computer(intcode) computer.inputs = [1, 5] computer.run() self.assertEqual(len(computer.log), 4) self.assertEqual(computer.log[0], f'opcode 6 at position 0 processed') self.assertEqual(computer.log[1], f'opcode 3 at position 3 processed') self.assertEqual(computer.log[2], f'opcode 3 at position 5 processed') self.assertEqual(computer.log[3], f'opcode 99, exiting')
def Intcode_mp(codefunc, instanceId, pipe): code = codefunc() try: ioHandler = MultiProcessIO(pipe, instanceId) prog = Intcode.Intcode(code, inputFunc=ioHandler.readInput, outputFunc=ioHandler.writeOutput) prog.Run() except: print('Failed to run Intcode for instance: {}'.format(instanceId)) raise
def test_copde3inputs5and3(self): intcode = [3, 0, 3, 1, 99] inputs = [5, 3] computer = Intcode.computer(intcode) computer.inputs = inputs computer.run() self.assertEqual(5, computer.intcode[0]) self.assertEqual(3, computer.intcode[1]) self.assertEqual(3, computer.intcode[2]) self.assertEqual(1, computer.intcode[3]) self.assertEqual(99, computer.intcode[4])
def test_immediateModeOpcode3ValueErrorParameterA(self): intcode = [103, 1, 99] # https://stackoverflow.com/questions/129507/how-do-you-test-that-a-python-function-throws-an-exception with self.assertRaises(ValueError) as e: userInput = 4 with patch('builtins.input', lambda *args: userInput): computer = Intcode.computer(intcode) computer.run() self.assertEqual( 'Parameters that an instruction writes to will never be in immediate mode.', str(e.exception))
def test_opcode3(self): # https://dev.to/vergeev/how-to-test-input-processing-in-python-3 # https://stackoverflow.com/questions/21046717/python-mocking-raw-input-in-unittests # https://stackoverflow.com/questions/18161330/using-unittest-mock-to-patch-input-in-python-3 intcode = [3, 1, 99] userInput = 5 with patch('builtins.input', lambda *args: userInput): computer = Intcode.computer(intcode) computer.run() self.assertEqual(3, computer.intcode[0]) self.assertEqual(userInput, computer.intcode[1]) self.assertEqual(99, computer.intcode[2])
def test_Day5Part2Example7(self): intcode = [ 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31, 1106, 0, 36, 98, 0, 0, 1002, 21, 125, 20, 4, 20, 1105, 1, 46, 104, 999, 1105, 1, 46, 1101, 1000, 1, 20, 4, 20, 1105, 1, 46, 98, 99 ] computerEqual = Intcode.computer(intcode) userInputEqual = 8 with patch('builtins.input', lambda *args: userInputEqual): computerEqual.run() self.assertEqual(1000, computerEqual.outputs[0]) computerLess = Intcode.computer(intcode) userInputLess = 5 with patch('builtins.input', lambda *args: userInputLess): computerLess.run() self.assertEqual(999, computerLess.outputs[0]) computerGreater = Intcode.computer(intcode) userInputGreater = 11 with patch('builtins.input', lambda *args: userInputGreater): computerGreater.run() self.assertEqual(1001, computerGreater.outputs[0])
def test_runningIntcodeComputer1and99(self): intcode = [1, 1, 1, 1, 99] computer = Intcode.computer(intcode) message0 = computer.takeStep() message1 = computer.takeStep() self.assertEqual('opcode 1 at position 0 processed', message0) self.assertEqual('opcode 99, exiting', message1) self.assertEqual(1, computer.intcode[0]) self.assertEqual(2, computer.intcode[1]) self.assertEqual(1, computer.intcode[2]) self.assertEqual(1, computer.intcode[3]) self.assertEqual(99, computer.intcode[4])
def __init__(self, start_c): self.dir = np.array( (1, 0)) #Left: (0,-1), Down: (-1,0), Right: (0,1), Up: (1,0) self.panels = {} self.panels[(0, 0)] = start_c #Start on panel that is black or white self.cur_pos = np.array((0, 0)) self.brain = Intcode(input_code) self.anti_clockwise = { (-1, 0): (0, -1), (0, -1): (1, 0), (1, 0): (0, 1), (0, 1): (-1, 0) } self.clockwise = { (-1, 0): (0, 1), (0, 1): (1, 0), (1, 0): (0, -1), (0, -1): (-1, 0) }
def __init__(self, prog): self.state = {} self.pos = (0, 0) self.dir = 'U' self.panels = {} self.outputs = [self.paintLoc, self.turn] self.outputcount = 0 self.io = [] self.prog = prog self.comp = Intcode.Intcode(self.prog, inputFunc=self.readCurrentPanel, outputFunc=self.handleOutput)