def run_bot(screen): f = open('input.txt').read() vm = IntcodeVm(f) ds = '^>v<' dx = [0, 1, 0, -1] dy = [1, 0, -1, 0] max_x, max_y = 50, 12 i, j, d = 0, max_y // 2, 0 grid = [[0 for x in range(max_x)] for y in range(max_y)] grid[j][i] = 1 def grid_in(): while True: yield grid[j][i] g_in = grid_in() while True: p = vm.eval(g_in) if p is None: break grid[j][i] = p p = vm.eval(g_in) if p is None: break if p == 0: p = -1 d = (d + p) % 4 i += dx[d] j += dy[d] v = grid[j][i] grid[j][i] = 2 draw(screen, grid, (i, j), ds[d]) grid[j][i] = v time.sleep(10)
def part2(): program = get_input() program[0] = 2 vm, screen, score = IntcodeVm(program, []), {}, 0 paddle_x, ball_x = 0, 0 def update(): nonlocal score, paddle_x, ball_x while True: a, b, c = vm.receive(), vm.receive(), vm.receive() if a is None or b is None or c is None: break if (a, b) != (-1, 0): screen[(a, b)] = c if c == 3: paddle_x = a if c == 4: ball_x = a else: score = c def joystick_pos(): if paddle_x < ball_x: return 1 elif paddle_x > ball_x: return -1 else: return 0 while True: try: update() vm.send(joystick_pos()) except StopIteration: break return score
def part1(): program = get_input() vm, screen = IntcodeVm(program, []), {} while True: try: a, b, c = vm.receive(), vm.receive(), vm.receive() screen[(a, b)] = c except StopIteration: break return sum(v == 2 for v in screen.values())
def play_game(): vm = IntcodeVm(MAIN_PROGRAM) while True: while True: try: char = next(vm.outputs) if char is None: break print(chr(char), end='') except StopIteration: return command = input('Enter command: ') for char in command + '\n': vm.send(ord(char))
def map_ship(screen=None): grid = [[0 for _ in range(lenx)] for _ in range(leny)] x, y = sx, sy # grid[y][x] = 8 while True: if grid[y][x] == 0: grid[y][x] = 1 d, nx, ny = choose_next(grid, x, y) if d == -1: # done break out = vm.eval(IntcodeVm.pipe_one(d)) if out == 0: grid[ny][nx] = -3 continue x, y = nx, ny if screen: draw(screen, grid) if out == 2: # time.sleep(10) grid[y][x] = 2 # break grid = [[-p if p < 0 else p if p != 0 else -3 for p in r] for r in grid] if screen: draw(screen, grid) time.sleep(0.5) fill(screen, grid) return grid
def part1(): program = get_input() program[1], program[2] = 12, 2 vm = IntcodeVm(program, []) for _ in vm.outputs: pass return vm[0]
def part2(): network = [IntcodeVm(MAIN_PROGRAM) for _ in range(50)] for idx, vm in enumerate(network): vm.send(idx) nat_x, nat_y = 0, 0 last_restart = None while True: while True: sent_packets = False for vm in network: a, b, c = vm.receive(), vm.receive(), vm.receive() if a is None: vm.send(-1) elif a is not None and 0 <= a < 50: network[a].send(b) network[a].send(c) sent_packets = True elif a is not None and a == 255: nat_x, nat_y = b, c if not sent_packets: break if nat_y == last_restart: return nat_y network[0].send(nat_x) network[0].send(nat_y) last_restart = nat_y
def part2(): for a, b in itertools.product(range(100), range(100)): program = get_input() program[1], program[2] = a, b vm = IntcodeVm(program) for _ in vm.outputs: pass if vm[0] == 19690720: return 100 * a + b
def part1(): program, best_output = get_input(), -float("inf") for phases in permutations(range(5), 5): amps, signal = [IntcodeVm(program, [phases[idx]]) for idx in range(5)], 0 for amp, phase in zip(amps, phases): amp.send(signal) signal = amp.receive() if signal > best_output: best_output = signal return best_output
def run_game(screen, data): mem = parse_memory(data) mem[0] = 2 vm = IntcodeVm(mem) grid = [[tiles[0] for _ in range(48)] for _ in range(24)] score = 0 ai = agent(grid) i = 0 while True: i += 1 x = vm.eval(ai) if x is None: break y = vm.eval(ai) t = vm.eval(ai) if x == -1 and y == 0: score = t else: grid[y][x] = tiles[t] if i % 1 == 0: draw(screen, grid, score) time.sleep(0.005) draw(screen, grid, score) time.sleep(10)
def part1(): network = [IntcodeVm(MAIN_PROGRAM) for _ in range(50)] for idx, vm in enumerate(network): vm.send(idx) while True: for vm in network: a, b, c = vm.receive(), vm.receive(), vm.receive() if a is None: vm.send(-1) elif a is not None and 0 <= a < 50: network[a].send(b) network[a].send(c) elif a is not None and a == 255: return c
def part2(): program, best_output = get_input(), -float("inf") for phases in permutations(range(5, 10), 5): amps = [IntcodeVm(program, [phases[idx]]) for idx in range(5)] current_amp, signal = 0, 0 while True: try: amps[current_amp].send(signal) signal = amps[current_amp].receive() current_amp = (current_amp + 1) % 5 except StopIteration: if signal > best_output: best_output = signal break return best_output
def run_springscript(instructions): vm = IntcodeVm(MAIN_PROGRAM) for instr in instructions: for c in instr: vm.send(ord(c)) vm.send(ord('\n')) outputs = list(vm.outputs) if outputs[-1] > 255: print(outputs[-1]) else: print(''.join(chr(c) for c in outputs))
def paint(prog, initial_white_tiles = []): vm = IntcodeVm(prog) visited, whites, pos, direction = set(), set(initial_white_tiles), (0, 0), 'U' while True: vm.send(1 if pos in whites else 0) try: paint, lr = vm.receive(), vm.receive() visited.add(pos) if paint == 1: whites.add(pos) elif paint == 0: whites.discard(pos) direction = turn(direction, lr) pos = move(pos, direction) except StopIteration: break return visited, whites
def part2(): program = get_input() vm = IntcodeVm(program) dest, locations = fully_explore(vm) distances = bfs(locations, dest) return max(distances.values())
def part1(): program = get_input() vm = IntcodeVm(program) dest, locations = fully_explore(vm) distances = bfs(locations, dest) return distances[(0, 0)]
def place_drone(x, y): def pipe_pos(): yield x yield y return IntcodeVm(mem).eval(pipe_pos())
def part1(): program = get_input() *_, output = IntcodeVm(program, [1]).outputs return output
def is_beam(x, y): vm = IntcodeVm(PROGRAM) vm.send(x) vm.send(y) return vm.receive() == 1
return i def render(screen, f, *args): curses.curs_set(0) f(screen, *args) curses.curs_set(1) def draw(screen, grid): for i, r in enumerate(reversed(grid)): for j, p in enumerate(r): if p == 0: p = ' ' elif abs(p) == 1: p = ' ' elif p == -1: p = ' ' elif abs(p) == 2: p = '0' elif abs(p) == 3: p = '#' elif abs(p) == 4: p = '0' screen.addstr(i, j, str(p)) screen.refresh() lenx, leny = 41, 41 sx, sy = lenx//2-1, leny//2-1 f = open('input.txt').read() vm = IntcodeVm(f) # lab = map_ship() # dijkstra(lab) # print(dijkstra(lab)) curses.wrapper(render, map_ship)
if c > 255: lines.append("Total Dust: " + str(c)) draw_grid(screen, prev_grid, lines) time.sleep(10) c = str(chr(c)) if c == '\n': if row: grid.append(row) if len(grid) == 43: draw_grid(screen, grid, lines) i = 0 if grid != []: prev_grid = grid grid = [] row = [] continue row.append(c) i += 1 curses.curs_set(1) # Part 1 mem = IntcodeVm.parse_memory(open('input.txt').read()) mem[0] = 2 vm = IntcodeVm(mem) grid = parse_grid(vm) print(sum([x * y for x, y in find_intersections(grid)])) # Part 2 print(find_strats(grid)) lines = provide_input(vm, start()) curses.wrapper(capture_video, vm, lines)