def main(): with open('input1.txt', 'r') as file: program = file.read().strip() comp = IntCode.IntCode(program) comp.push(1) comp.run() print(f"Part 1 solution is: {comp.output.pop()}") comp2 = IntCode.IntCode(program) comp2.push(5) comp2.run() print(f"Part 2 solution is: {comp2.pop()}")
def arcade(code, play_free=False): codes = [int(x) for x in code.split(',')] computer = IntCode(codes) tiles = dict() outputs = [] score = 0 if play_free: computer.edit(0, 2) while True: res = computer.run() if res == R_HALT: break elif res == R_OUTPUT: outputs.append(computer.read()) if len(outputs) == 3: x = outputs[0] y = outputs[1] tile_id = outputs[2] if x == -1 and y == 0: score = tile_id else: tiles[(x, y)] = tile_id if tile_id == 3: paddle = x elif tile_id == 4: ball = x outputs = [] elif res == R_INPUT: computer.write(cmp(ball, paddle)) return tiles, score
def maximize_thrust(program: IntCode, phases: list, feedback_loop: bool = False) -> int: """ Return maximal possible thrust. """ list_phases = list(permutations(phases)) list_thrust = [ amplify_thrust(program, phase, feedback_loop) for phase in list_phases ] return max(list_thrust) if __name__ == '__main__': # 1 program_test_1 = IntCode( [3, 15, 3, 16, 1002, 16, 10, 16, 1, 16, 15, 15, 4, 15, 99, 0, 0]) assert maximize_thrust(program_test_1, list(range(5))) == 43210 program_test_2 = IntCode([ 3, 23, 3, 24, 1002, 24, 10, 24, 1002, 23, -1, 23, 101, 5, 23, 23, 1, 24, 23, 23, 4, 23, 99, 0, 0 ]) assert maximize_thrust(program_test_2, list(range(5))) == 54321 program_test_3 = IntCode([ 3, 31, 3, 32, 1002, 32, 10, 32, 1001, 31, -2, 31, 1007, 31, 0, 33, 1002, 33, 7, 33, 1, 33, 31, 31, 1, 32, 31, 31, 4, 31, 99, 0, 0, 0 ]) assert maximize_thrust(program_test_3, list(range(5))) == 65210 program = IntCode("input.csv")
from IntCode import * if __name__ == '__main__': program = IntCode("input.csv") program.run(inputs=[1]) output_1 = program.get_output() print("Answer #5.1: ", output_1[-1]) program_is_8_position = [3, 9, 8, 9, 10, 9, 4, 9, 99, -1, 8] assert IntCode(program_is_8_position).run(inputs=[7])[0] == 0 assert IntCode(program_is_8_position).run(inputs=[8])[0] == 1 assert IntCode(program_is_8_position).run(inputs=[9])[0] == 0 program_lt_8_position = [3, 9, 7, 9, 10, 9, 4, 9, 99, -1, 8] assert IntCode(program_lt_8_position).run(inputs=[7])[0] == 1 assert IntCode(program_lt_8_position).run(inputs=[8])[0] == 0 assert IntCode(program_lt_8_position).run(inputs=[9])[0] == 0 program_is_8_immediate = [3, 3, 1108, -1, 8, 3, 4, 3, 99] assert IntCode(program_is_8_immediate).run(inputs=[7])[0] == 0 assert IntCode(program_is_8_immediate).run(inputs=[8])[0] == 1 assert IntCode(program_is_8_immediate).run(inputs=[9])[0] == 0 program_lt_8_immediate = [3, 3, 1107, -1, 8, 3, 4, 3, 99] assert IntCode(program_lt_8_immediate).run(inputs=[7])[0] == 1 assert IntCode(program_lt_8_immediate).run(inputs=[8])[0] == 0 assert IntCode(program_lt_8_immediate).run(inputs=[9])[0] == 0 program_test = [ 3, 21, 1008, 21, 8, 20, 1005, 20, 22, 107, 8, 21, 20, 1006, 20, 31,
from IntCode import * from math import log if __name__=='__main__': # Test Replicant replicant = IntCode([ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ]) assert replicant.original_program == replicant.run() # Test Big Product program_bm = IntCode([1102,34915192,34915192,7,4,7,99,0]) assert int(log(program_bm.run()[0], 10))+1 == 16 # Test Big Print program_bp = IntCode([104,1125899906842624,99]) assert program_bp.run()[0] == 1125899906842624 for i in range(10): assert IntCode([109, 4, 203, -4, 204, -4, 99]).run(inputs=[i])[0] == i program = IntCode('input.csv') out = program.run(inputs=[1]) print("Answer #9.1: ", out) out = program.run(inputs=[2])