def run(prog, instr): intcode = IntCode(prog[:], [instr]) intcode.running = True while intcode.running: intcode.run_program() return intcode.output_buffer[-1]
def part_1(): input_file = open("resources/02input.txt") prog = [int(a) for a in input_file.read().strip().split(",")] input_file.close() prog[1] = 12 prog[2] = 2 intcode = IntCode() intcode.program = prog[:] intcode.run_program() return intcode.program[0]
def part_2(): input_file = open("resources/09input.txt") my_prog = [int(a) for a in input_file.read().strip().split(",")] input_file.close() intcode = IntCode(my_prog, [2], 2000) intcode.running = True while intcode.running: intcode.run_program() # print(intcode.output_buffer) return intcode.output_buffer[0]
def gravity_assist(program, expected_result): intcode = IntCode() for noun in range(0, 99): for verb in range(0, 99): testprog = program[:] testprog[1], testprog[2] = noun, verb intcode.program = testprog intcode.run_program() result = intcode.program if result[0] == expected_result: return (noun * 100) + verb return -1, -1
def part_1(): input_file = open("resources/07input.txt") my_prog = [int(a) for a in input_file.read().strip().split(",")] input_file.close() params1 = [0, 1, 2, 3, 4] param_sets = list(itertools.permutations(params1)) highscore = 0 for param_set in param_sets: input_vals = [0, 0] for phase in param_set: intcode = IntCode() input_vals[0] = phase intcode.input_buffer = input_vals[:] intcode.program = my_prog[:] intcode.run_program() if len(intcode.output_buffer) > 0: input_vals[1] = intcode.output_buffer[0] highscore = intcode.output_buffer[ 0] if intcode.output_buffer[0] > highscore else highscore return highscore
def test_quine(): ex1 = IntCode() ex1.get_input("input/day_09_p1_ex1") ex1.diagnostic_program() assert ex1.result_history == [ 109, 1, 204, -1, 1001, 100, 1, 100, 1008, 100, 16, 101, 1006, 101, 0, 99 ]
def part_2(): input_file = open("resources/07input.txt") my_prog = [int(a) for a in input_file.read().strip().split(",")] input_file.close() params1 = [5, 6, 7, 8, 9] param_sets = list(itertools.permutations(params1)) highscore = 0 ampidx = 0 for param_set in param_sets: # Instantiate the chained amplifiers. intcodes = [ IntCode(my_prog[:], [param_set[i]]) for i in range(len(param_set)) ] for amp in intcodes: amp.running = True # Set up the first one with input. intcode = intcodes[ampidx] input_val = 0 # Loop continuously through the amplifiers, with any output passed on as input to the next one. while True: intcode.input_buffer.append(input_val) intcode.run_program() # Prepare settings for next amplifier. ampidx = (ampidx + 1) % len(intcodes) # Check latest output if any. if len(intcode.output_buffer) > 0: input_val = intcode.output_buffer[-1] highscore = intcode.output_buffer[ -1] if intcode.output_buffer[-1] > highscore else highscore # Quit when last amplifier has terminated. if not intcodes[-1].running: break intcode = intcodes[ampidx] return highscore
def test_d2(input_file, user_input, expected_result): intcode = IntCode() intcode.get_input(input_file) intcode.set_user_input(user_input) assert intcode.diagnostic_program() == expected_result
def test_part1(): intcode = IntCode() intcode.get_input("input/day_05") intcode.set_user_input([1]) intcode.diagnostic_program() assert intcode.result == 16489636
def test_halt_and_restart(): intcode = IntCode() intcode.get_input("input/day_07") intcode.set_user_input([1, 2]) intcode.diagnostic_program() return_value = intcode.result intcode = IntCode() intcode.get_input("input/day_07") intcode.set_user_input([1]) intcode.diagnostic_program() intcode.set_user_input([2]) intcode.diagnostic_program() assert return_value == intcode.result
def part1(input_file="input/day_07"): results = [] perm = permutations([0, 1, 2, 3, 4]) for p in perm: amp_a = IntCode() amp_a.get_input(input_file) amp_a.set_user_input([p[0], 0]) amp_a.diagnostic_program() result_a = amp_a.result amp_b = IntCode() amp_b.get_input(input_file) amp_b.set_user_input([p[1], result_a]) amp_b.diagnostic_program() result_b = amp_b.result amp_c = IntCode() amp_c.get_input(input_file) amp_c.set_user_input([p[2], result_b]) amp_c.diagnostic_program() result_c = amp_c.result amp_d = IntCode() amp_d.get_input(input_file) amp_d.set_user_input([p[3], result_c]) amp_d.diagnostic_program() result_d = amp_d.result amp_e = IntCode() amp_e.get_input(input_file) amp_e.set_user_input([p[4], result_d]) amp_e.diagnostic_program() result_e = amp_e.result if result_e == 298586: print(p) results.append(result_e) print(max(results)) return max(results)
def part2(input_file="input/day_07"): perm = permutations([5, 6, 7, 8, 9]) results = [] for p in perm: phase_seq = str(p[0]) + str(p[1]) + str(p[2]) + str(p[3]) + str(p[4]) result_e = 0 amp_a = IntCode() amp_a.get_input(input_file) amp_a.set_user_input([p[0]]) amp_b = IntCode() amp_b.get_input(input_file) amp_b.set_user_input([p[1]]) amp_c = IntCode() amp_c.get_input(input_file) amp_c.set_user_input([p[2]]) amp_d = IntCode() amp_d.get_input(input_file) amp_d.set_user_input([p[3]]) amp_e = IntCode() amp_e.get_input(input_file) amp_e.set_user_input([p[4]]) while True: amp_a.set_user_input([result_e]) amp_a.diagnostic_program() result_a = amp_a.result amp_b.set_user_input([result_a]) amp_b.diagnostic_program() result_b = amp_b.result amp_c.set_user_input([result_b]) amp_c.diagnostic_program() result_c = amp_c.result amp_d.set_user_input([result_c]) amp_d.diagnostic_program() result_d = amp_d.result amp_e.set_user_input([result_d]) amp_e.diagnostic_program() result_e = amp_e.result if result_e == 9246095: print(p) if amp_e.halt_code_reached: # print(phase_seq, result_e) results.append(result_e) break print(max(results)) return max(results)