Пример #1
0
def solution2(data: List[int]) -> int:
    computer = IntcodeComputer(data)
    instructions = [
        "NOT C J\n",
        "AND D J\n",
        "NOT H T\n",
        "NOT T T\n",
        "OR E T\n",
        "AND T J\n",
        "NOT A T\n",
        "OR T J\n",
        "NOT B T\n",
        "NOT T T\n",
        "OR E T\n",
        "NOT T T\n",
        "OR T J\n",
    ]
    for instruction in instructions:
        computer.send_long(instruction)
    computer.send_long("RUN\n")
    computer.run_until_blocked()
    while computer.has_output():
        value = computer.read()
        try:
            print(chr(value), end="")
        except ValueError:
            return value
Пример #2
0
def get_result(data: List[int], x: int, y: int) -> int:
    computer = IntcodeComputer(data)
    computer.send(x)
    computer.send(y)
    computer.run_until_blocked()
    assert computer.has_output()
    result = computer.read()
    return result
Пример #3
0
def solution2(data: List[int]) -> int:
    orig_data = data[:]
    graph = create_graph(data)

    for current_location, representation in graph.items():
        if representation in ("<", ">", "^", "v"):
            break

    current_row, current_col = current_location
    delta_row, delta_col = -1, 0

    left_map = {
        (-1, 0): (0, -1),
        (0, -1): (1, 0),
        (1, 0): (0, 1),
        (0, 1): (-1, 0),
    }
    right_map = {
        (-1, 0): (0, 1),
        (0, 1): (1, 0),
        (1, 0): (0, -1),
        (0, -1): (-1, 0),
    }

    path = []
    while True:
        steps_forward = 0
        while True:
            next_location = current_row + delta_row, current_col + delta_col
            if not graph.get(next_location) == "#":
                break
            steps_forward += 1
            current_row, current_col = next_location

        if steps_forward:
            last_turn = path.pop()
            path.append(f"{last_turn}{steps_forward}")

        left_row, left_col = left_map[(delta_row, delta_col)]
        right_row, right_col = right_map[(delta_row, delta_col)]

        left_location = graph.get((current_row + left_row, current_col + left_col))
        right_location = graph.get((current_row + right_row, current_col + right_col))

        if left_location == "#" and right_location == "#":
            assert False, "Should not happen..."
        elif left_location == "#":
            path.append("L")
            delta_row, delta_col = left_row, left_col
        elif right_location == "#":
            path.append("R")
            delta_row, delta_col = right_row, right_col
        else:
            break

    routine, a, b, c = find_groups(path)
    data = orig_data[:]
    data[0] = 2

    computer = IntcodeComputer(data)
    for char in chain(routine, a, b, c):
        computer.send(ord(char))
    computer.send(ord("n"))
    computer.send(ord("\n"))
    computer.run_until_blocked()
    result = None
    while computer.has_output():
        result = computer.read()
    return result