def __init__(self, program_file): self.ic = IntcodeComputer.from_file(program_file) self.vm = Thread(target=self.ic.run) self.seen = {} self.oxygen = {} self.to_fill = None self._in = self.ic.in_queue.put self._out = self.ic.out_queue.get self.vm.start()
def main(filename): ic = IntcodeComputer.from_file(filename) for line in reversed(SCRIPT): [ic.in_queue.put(ord(x)) for x in reversed(line + '\n')] ic.run() out = [] while True: try: out.append(ic.output) except Empty: break try: print(''.join([chr(x) for x in reversed(out)])) except ValueError: print('DAMAGE:', out[0])
def main(filename): ic = IntcodeComputer.from_file(filename) offset = 639 height = ic.code[604] width = max(ic.code[620], ic.code[621]) // height no = ic.code[632] a = max(ic.code[612], ic.code[613]) b = max(ic.code[616], ic.code[617]) s = 0 for y in range(height): for x in range(width): if ic.code[offset + y * width + x] == 2: s += ic.code[no + (((x * height + y) * a + b) % (width * height))] print(s)
def main(filename): ic = IntcodeComputer.from_file(filename) ic.run() out = ic.get_output() grid = defaultdict(int) for x, y, tile in chunks(out, 3): grid[(x, y)] = tile max_x, _ = max(grid.keys(), key=itemgetter(0)) _, max_y = max(grid.keys(), key=itemgetter(1)) print(max_x, max_y) count = 0 for tile in grid.values(): if tile == 2: count += 1 print(count)
def main(filename): ic = IntcodeComputer.from_file(filename) robot = Thread(target=ic.run) robot.start() loc = defaultdict(int) start = [0, 0] while True: cmd = int(input('CMD: ')) if cmd == 0: break ic.in_queue.put(cmd) out = ic.out_queue.get() if out == 2: break start[0] += 1 print(start)
def main(filename): affected = 0 for y in range(50): for x in range(50): ic = IntcodeComputer.from_file(filename) ic.in_queue.put(y) ic.in_queue.put(x) ic.run() try: out = ic.out_queue.get(timeout=0.5) affected += out print('#' if out else '.', end='') except Empty: pass print() print(affected)
def main(filename): nodes = [] queues = [] for addr in range(50): queues.append((Queue(), Queue())) ic = IntcodeComputer.from_file(filename, queues[-1][0], queues[-1][1], addr) nodes.append(Thread(target=ic.run)) nodes[-1].start() nat = None idle = False last = None while True: if idle and nat: x, y = nat queues[0][0].put_nowait(x) queues[0][0].put_nowait(y) print('NAT sending', x, y) if y == last: print('TWICE in a row:', y) break last = y idle = False sendout = [] for qin, qout in queues: try: out = qout.get_nowait() if out: x = qout.get_nowait() y = qout.get_nowait() print(f'SEND TO {out}: X: {x}, Y: {y}') if out == 255: nat = x, y else: queues[out][0].put_nowait(x) queues[out][0].put_nowait(y) sendout.append(out) except Empty: pass for q in [x for x in range(50) if x not in sendout]: queues[q][0].put_nowait(-1) if not sendout: idle = True
def main(filename): ic = IntcodeComputer.from_file(filename) ic.run() field = [] line = [] for i in ic.get_output(): if i == 10: field.append(line[:]) line = [] else: line.append(chr(i)) for i, line in enumerate(field): print(f'{i:2d}', ''.join(line)) align = [] for y, row in enumerate(field): for x, cell in enumerate(row): if cell == '#' and is_intersection(x, y, field): print(f'FOUND: X: {x}, Y: {y}') align.append(x * y) print(sum(align))
def main(filename): nodes = [] queues = [] for addr in range(50): queues.append((Queue(), Queue())) ic = IntcodeComputer.from_file(filename, queues[-1][0], queues[-1][1], addr) nodes.append(Thread(target=ic.run)) nodes[-1].start() while True: for qin, qout in queues: try: out = qout.get_nowait() if out: x = qout.get_nowait() y = qout.get_nowait() print(f'SEND TO {out}: X: {x}, Y: {y}') if out == 255: print(y) break queues[out][0].put_nowait(x) queues[out][0].put_nowait(y) except Empty: qin.put_nowait(-1)
def inside(filename, x, y): ic = IntcodeComputer.from_file(filename) ic.write(y) ic.write(x) ic.run() return ic.output == 1