コード例 #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 create_graph(data: List[int]) -> Dict[Tuple[int, int], str]:
    computer = IntcodeComputer(data)
    computer.run_until_blocked()

    graph = defaultdict(str)

    row = 0
    col = 0
    while computer.stdout:
        value = computer.read()
        if value == 10:  # Newline
            col = 0
            row += 1
            continue

        graph[(row, col)] = chr(value)
        col += 1

    return graph
コード例 #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
コード例 #5
0
def solution1(data: List[int]) -> int:
    computer = IntcodeComputer(data)
    computer.send(1)
    computer.run_until_blocked()
    return computer.read()
コード例 #6
0
def solution2(data: List[int]) -> object:
    computer = IntcodeComputer(data)
    computer.send(2)
    computer.run_until_blocked()
    return computer.read()
コード例 #7
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
コード例 #8
0
def solution(data: List[int]) -> int:
    os.system("clear")
    computer = IntcodeComputer(data)
    instructions = [
        "north",
        "take wreath",
        "east",
        "east",
        "east",
        "take weather machine",
        "west",
        "west",
        "west",
        "south",
        "south",
        "west",
        "take prime number",
        "west",
        "take astrolabe",
        "east",
        "east",
        "south",
        "take candy cane",
        "north",
        "north",
        "east",
        "take food ration",
        "south",
        "east",
        "south",
        "take hypercube",
        "east",
        "take space law space brochure",
        "north",
    ]

    # Gather all non-killing items and get to the security checkpoint
    for instruction in instructions:
        computer.send_long(instruction)

    items = [
        "candy cane",
        "wreath",
        "hypercube",
        "food ration",
        "weather machine",
        "space law space brochure",
        "prime number",
        "astrolabe",
    ]

    # Drop all items to begin iterations
    for item in items:
        computer.send_long(f"drop {item}")

    computer.run_until_blocked()
    computer.readlines()

    while not computer.halted:
        computer.run_until_blocked()
        for item_count in range(1, len(items) + 1):
            print(f"Trying combinations of {item_count} items")
            for combination in combinations(items, item_count):

                # Pick up all items in the combination
                for item in combination:
                    computer.send_long(f"take {item}")
                computer.run_until_blocked()
                computer.stdout.clear()

                # Make attempt
                computer.send_long("west")
                computer.run_until_blocked()
                lines = list(computer.readlines())

                # Check for success
                if not any("heavier" in line or "lighter" in line for line in lines):
                    return "\n".join(lines[8:])

                # Drop up all items in the combination
                for item in combination:
                    computer.send_long(f"drop {item}")
                computer.run_until_blocked()
                computer.stdout.clear()