def solve_part_2(puzzle_input): # Not hard to do the paths by hand PART_2_PATH = "R,12,L,8,L,4,L,4,L,8,R,6,L,6,R,12,L,8,L,4,L,4,L,8,R,6,L,6,L,8,L,4,R,12,L,6,L,4,R,12,L,8,L,4,L,4,L,8,L,4,R,12,L,6,L,4,R,12,L,8,L,4,L,4,L,8,L,4,R,12,L,6,L,4,L,8,R,6,L,6" sub_paths = { "A": "R,12,L,8,L,4,L,4", "B": "L,8,L,4,R,12,L,6,L,4", "C": "L,8,R,6,L,6" } main_path = "A,C,A,C,B,A,B,A,B,C" logic_input = [main_path, sub_paths["A"], sub_paths["B"], sub_paths["C"], "n"] program = puzzle_input[:] program[0] = 2 runtime = IntCodeRuntime() runtime.set_program(program) for line in logic_input: for char in line: runtime.input_number(ord(char)) runtime.input_number(ord("\n")) runtime.run() return runtime.get_output()[-1]
def solve_part_2(puzzle_input): amplifier_configurations = permutations(range(5, 10)) highest_signal = 0 best_configuration = None for configuration in amplifier_configurations: #print(configuration) signal = 0 amp_runtimes = [] for amp_i in range(5): amp_config = configuration[amp_i] program = puzzle_input[:] runtime = IntCodeRuntime() runtime.set_program(program) runtime.input_number(amp_config) amp_runtimes.append(runtime) while all(r.get_status() != IntCodeStatus.HALTED for r in amp_runtimes): for amp_i, runtime in enumerate(amp_runtimes): if runtime.get_status() == IntCodeStatus.HALTED: break runtime.input_number(signal) runtime.run() signal = runtime.get_output()[0] if signal > highest_signal: highest_signal = signal best_configuration = configuration return highest_signal
def solve_part_2(puzzle_input): # instructions = [ "NOT A T", "OR T J", "NOT B T", "OR T J", "NOT C T", "OR T J", "AND D J", "NOT E T", "NOT T T", "OR H T", "AND T J", ] runtime = IntCodeRuntime() runtime.set_program(puzzle_input) runtime.run() print(runtime.get_ascii_output()) runtime.input_ascii("\n".join(instructions)) runtime.input_ascii("\nRUN\n") runtime.run() output = runtime.get_output() for num in output: try: print(chr(num), end="") except: return num return ""
def solve_part_1(puzzle_input): runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) runtime.run() output = runtime.get_output() ascii_output = "".join(chr(code) for code in output) print(ascii_output) scaffold_map = [] current_line = [] for char in ascii_output: if char == "\n": scaffold_map.append(current_line) current_line = [] else: current_line.append(char) alignment_sum = 0 for j in range(1,len(scaffold_map)-2): for i in range(1, len(scaffold_map[j])-1): if scaffold_map[j][i] == "#" and \ scaffold_map[j][i+1] == "#" and scaffold_map[j+1][i] == "#" and \ scaffold_map[j][i-1] == "#" and scaffold_map[j-1][i] == "#": alignment_sum += j * i return alignment_sum
def solve_part_1(puzzle_input, system_id): runtime = IntCodeRuntime() runtime.set_program(puzzle_input) runtime.input_number(system_id) runtime.run() for line in runtime.get_output(): print(line)
def solve_part_1(puzzle_input): nics: List[IntCodeRuntime] = [] for i in range(50): runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) runtime.run() runtime.input_number(i) runtime.run() nics.append(runtime) final_output = None while final_output is None: for nic in nics: if len(nic._input) == 0: nic.input_number(-1) nic.run() output = nic.get_output() for p in range(0, len(output), 3): address, x, y = output[p:p + 3] if address == 255: final_output = y elif address < 50: nics[address].input_number(x) nics[address].input_number(y) else: raise Exception(f"Invalid address {address}") return final_output
def solve_part_1(puzzle_input): runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) runtime.run() droid = DroidController(runtime) distance = droid.explore(0) return distance, runtime
def solve_part_1(puzzle_input): runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) runtime.run() output = runtime.get_output() tile_ids = output[2::3] count_blocks = 0 for tile_id in tile_ids: if tile_id == 2: count_blocks += 1 return count_blocks
def solve_part_2(puzzle_input): nics: List[IntCodeRuntime] = [] for i in range(50): runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) runtime.run() runtime.input_number(i) runtime.run() nics.append(runtime) nat_x = None nat_y = None last_restart_y = None final_output = None while final_output is None: idle = True for nic in nics: if len(nic._input) == 0: nic.input_number(-1) else: idle = False nic.run() output = nic.get_output() for p in range(0, len(output), 3): idle = False address, x, y = output[p:p + 3] if address == 255: nat_x = x nat_y = y elif address < 50: nics[address].input_number(x) nics[address].input_number(y) else: raise Exception(f"Invalid address {address}") if idle is True: if nat_y == last_restart_y: final_output = nat_y nics[0].input_number(nat_x) nics[0].input_number(nat_y) last_restart_y = nat_y return final_output
def solve_part_2(puzzle_input): puzzle_input[0] = 2 runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) screen = [[None for _ in range(45)] for __ in range(24)] segment_display = 0 while runtime.get_status() != IntCodeStatus.HALTED: runtime.run() output = runtime.get_output() segment_display = refresh_screen(screen, segment_display, output) #joystick_input = input("Joystick Input: ") sleep(1 / 60) joystick_input = get_joystick_input(screen) joystick_signal = joystick_signal_map[joystick_input] runtime.input_number(joystick_signal) return "Game Over"
def solve_part_1(puzzle_input, macros): runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) recording = None while runtime.get_status() != IntCodeStatus.HALTED: runtime.run() print(runtime.get_ascii_output(), end="") command = input() if recording is not None: macros[recording].append(command) if command.startswith("record"): name = command.split()[-1] macros[name] = [] recording = name print("") print(f"Recording macro `{name}`") print("") print("Command?") elif command.startswith("macro"): name = command.split()[-1] run_macro(runtime, macros[name]) elif command.startswith("stop record"): print("") print(f"Stopping record of macro `{recording}`") print("") del macros[recording][-1] print("Macro saved with:") for item in macros[recording]: print(f"- {item}") recording = None print("") print("Command?") elif command.startswith("hack sensor"): hack_sensor(runtime) else: runtime.input_ascii(command + "\n") return ""
def solve(puzzle_input, start_square=0): painted = set() grid = defaultdict(int) runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) x, y = (0, 0) grid[(x, y)] = start_square dx, dy = NORTH while runtime.get_status() != IntCodeStatus.HALTED: runtime.input_number(grid[(x, y)]) runtime.run() output = runtime.get_output() assert len(output) == 2 grid[(x, y)] = output[0] painted.add((x, y)) dx, dy = get_new_direction((dx, dy), output[1]) x, y = (x + dx, y + dy) return len(painted), grid
def solve_part_1(puzzle_input): amplifier_configurations = permutations(range(5)) highest_signal = 0 best_configuration = None for configuration in amplifier_configurations: #print(configuration) signal = 0 for amp_i in range(5): amp_config = configuration[amp_i] program = puzzle_input[:] runtime = IntCodeRuntime() runtime.set_program(program) runtime.input_number(amp_config) runtime.input_number(signal) runtime.run() signal = runtime.get_output()[0] if signal > highest_signal: highest_signal = signal best_configuration = configuration return highest_signal
def solve_part_1(puzzle_input): runtime = IntCodeRuntime() runtime.set_program(puzzle_input[:]) runtime.input_number(1) runtime.run() return "\n" + "\n".join(map(str, runtime.get_output()))