def signal(phases): amps = [Vm(program, [x]) for x in phases] signal = 0 for i, amp in cycle(enumerate(amps)): amp.input(signal) if amp.run() and i == 4: return amp.output() signal = amp.output()
def simulate(): computers = [Vm(program, [i]) for i in range(50)] while True: idle = True nat = None for c in computers: if not c.has_input(): c.input(-1) c.run() for dest, x, y in chunk(c.drain_output(), 3): if dest == 255: nat = x, y else: computers[dest].input(x) computers[dest].input(y) idle = False if idle and nat and not any(c.has_input() for c in computers): x, y = nat yield y computers[0].input(x) computers[0].input(y)
droid.input_line(line) droid.run() return droid.drain_output() def response(line): return "".join(map(chr, run(line))) def solve(): for line in all_items.splitlines(): droid.input_line(line) droid.run() droid.drain_output() assert "Security Checkpoint" in response("north") items = [x[2:] for x in response("inv").splitlines() if x.startswith("-")] last_c = 0 for ix in range(1, 2 ** len(items)): c = ix ^ (ix >> 1) # Gray code. item_ix = len(bin(last_c ^ c)) - 3 # Find the index of the flipped bit. action = "drop" if c & (last_c ^ c) else "take" run(f"{action} {items[item_ix]}") last_c = c if "you are ejected back to the checkpoint" not in (r := response("east")): return re.search(f"[0-9]+", r)[0] droid = Vm(data(25).read()) print(solve())
from intcode import Vm from aoc import * program = data(9).read() print(Vm(program, [1]).complete()[-1]) print(Vm(program, [2]).complete()[-1])
if r := search(pos): # Do not stop exploring, we need to map the entire ship. system = r else: continue # Found a wall. Do not move back. droid.input(back[cmd]) droid.run() droid.output() # Pop return system def distances(start): queue = deque([start]) dist = {start: 0} while queue: pos = queue.pop() for p in around(pos): if p not in dist and ship.get(p) in [1, 2]: dist[p] = dist[pos] + 1 queue.append(p) return dist ship = {} moves = {1: (-1, 0), 2: (1, 0), 3: (0, -1), 4: (0, 1)} back = {1: 2, 2: 1, 3: 4, 4: 3} droid = Vm(data(15).read()) system = search((0, 0)) print(distances((0, 0))[system]) print(max(distances(system).values()))
from intcode import Vm from aoc import * program = data(13).read() print(Vm(program).complete()[2::3].count(2)) game = Vm(program) game.set_tape(0, 2) score = 0 last_x = {} game.run() while game.has_output(): for x, y, k in chunk(game.drain_output(), 3): if x == -1 and y == 0: score = k else: last_x[k] = x game.input((last_x[3] < last_x[4]) - (last_x[3] > last_x[4])) game.run() print(score)
if c.isprintable() or c == "\n": return c return f"<{x}>" parser = argparse.ArgumentParser() parser.add_argument("-r", "--raw", help="do not use ascii I/O", action="store_true") parser.add_argument("-i", "--inputs", help="provide input in advance", nargs="+") parser.add_argument("-l", "--log", help="log the session") parser.add_argument( "-t", "--tape", help="tape overrides", nargs="+", default=[], type=parse_overrides ) parser.add_argument("program", help="program to run") args = parser.parse_args() vm = Vm(open(args.program).read()) for pos, val in args.tape: vm.set_tape(pos, val) log = open(args.log, "w") if args.log else None while True: halted = vm.run() output = vm.drain_output() if args.raw: # In raw mode we might get no output, no point printing nothing. if output: print(" ".join(map(str, output))) else: print("".join(map(try_chr, output)), end="") if halted:
def pulled(x, y): return bool(Vm(drone, [x, y]).complete()[-1])
def simulate(n, v): computer = Vm(program) computer.set_tape(1, n) computer.set_tape(2, v) computer.complete() return computer.get_tape(0)
def run(program): droid = Vm(data(21).read()) for line in program.strip().splitlines(): droid.input_line(line) print(droid.complete()[-1])