def run_program_sequence(program, phase_setting_seq): amps = {} output = 0 feedback_loop_idx = 0 while amps.get('E', {}).get('status') != 'halt': for amp, phase in enumerate(phase_setting_seq, start=65): amp_name = chr(amp) if amp_name not in amps: amps[amp_name] = { 'status': 'running', 'computer': IntCodeComputer(program, [phase]) } computer = amps[amp_name]['computer'] computer.run([output]) output = computer.outputs[-1] if computer.last_optcode == 99: amps[amp_name]['status'] = 'halt' feedback_loop_idx += 1 return output
def paint_panel(hull, color, panel): hull[panel[1]][panel[0]] = BLACK if paint == 0 else WHITE return hull def print_hull(hull, robot_pos, robot_dir, delay=.015): hull[robot_pos[1]][robot_pos[0]] = robot_dir print(chr(27) + "[2J") # clear terminal for row in hull: print(''.join(row)) time.sleep(delay) hull = [[WHITE]] robot_pos = (0, 0) robot_dir = get_direction(None) process = IntCodeComputer(load_input()) camera_input = 1 # Start on white panel this time while process.last_optcode != 99: # Get color of panel camera_input = get_panel_color(hull, robot_pos) process.run(camera_input) paint, dir_output = process.outputs[-2:] # Paint panel hull = paint_panel(hull, paint, robot_pos) # Turn robot_dir = get_direction(robot_dir, dir_output) # And move hull, robot_pos = move_robot(hull, robot_pos, robot_dir) print_hull(hull, robot_pos, robot_dir)
import os os.sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from Day2.final import IntCodeComputer # noqa: E402 def load_input(): input_file = os.path.join(os.path.dirname(__file__), 'input.txt') with open(input_file) as f: inputs = f.read().split(',') return list(map(int, inputs)) if __name__ == '__main__': print("Day 5:") p1 = IntCodeComputer(load_input(), 1) p1.run() print("\tPart 1:", p1.outputs[-1]) p2 = IntCodeComputer(load_input(), 5) p2.run() print("\tPart 2:", p2.outputs[-1])
import os from collections import Counter os.sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from Day2.final import IntCodeComputer # noqa: E402 def load_input(): input_file = os.path.join(os.path.dirname(__file__), 'input.txt') with open(input_file) as f: inputs = f.read().split(',') return list(map(int, inputs)) process = IntCodeComputer(load_input()) process.run() screen = {} while process.outputs: x = process.outputs.pop(0) y = process.outputs.pop(0) tile_id = process.outputs.pop(0) screen[(x, y)] = tile_id print(Counter(screen.values())[2])
if last_ball_pos[0] > last_paddle_pos[0]: return 1 else: return -1 # Found size from part 1 using max x and max y value cols = 39 rows = 23 screen = [[' '] * (cols + 1) for i in range(rows + 1)] last_ball_pos = None last_paddle_pos = None program = load_input() program[0] = 2 process = IntCodeComputer(program) while process.last_optcode != 99: # joystick = manual_joystick() # Use arrow keys to move, down to stay still, any other key to quit joystick = auto_joystick(last_ball_pos, last_paddle_pos) # Game will play itself process.run(joystick) while process.outputs: x = process.outputs.pop(0) y = process.outputs.pop(0) tile_id = process.outputs.pop(0) if x == -1 and y == 0: player_score = tile_id else: if tile_id == 4: last_ball_pos = (x, y) if tile_id == 3:
def run_int_code_computer(program, input_val): process = IntCodeComputer(program, input_val) process.run() return process
def run_int_code_computer(program): process = IntCodeComputer(program) process.run() return process