def part1(filename): memory = load_memory(filename, script=__file__) prog = Program(memory) run = prog.run_computer() rows = ''.join(map(chr, run)).split('\n') grid = {(x, y): cell for y, row in enumerate(rows) for x, cell in enumerate(row)} intersections = [(x, y) for x, y in grid if grid[x, y] == grid.get( (x+1, y)) == grid.get((x-1, y)) == grid.get((x, y+1)) == grid.get((x, y-1)) == '#'] return sum(x*y for x, y in intersections)
def solve(filename, count=50, use_nat=True): memory = load_memory(filename, script=__file__) computers = [Computer(memory, i) for i in range(count)] nat_time, nat_x, nat_y = None, None, None def kill_all(): for computer in computers: computer.prog.interrupt = True def notify(id, addr, x, y): nonlocal nat_time, nat_x, nat_y time = datetime.now() print('[{}] id={}: addr={} x={} y={}'.format(time, id, addr, x, y)) if addr == 255: nat_time, nat_x, nat_y = time, x, y if not use_nat: kill_all() else: i = computers[addr].prog.input i.append(x) i.append(y) def nat(grace_period): print('NAT booted') last_delivered_y = None while True: time = datetime.now() if nat_time is not None and (time - nat_time).seconds > grace_period: print('NATTACK!') if last_delivered_y == nat_y: kill_all() return last_delivered_y = nat_y notify(255, 0, nat_x, nat_y) sleep(grace_period) else: print('NATNAPPING...', nat_time and (time - nat_time).seconds) sleep(1) threads = [ Thread(target=computer.start, args=(notify, )) for computer in computers ] if use_nat: threads.append(Thread(target=nat, args=(5, ))) for t in threads: t.start() for t in threads: t.join() return nat_y
def part2(filename): memory = load_memory(filename, script=__file__) memory[0] = 2 input = parse_vararg_input( main='A,A,B,C,B,C,B,C,C,A', A='L,10,R,8,R,8', B='L,10,L,12,R,8,R,10', C='R,10,L,12,R,10', video='n' ) prog = Program(memory, input) run = prog.run_computer() return list(run)[-1]
def solve(filename): memory = load_memory(filename, script=__file__) prog = Program(memory) run = prog.run_computer() grid = defaultdict(int, {(0, 0): 1}) o2_distance = None o2_q = [] def dfs(x, y, distance): nonlocal o2_distance, o2_q for (dir_code, return_code), (dx, dy) in DIRS.items(): x1, y1 = x + dx, y + dy if (x1, y1) not in grid: prog.input.append(dir_code) status = next(run) grid[x1, y1] = status if status != 0: dfs(x1, y1, distance + 1) prog.input.append(return_code) next(run) if grid[x, y] == 2: o2_distance = distance o2_q.append((x, y)) dfs(0, 0, 0) o2_minutes = -1 while o2_q: next_q = [] for x, y in o2_q: for dx, dy in DIRS.values(): x1, y1 = x + dx, y + dy if grid[x1, y1] == 1: grid[x1, y1] = 2 next_q.append((x1, y1)) o2_minutes += 1 o2_q = next_q return (o2_distance, o2_minutes)
def part2(filename): memory = load_memory(filename, script=__file__) memory[0] = 2 prog = Program(memory) grid = {} ball_x = paddle_x = score = None block_count = 0 class PaddleInput: popleft = lambda self: sign(ball_x - paddle_x) prog.input = PaddleInput() for x, y, id in grouper(prog.run_computer(), 3): if (x, y) == (-1, 0): score = id if block_count == 0: return score else: if id == 2: block_count += 1 elif id == 3: paddle_x = x elif id == 4: ball_x = x elif id == 0 and grid.get((x, y)) == 2: block_count -= 1 grid[x, y] = id draw(grid, out=' |#-o')
def part1(filename): memory = load_memory(filename, script=__file__) grid = {(x, y): id for x, y, id in grouper(Program(memory, []).run_computer(), 3)} draw(grid, out=' █#-o') return sum(id == 2 for (x, y), id in grid.items())
def solve(filename, initial_color): memory = load_memory(filename, script=__file__) prog = Program(memory, [initial_color]) return paint(prog, initial_color)
def part2(filename): scanner = Scanner(load_memory(filename, script=__file__)) return find_box(scanner, 100, 100)
def part1(filename): scanner = Scanner(load_memory(filename, script=__file__)) return sum(scanner.scan(x, y) for y in range(50) for x in range(50))
def solve(filename): memory = load_memory(filename, script=__file__) game = Game(memory) return game.solve()
def part2(filename): memory = load_memory(filename, script=__file__) return list(Program(memory, [5]).run_computer())[-1]
def part2(filename): memory = load_memory(filename, script=__file__) return max( try_combos(perm, memory) for perm in permutations(list(range(5, 10))))
def solve(filename, script): memory = load_memory(filename, script=__file__) input = parse_input(load(script, script=__file__)) return execute_springdroid(memory, input)