Day 24: Lobby Layout

Classes:

Cell(x, y, z)

Represent a hexagonal cell of the layout using cube coordinates.

Direction(value)

Enumerate the possible directions from a hexagonal cell.

Functions:

cell_as_tuple(cell)

Convert the cell into a tuple of x, y and z coordinates.

next_cell(cell, direction)

Retrieve the next cell starting from cell in the direction.

follow_directions(start, directions)

Walk the directions from the start.

parse_line(line)

Parse the input line.

stringify_directions(directions)

Represent the directions as a concatenated text.

count_flips(plan)

Count how many cells had to flip for the given plan.

Data:

VALUE_TO_DIRECTION

Map string literal of Direction 🠒 Direction.

DIRECTIONS_RE

Express a list of directions

ONE_DIRECTION_RE

Express a single direction

class Cell(x: int, y: int, z: int)[source]

Represent a hexagonal cell of the layout using cube coordinates.

See https://www.redblobgames.com/grids/hexagons/.

Methods:

__init__(x, y, z)

Initialize with the given values.

Attributes:

x

X-coordinate (south-east, north-west)

y

Y-coordinate (south-west, north-east)

z

Z-coordinate (east, west)

__init__(x: int, y: int, z: int) None[source]

Initialize with the given values.

Requires
  • x + y + z == 0

x: Final[int]

X-coordinate (south-east, north-west)

y: Final[int]

Y-coordinate (south-west, north-east)

z: Final[int]

Z-coordinate (east, west)

cell_as_tuple(cell: Cell) Tuple[int, int, int][source]

Convert the cell into a tuple of x, y and z coordinates.

class Direction(value)[source]

Enumerate the possible directions from a hexagonal cell.

Attributes:

EAST

SOUTH_EAST

SOUTH_WEST

WEST

NORTH_WEST

NORTH_EAST

EAST = 'e'
SOUTH_EAST = 'se'
SOUTH_WEST = 'sw'
WEST = 'w'
NORTH_WEST = 'nw'
NORTH_EAST = 'ne'
VALUE_TO_DIRECTION = {'e': Direction.EAST, 'ne': Direction.NORTH_EAST, 'nw': Direction.NORTH_WEST, 'se': Direction.SOUTH_EAST, 'sw': Direction.SOUTH_WEST, 'w': Direction.WEST}

Map string literal of Direction 🠒 Direction.

next_cell(cell: Cell, direction: Direction) Cell[source]

Retrieve the next cell starting from cell in the direction.

follow_directions(start: Cell, directions: List[Direction]) Cell[source]

Walk the directions from the start.

Returns

The final Cell of the journey

DIRECTIONS_RE = re.compile('^(se|sw|nw|ne|w|e)+\\Z')

Express a list of directions

ONE_DIRECTION_RE = re.compile('(se|sw|nw|ne|w|e)')

Express a single direction

parse_line(line: str) List[Direction][source]

Parse the input line.

Requires
  • len(line) > 0DIRECTIONS_RE.match(line)

Ensures
  • stringify_directions(result) == line

  • len(line) == 0len(result) == 0

stringify_directions(directions: List[Direction]) str[source]

Represent the directions as a concatenated text.

Ensures
  • parse_line(result) == directions

count_flips(plan: List[List[Direction]]) int[source]

Count how many cells had to flip for the given plan.

The plan consists of different journeys, all starting from the cell zero.