def part2(pc: intcode.Computer) -> int: pc.reset(wait_for_input=True) pc.data[0] = 2 # initial setup pc.run() idx = pc.output.index(-1) while pc.output[idx + 1] != 0: idx = pc.output.index(-1, idx + 1) entities = getEntities(pc.output[:idx]) pc.output = pc.output[idx:] # loop while not pc.stop: pc.run() changes = getEntities(pc.output) if changes["score"]: entities["score"] = changes["score"] if changes["ball"]: changes["empty"].remove(entities["ball"]) entities["ball"] = changes["ball"] if changes["paddle"]: changes["empty"].remove(entities["paddle"]) entities["paddle"] = changes["paddle"] for e in changes["empty"]: entities["block"].remove(e) pc.output.clear() pc.input = entities["ball"][0] - entities["paddle"][0] return entities["score"]
def p2(lines): seed = list(lines[0]) seed[0] = 2 computer = Computer(seed) runnable = computer.run_output() # solved manually by looking at the view generated at part 1 # after writing the whole sequence, find repeating sequences fa = ['R', '12', 'L', '10', 'R', '12'] fb = ['L', '8', 'R', '10', 'R', '6'] fc = ['R', '12', 'L', '10', 'R', '10', 'L', '8'] mainseq = ['A', 'B', 'A', 'C', 'B', 'C', 'B', 'C', 'A', 'C'] cinput = '\n'.join([ ','.join(mainseq), ','.join(fa), ','.join(fb), ','.join(fc), ]) + '\nn\n' def fetch(): for c in cinput: yield ord(c) computer.input = fetch() for o in runnable: if o > 255: return o
def p2(lines): screen = dict() score = 0 def fetch(): while True: ball = next((k for k, v in screen.items() if v == 4), None) paddle = next((k for k, v in screen.items() if v == 3), None) if not ball or not paddle: yield 0 if ball[0] < paddle[0]: yield -1 elif ball[0] > paddle[0]: yield 1 else: yield 0 seed = list(lines[0]) seed[0] = 2 computer = Computer(seed) computer.input = fetch() runnable = computer.run_output() try: while True: x, y, tile_id = next(runnable), next(runnable), next(runnable) if x == -1 and y == 0: score = tile_id else: screen[(x, y)] = tile_id except StopIteration: pass return score
def p1(lines): # All of the panels are currently black panels = defaultdict(int) painted = set() pos = ((0, 0), 0) def fetch(): while True: # provide 0 if the robot is over a black panel # or 1 if the robot is over a white panel yield panels[pos[0]] computer = Computer(lines[0]) computer.input = fetch() runnable = computer.run_output() try: while True: panels[pos[0]] = next(runnable) painted.add(pos[0]) turn = next(runnable) d = (pos[1] + (3 if turn == 0 else 1)) % 4 pos = ((pos[0][0] + directions[d][0], pos[0][1] + directions[d][1]), d) except StopIteration: pass return len(painted)
def compute(lines, permutation): signal = 0 for phase in permutation: computer = Computer(lines[0]) computer.input = iter([phase, signal]) computer.run_mode() signal = computer.output return signal
def compute2(lines, permutation): inputs = {} runnable = {} def fetch(idx): return (inputs[idx][i] for i in itertools.count()) for idx, phase in enumerate(permutation): c = Computer(lines[0]) c.input = fetch(idx) inputs[idx] = [phase] runnable[idx] = c.run_output() inputs[0].append(0) while True: for n, r in runnable.items(): try: out = next(r) except StopIteration: return out inputs[(n + 1) % 5].append(out)
def p2(lines): # The rest of the panels are still black, # but it looks like the robot was expecting to start on a white panel, not a black one. panels = defaultdict(int) panels[(0, 0)] = 1 pos = ((0, 0), 0) def fetch(): while True: # provide 0 if the robot is over a black panel # or 1 if the robot is over a white panel yield panels[pos[0]] computer = Computer(lines[0]) computer.input = fetch() runnable = computer.run_output() try: while True: panels[pos[0]] = next(runnable) turn = next(runnable) d = (pos[1] + (3 if turn == 0 else 1)) % 4 pos = ((pos[0][0] + directions[d][0], pos[0][1] + directions[d][1]), d) except StopIteration: pass xs = sorted(x for x, _ in panels.keys()) ys = sorted(y for _, y in panels.keys()) minx, maxx, miny, maxy = min(xs), max(xs), min(ys), max(ys) return '\n' + '\n'.join([ ''.join([ '█' if panels[(x, y)] == 1 else ' ' for x in range(minx, maxx + 1) ]) for y in range(maxy, miny - 1, -1) ])
def p1(lines): computer = Computer(lines[0]) computer.input = iter([1]) computer.run_mode() return computer.output
screen[y][x] = tile_id outs = [] print(sum([row.count(2) for row in screen])) # part two opcodes[0] = 2 cpu = Computer(opcodes[:]) screen = [[0] * 50 for _ in range(30)] score = 0 while not cpu.halted: out = cpu.step() if out != None: outs.append(out) if len(outs) == 3: x, y, tile_id = outs if x == None: break if x == -1 and y == 0: score = tile_id else: screen[y][x] = tile_id outs = [] if tilex(3) < tilex(4): cpu.input = 1 elif tilex(3) > tilex(4): cpu.input = -1 else: cpu.input = 0 print(score)