def run_amps(prog, phases): amps = [] phases_supplied = [] for i in range(5): amps.append(IntcodeComp(prog)) phases_supplied.append(False) current_amp = 0 last_output = [0] prev_output = None while len(last_output) > 0: input_data = [] if not phases_supplied[current_amp]: phases_supplied[current_amp] = True input_data.append(phases[current_amp]) input_data.append(last_output[0]) prev_output = last_output last_output = amps[current_amp].run_until_input(input_data) current_amp = (current_amp + 1) % 5 return prev_output[0]
def main(stdscr): lines = open("15.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) map_graph = defaultdict(set) loc = (0, 0) for neig in neighbores(loc): map_graph[neig].add(loc) map_graph[loc].add(neig) comp = IntcodeComp(prog) direction = 1 loc_status = defaultdict(lambda: ' ') loc_status[loc] = '.' stdscr.clear() sys_loc = None while not comp.halted: new_loc = neighbores(loc)[direction - 1] droid_status = comp.run_until_input([direction])[0] if droid_status == 0: map_graph[loc].remove(new_loc) del map_graph[new_loc] loc_status[new_loc] = '#' elif droid_status == 1: loc_status[new_loc] = '.' loc = new_loc for neig in neighbores(loc): if loc_status[neig] != '#': map_graph[loc].add(neig) map_graph[neig].add(loc) elif droid_status == 2: loc_status[new_loc] = 'O' loc = new_loc sys_loc = loc for neig in neighbores(loc): if loc_status[neig] != '#': map_graph[loc].add(neig) map_graph[neig].add(loc) dists = dijkstra(map_graph, loc) unvisted_dists = { l: dists[l] for l in dists.keys() if loc_status[l] == ' ' } if len(unvisted_dists) == 0: break dest_loc = min(unvisted_dists, key=unvisted_dists.get) next_on_path = dest_loc while dists[next_on_path] > 1: neig_dists = { neig: dists[neig] for neig in neighbores(next_on_path) if neig in dists } next_on_path = min(neig_dists, key=neig_dists.get) loc_neigs = neighbores(loc) for d in range(4): if loc_neigs[d] == next_on_path: direction = d + 1 break draw_screen(stdscr, loc_status, loc, dest_loc) dists = dijkstra(map_graph, sys_loc) stdscr.addstr(0, 0, f"Part 1: {dists[(0,0)]}") stdscr.getkey() ox_locs = [sys_loc] time = 0 while len(ox_locs) > 0: time += 1 new_ox_locs = [] for ox_loc in ox_locs: for neig in neighbores(ox_loc): if loc_status[neig] == '.': new_ox_locs.append(neig) loc_status[neig] = 'O' ox_locs = new_ox_locs draw_screen(stdscr, loc_status, loc, None) stdscr.addstr(0, 0, f"Part 2: {time}") stdscr.getkey()
from intcode_comp import IntcodeComp if __name__ == "__main__": lines = open("05.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) comp = IntcodeComp(prog) out = comp.run([1]) print(f"Part 1: {out}") comp = IntcodeComp(prog) out = comp.run([5]) print(f"Part 2: {out}")
from intcode_comp import IntcodeComp if __name__ == "__main__": lines = open("11.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) comp = IntcodeComp(prog) out = comp.run_until_input([1]) world = {} coords = (0, 0) coords_delta = (0, 1) d = 0 while len(out) > 0: # print(f"O {coords} {d} {out}") world[coords] = out[0] if out[1] == 0: d = (d - 90) % 360 if d < 0: d = 360 + d else: d = (d + 90) % 360 if d == 0: coords_delta = (0, 1) elif d == 90: coords_delta = (1, 0) elif d == 180: coords_delta = (0, -1) elif d == 270: coords_delta = (-1, 0) else: assert False
from itertools import combinations def run_inputs(comp, in_strs): out_str = None for in_str in in_strs: in_data = list(map(ord, in_str + "\n")) out_data = comp.run_until_input(in_data) out_str = ''.join(map(chr, out_data)) return out_str if __name__ == "__main__": lines = open("25.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) comp = IntcodeComp(prog) in_strs = [ "east", "take antenna", # Crew "west", "north", # Hall "take weather machine", "north", # Storage "take klein bottle", "east", # Stable "take spool of cat6", "east", # Gift "south", # Kitchen "take mug", "north", "north", # Sick
from intcode_comp import IntcodeComp if __name__ == "__main__": lines = open("02.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) comp = IntcodeComp(prog) comp.mem[1] = 12 comp.mem[2] = 2 comp.run([]) print(f"Part 1: {comp.mem[0]}") target = 19690720 for noun in range(100): for verb in range(100): comp = IntcodeComp(prog) comp.mem[1] = noun comp.mem[2] = verb comp.run([]) if comp.mem[0] == target: break if comp.mem[0] == target: break print(f"Part 2: {noun * 100 + verb}")
from intcode_comp import IntcodeComp from collections import defaultdict if __name__ == "__main__": lines = open("23.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) comps = {} in_queues = defaultdict(list) for addr in range(0, 50): comp = IntcodeComp(prog) out = comp.run_until_input([addr]) assert len(out) == 0 comps[addr] = comp nat_mem = None last_nat_mem_send = None done = False while not done: idle = True for addr in range(0, 50): in_queue = in_queues[addr] if len(in_queue) == 0: in_queue.append(-1) out = comps[addr].run_until_input(in_queue) in_queues[addr] = [] assert not comps[addr].halted for idx in range(0, len(out), 3): idle = False dest = out[idx]
from intcode_comp import IntcodeComp if __name__ == "__main__": lines = open("19.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) affected = 0 for y in range(0, 50): for x in range(0, 50): comp = IntcodeComp(prog) out = comp.run([x, y]) print('.' if out[0] == 0 else '#', end="") affected += out[0] print("") print(f"Part 1: {affected}")
from intcode_comp import IntcodeComp def neighbores(loc): result = list() result.append((loc[0] + 0, loc[1] - 1)) # North result.append((loc[0] + 0, loc[1] + 1)) # South result.append((loc[0] - 1, loc[1] + 0)) # West result.append((loc[0] + 1, loc[1] + 0)) # East return result if __name__ == "__main__": lines = open("17.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) comp = IntcodeComp(prog) out = comp.run_until_input([]) map_str = ''.join(map(chr, out)) map_lines = map_str.split("\n") align_param = 0 for y in range(1, 45 - 1): for x in range(1, 38 - 1): cross = map_lines[y][x] == '#' and all( map_lines[neig[1]][neig[0]] == '#' for neig in neighbores((x, y))) if cross: align_param += (x * y) print(f"Part 1: {align_param}") comp = IntcodeComp(prog)
paddle_x = x elif tile == 4: # print("o", end="") # ball ball_x = x #print(" ") return (ball_x, paddle_x) if __name__ == "__main__": lines = open("13.dat", "r").readlines() prog = list(map(int, lines[0].split(','))) seconds_per_frame = 0 frame_start = time.perf_counter() screen = defaultdict(lambda: 0) comp = IntcodeComp(prog) comp.mem[0] = 2 raw_out = comp.run_until_input([]) while not comp.halted: #os.system('cls' if os.name == 'nt' else 'clear') ball_x, paddle_x = print_screen(screen, raw_out) if ball_x < paddle_x: joystick = -1 elif ball_x > paddle_x: joystick = 1 else: joystick = 0 frame_end = time.perf_counter() frame_time = frame_end - frame_start