Day 23: Crab Cups

Classes:

Cup(label[, next_cup])

Represent a cup with a label and the cup next to it clockwise.

CupCircle()

Represent a circle of cups as a circular linked list.

Functions:

cup_circle_to_str(cup_circle)

Stringify the labels of the cup_circle, starting from the current cup.

initialize_cups(cup_labels)

Create a CupCircle from cup_labels.

crab_move(cup_circle)

Perform one move by the crab.

solve_100_steps(cup_labels)

Solve the problem for 100 crab moves.

class Cup(label: int, next_cup: Optional[Cup] = None)[source]

Represent a cup with a label and the cup next to it clockwise.

Methods:

__init__(label[, next_cup])

Initialize with the given values.

Attributes:

label

label of the cup

next_cup

the next cup clockwise

__init__(label: int, next_cup: Optional[Cup] = None) None[source]

Initialize with the given values.

label: int

label of the cup

next_cup: Cup

the next cup clockwise

class CupCircle[source]

Represent a circle of cups as a circular linked list.

Methods:

__init__()

Initialize the circle as an empty circular linked list.

add_new_cup(label)

Add a new cup to the circle with label.

__repr__()

Represent the circle as its labels.

__eq__(other)

Return whether two circles are identical in the labels of the circle.

__len__()

Return the number of cups in the circle.

Attributes:

current_cup

the cup from which each new move starts

__init__() None[source]

Initialize the circle as an empty circular linked list.

current_cup: Optional[Cup]

the cup from which each new move starts

add_new_cup(label: int) None[source]

Add a new cup to the circle with label.

The cup is added next to the current cup counter-clockwise.

Requires
  • label >= 0

  • not self._is_label_in_circle(label)

__repr__() str[source]

Represent the circle as its labels.

__eq__(other: object) bool[source]

Return whether two circles are identical in the labels of the circle.

__len__() int[source]

Return the number of cups in the circle.

cup_circle_to_str(cup_circle: CupCircle) str[source]

Stringify the labels of the cup_circle, starting from the current cup.

Ensures
  • cup_circle == initialize_cups(result)

initialize_cups(cup_labels: str) CupCircle[source]

Create a CupCircle from cup_labels.

Requires
  • len(set(cup_labels)) == len(cup_labels)

  • NUMBER_RE.fullmatch(cup_labels)

Ensures
  • cup_labels == cup_circle_to_str(result)

crab_move(cup_circle: CupCircle) None[source]

Perform one move by the crab.

Requires
  • len(cup_circle) >= 5

OLD
  • .len_cup_circle = len(cup_circle)

Ensures
  • OLD.len_cup_circle == len(cup_circle)

solve_100_steps(cup_labels: str) CupCircle[source]

Solve the problem for 100 crab moves.

Requires
  • len(set(cup_labels)) == len(cup_labels)

  • NUMBER_RE.fullmatch(cup_labels)

  • len(cup_labels) >= 5

Ensures
  • len(cup_labels) == len(result)