Exemplo n.º 1
0
class Y2019D21(object):
    def __init__(self, file_name):
        self.computer = Intcode(file_name)

    def part1(self):
        result = self._run("NOT A J\n"
                           "NOT B T\n"
                           "OR T J\n"
                           "NOT C T\n"
                           "OR T J\n"
                           "AND D J\n"
                           "WALK\n")

        print("Part 1:", result)

    def part2(self):
        result = self._run("NOT H T\n"
                           "OR C T\n"
                           "AND B T\n"
                           "AND A T\n"
                           "NOT T J\n"
                           "AND D J\n"
                           "RUN\n")

        print("Part 2:", result)

    def _run(self, *program: str):
        self.computer.reset()
        self.computer.run()
        self.computer.output_str()
        for command in program:
            self.computer.input_str(command)

        result = self.computer.output_str()

        if "Didn't make it across" in result:
            print(result)
            return -1
        else:
            return self.computer.output()
Exemplo n.º 2
0
class Y2019D17(object):
    def __init__(self, file_name):
        self.computer = Intcode(file_name)

    def part1(self):
        self.computer.reset()
        self.computer.run()
        map_string = self.computer.output_str()
        grid = Grid.from_str(map_string)

        result = 0
        scaffolding = grid.find('#')

        for scaffold in scaffolding:
            intersecting = all(grid[neighbor] == '#'
                               for neighbor in scaffold.neighbors())

            if intersecting:
                result += scaffold.x * scaffold.y

        print("Part 1:", result)

    def part2(self):
        self.computer.reset()
        self.computer.ram[0] = 2
        self.computer.run()

        self.computer.output_str()
        self.computer.input_str("B,C,B,C,A,B,C,A,B,A\n")
        self.computer.output_str()
        self.computer.input_str("R,6,L,8,L,10,R,6\n")
        self.computer.output_str()
        self.computer.input_str("R,6,L,6,L,10\n")
        self.computer.output_str()
        self.computer.input_str("L,8,L,6,L,10,L,6\n")
        self.computer.output_str()
        self.computer.input_str("n\n")
        map_string = self.computer.output_str()
        grid = Grid.from_str(map_string)
        result = self.computer.output()

        print("Part 2:", result)
Exemplo n.º 3
0
class Y2019D25(object):
    def __init__(self, file_name):
        self.computer = Intcode(file_name)
        self._do_not_take = [
            "escape pod",  # "You're launched into space! Bye!"
            "giant electromagnet",  # "The giant electromagnet is stuck to you.  You can't move!!"
            "infinite loop",  # Literally halts the program in an infinite loop
            "molten lava",  # "The molten lava is way too hot! You melt!"
            "photons",  # "It is suddenly completely dark! You are eaten by a Grue!"
            "mug",  # Too heavy, by itself
            "prime number",  # Too heavy, by itself
            "weather machine",  # Too heavy, by itself
            "festive hat",  # Don't take this and our weight is correct with other items
        ]

    def part1(self):
        self.computer.reset()
        self.computer.run()

        back_moves: List[str] = []
        path_from_security: Optional[List[str]] = None
        seen: Set[Tuple[str, str]] = set()
        taken: Set[str] = set()

        while True:
            output = self.computer.output_str()
            # print(output)
            location = DungeonLocation.read(output)

            if location.name == "Security Checkpoint":
                path_from_security = back_moves.copy()
                previous_move = back_moves.pop()
                # print(f"Going back {previous_move}")
                self.computer.input_str(f"{previous_move}\n")
                continue

            for item in location.items:
                if item not in self._do_not_take:
                    taken.add(item)
                    input_str = f"take {item}\n"
                    # print(input_str)
                    self.computer.input_str(input_str)
                    response = self.computer.output_str()
                    # print(response)

            moved_through_a_door = False
            opposite_direction = None
            for direction in location.doors_here_lead:
                back_direction = back_moves[-1] if len(
                    back_moves) > 0 else None
                if back_direction == direction:
                    continue

                movement = (location.name, direction)
                if movement in seen:
                    continue

                if direction == "north":
                    opposite_direction = "south"
                elif direction == "south":
                    opposite_direction = "north"
                elif direction == "east":
                    opposite_direction = "west"
                elif direction == "west":
                    opposite_direction = "east"

                seen.add(movement)
                back_moves.append(opposite_direction)
                moved_through_a_door = True

                # print(f"Going {direction}")
                self.computer.input_str(f"{direction}\n")

                break

            if not moved_through_a_door:
                if len(back_moves) > 0:
                    previous_move = back_moves.pop()
                    # print(f"Going back {previous_move}")
                    self.computer.input_str(f"{previous_move}\n")
                else:
                    break

        for movement in path_from_security:
            if movement == "north":
                movement = "south"
            elif movement == "south":
                movement = "north"
            elif movement == "east":
                movement = "west"
            elif movement == "west":
                movement = "east"

            self.computer.input_str(f"{movement}\n")
            self.computer.output_str()

        self.computer.input_str("east\n")
        output = self.computer.output_str()
        result = re.search(r"(\d+)", output).group(1)

        print("Part 1:", result)

    def part2(self):
        pass