Day 7: Handy Haversacks¶
Functions:
|
Parse the expression representing a bag. |
|
Parse the rule from the text given as a single line. |
|
Parse the rules from the given |
|
Check whether the |
|
Compute the set of known containers. |
|
Parse the rules given as |
|
Execute the main routine. |
- parse_bagexpr(text: str) Tuple[int, str] [source]¶
Parse the expression representing a bag.
- Returns
number of units, kind
- Requires
re.fullmatch(r"d+ .* bags?", text)
- parse_rule(text: str) Tuple[str, Dict[str, int]] [source]¶
Parse the rule from the text given as a single line.
- Returns
kind, contents
- Requires
"n" not in text
- directly_contains(container: str, contained: str, rules: Dict[str, Dict[str, int]]) bool [source]¶
Check whether the
contained
is contained in thecontainer
.
- containers(kind: str, rules: Dict[str, Dict[str, int]]) Set[str] [source]¶
Compute the set of known containers.
- Ensures
all( not directly_contains(non_container, container, rules) for non_container in (rules.keys() - result) for container in result )
(Nothing else contains anything in the result)
kind in result
(given kind is in the result)