Day 12: Rain Risk

Input: list of actions Output: position, Manhattan distance

Classes:

Orientation(value)

Represent the facing orientation of the ship.

ShipPosition(horizontal, vertical, orientation)

Represent the current ferry position.

Functions:

parse_input(puzzle_input)

Split the puzzle input along the newlines.

update_position(current_position, move)

Execute a single move on current_position and return a new position.

solve(puzzle_input)

Execute the instructions and return the final ship's position.

class Orientation(value)[source]

Represent the facing orientation of the ship.

Attributes:

EAST

SOUTH

WEST

NORTH

EAST = 0
SOUTH = 1
WEST = 2
NORTH = 3
class ShipPosition(horizontal: int, vertical: int, orientation: Orientation)[source]

Represent the current ferry position.

Attributes:

horizontal

Position of the ship in horizontal direction

vertical

Position of the ship in vertical direction

orientation

Orientation of the ship

Methods:

__repr__()

Return repr(self).

__eq__(other)

Return self==value.

__init__(horizontal, vertical, orientation)

horizontal: int

Position of the ship in horizontal direction

vertical: int

Position of the ship in vertical direction

orientation: Orientation

Orientation of the ship

__repr__() str[source]

Return repr(self).

__eq__(other)

Return self==value.

__init__(horizontal: int, vertical: int, orientation: Orientation) None
parse_input(puzzle_input: str) Lines[source]

Split the puzzle input along the newlines.

Requires
  • re.fullmatch(r"[NSEWLRF][0-9]+(n[NSEWLRF][0-9]+)*", puzzle_input)

Ensures
  • "n".join(result) == puzzle_input

update_position(current_position: ShipPosition, move: str) ShipPosition[source]

Execute a single move on current_position and return a new position.

Requires
  • not (move[0] == "L" or move[0] == "R")
    or int(move[1:]) in [0, 90, 180, 270, 360]
    
  • re.match(r"^[NSEWLRF][0-9]+$", move)

solve(puzzle_input: str) ShipPosition[source]

Execute the instructions and return the final ship’s position.

Requires
  • re.match(
        r"^(([NSEWF][0-9]+)|([LR](0|90|180|270|360)))"
        r"(\n(([NSEWF][0-9]+)|([LR](0|90|180|270|360))))*\Z",
        puzzle_input,
    )