Exemplo n.º 1
0
    def part2(self):
        waypoint = Coordinate(10, 1)
        ship = Coordinate(0, 0)

        for line in self.lines:
            value = int(line[1:])
            command = line[0]
            if command == "N":
                waypoint = waypoint.up(value)
            elif command == "S":
                waypoint = waypoint.down(value)
            elif command == "E":
                waypoint = waypoint.right(value)
            elif command == "W":
                waypoint = waypoint.left(value)
            elif command == "L":
                waypoint = waypoint.ccw_around(ship, value // 90)
            elif command == "R":
                waypoint = waypoint.cw_around(ship, value // 90)
            elif command == "F":
                diff_coordinate = waypoint - ship
                ship = ship + (diff_coordinate * value)
                waypoint = ship + diff_coordinate

        result = ship.manhattan(Coordinate(0, 0))

        print("Part 2:", result)
Exemplo n.º 2
0
 def _neighbors(coordinate: Coordinate) -> Set[Coordinate]:
     return {
         coordinate.left().up(),
         coordinate.up(),
         coordinate.down(),
         coordinate.right().down(),
         coordinate.left(),
         coordinate.right()
     }
Exemplo n.º 3
0
class Y2017D11(object):
    def __init__(self, file_name):
        line = Input(file_name).line()

        self.furthest_distance = 0
        self.coordinate = Coordinate(0, 0)
        for step in line.split(","):
            if step == 'n':
                self.coordinate = self.coordinate.up()
            elif step == 's':
                self.coordinate = self.coordinate.down()
            elif step == 'ne':
                self.coordinate = self.coordinate.right()
            elif step == 'sw':
                self.coordinate = self.coordinate.left()
            elif step == 'nw':
                self.coordinate = self.coordinate.left().up()
            elif step == 'se':
                self.coordinate = self.coordinate.right().down()

            self.furthest_distance = max(self.furthest_distance,
                                         self._distance(self.coordinate))

    @staticmethod
    def _distance(coordinate: Coordinate) -> int:
        if (coordinate.x < 0 < coordinate.y) or (coordinate.x > 0 >
                                                 coordinate.y):
            return max(abs(coordinate.x), abs(coordinate.y))
        return abs(coordinate.x + coordinate.y)

    def part1(self):
        result = self._distance(self.coordinate)

        print("Part 1:", result)

    def part2(self):
        result = self.furthest_distance

        print("Part 2:", result)