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("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
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) comp.mem[0] = 2
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]
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 #print(f"Frame render time (s): {frame_time}") sleep_time = max(0, seconds_per_frame - frame_time) time.sleep(sleep_time)