async def trial(out_recv, in_send): moves = next_trick() move = next(moves) intcode.send_ascii(in_send, move) async for screen in display(out_recv): # print(screen.strip()) # check if passed if '==' in screen: name, items, dirs = parse_screen(screen) if name != 'Security Checkpoint': print(screen.strip()) return move = next(moves) intcode.send_ascii(in_send, move)
async def find_checkpoint(out_recv, in_send): pos = 0 visited = {pos} path = deque([pos]) async for screen in display(out_recv): if '==' in screen: name, items, dirs = parse_screen(screen) if name == 'Security Checkpoint': return pos, visited, path # found it! move, npos = next_move(pos, items, dirs, visited, path) if npos is not None: path.append(npos) visited.add(npos) pos = npos intcode.send_ascii(in_send, move)
async def rewind(out_recv, in_send, pos, visited, path): # program waiting for input already, just take a step back move, pos = next_move(pos, [], [], visited, path) path.append(pos) intcode.send_ascii(in_send, move) async for screen in display(out_recv): if '==' in screen: name, items, dirs = parse_screen(screen) move, npos = next_move(pos, items, dirs, visited, path) if move is None: return # back at beginning if npos is not None: path.append(npos) visited.add(npos) pos = npos intcode.send_ascii(in_send, move)
async def run(memory, input): async with trio.open_nursery() as nursery: # set up channels in_send, in_recv = trio.open_memory_channel(10) out_send, out_recv = trio.open_memory_channel(0) # start program nursery.start_soon(intcode.process, memory, in_recv, out_send) out = deque(input.strip().split('\n')) async with in_send, out_recv: async for status in intcode.irecv_ascii(out_recv): if status == intcode.Command.INPUT: intcode.send_ascii(in_send, out.popleft()) else: if isinstance(status, int) and status > 255: print(f"damage: {status}") return status else: print(status)
async def replay(out_recv, in_send, pos, path): # program waiting for input already, take first step _ = path.popleft() # drop start npos = path.popleft() dir = npos - pos move = _compass[dir] print(f'> retracing {move}') intcode.send_ascii(in_send, move) pos = npos async for _ in display(out_recv): if not path: # figure out direction or assume west? return # done npos = path.popleft() dir = npos - pos move = _compass[dir] print(f'> retracing {move}') intcode.send_ascii(in_send, move) pos = npos