Day 23: Crab Cups¶
Classes:
|
Represent a cup with a label and the cup next to it clockwise. |
Represent a circle of cups as a circular linked list. |
Functions:
|
Stringify the labels of the |
|
Create a CupCircle from |
|
Perform one move by the crab. |
|
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 of the 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
- 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:
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)
- 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)