Esempio n. 1
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
Esempio n. 2
0
def solution1(data: List[int]) -> int:
    computer = IntcodeComputer(data)
    computer.send(1)
    while not computer.halted:
        computer.run()
    result = 0
    while result == 0:
        result = computer.read()
    return result
Esempio n. 3
0
def solution1(data: List[int]) -> int:
    best = float("-inf")
    for phase_setting in permutations(range(5), 5):
        result = 0
        for amplifier in phase_setting:
            computer = IntcodeComputer(data)
            computer.send(amplifier)
            computer.send(result)
            while not computer.halted:
                computer.run()
            result = computer.read()
        best = max(best, result)
    return best
Esempio n. 4
0
def solution2(data: List[int]) -> int:
    best = float("-inf")
    for phase_setting in permutations(range(5, 10), 5):
        computers = deque()
        for amplifier in phase_setting:
            computer = IntcodeComputer(data)
            computer.send(amplifier)
            computers.append(computer)

        result = 0
        i = 0
        while not all(computer.halted for computer in computers):
            computer = computers[i]
            if not computer.halted:
                computer.send(result)
                computer.run_until_blocked()
                result = computer.read()
            i += 1
            i %= 5
        best = max(best, result)
    return best
Esempio n. 5
0
def solution2(data: List[int]) -> int:
    computer = IntcodeComputer(data)
    computer.send(5)
    while not computer.halted:
        computer.run()
    return computer.read()
Esempio n. 6
0
def solution1(data: List[int]) -> int:
    computer = IntcodeComputer(data)
    computer.send(1)
    computer.run_until_blocked()
    return computer.read()
Esempio n. 7
0
def solution2(data: List[int]) -> object:
    computer = IntcodeComputer(data)
    computer.send(2)
    computer.run_until_blocked()
    return computer.read()
Esempio n. 8
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