Example #1
0
def send_cmd(p: IntcodeProgram, cmd: str | None):
    if cmd is not None:
        for c in cmd:
            p.write_input(ord(c))
        p.write_input(ord("\n"))
    p.run()
    output = "".join(chr(c) for c in p.outputs)
    last_output = output.split("\n\n\n")[-1]
    return last_output.strip()
Example #2
0
def explore(p: IntcodeProgram, grid: dict, pos: complex = 0 + 0j):
    for command in range(1, 5):
        delta = MOVES[command]
        new_pos = pos + delta
        if new_pos not in grid:
            # try to move to new_pos
            p.write_input(command)
            p.run()
            # get result of trying to move
            status = p.outputs.popleft()
            grid[new_pos] = DISPLAY_CHAR_MAP[status]
            if status != 0:  # if we moved
                explore(p, grid, new_pos)  # explore from the new pos
                # after we're done exploring the branch, go back one step, so we can try the next move fom pos
                p.write_input(OPPOSITES[command])
                p.run()
                # pop the output from returning to where we came from. we know this is not a wall
                assert p.outputs.popleft() != 0
Example #3
0
 def is_affected(x, y):
     p = IntcodeProgram(program)
     p.write_input(x)
     p.write_input(y)
     p.run()
     return bool(p.outputs[-1])