Day 16: Ticket Translation¶
Classes:
|
Represent a rule which is not constrained by pre-conditions. |
|
Represent a rule for the ticket field. |
Functions:
|
Check whether the |
|
Parse the |
|
Parse the nearby tickets from |
|
Select the invalid fields from a |
|
Select the invalid fields accross all |
|
Compute the error rate as sum of the invalid values. |
- class RuleParsing(identifier: str, ranges: List[Tuple[int, int]])[source]¶
Represent a rule which is not constrained by pre-conditions.
Methods:
__init__
(identifier, ranges)Initialize with the given values.
Attributes:
Identifier of the field
Valid range of values for the field
- __init__(identifier: str, ranges: List[Tuple[int, int]]) None [source]¶
Initialize with the given values.
- identifier: Final[str]¶
Identifier of the field
- ranges: Final[List[Tuple[int, int]]]¶
Valid range of values for the field
- class Rule(identifier: str, ranges: List[Tuple[int, int]])[source]¶
Represent a rule for the ticket field.
Methods:
__init__
(identifier, ranges)Initialize with the given values.
Attributes:
identifier of the field
acceptable ranges for the field
- __init__(identifier: str, ranges: List[Tuple[int, int]]) None [source]¶
Initialize with the given values.
- Requires
all(range[0] < range[1] for range in ranges)
len(identifier) > 0
- identifier: Final[str]¶
identifier of the field
- ranges: Final[List[Tuple[int, int]]]¶
acceptable ranges for the field
- parse_rules(lines: Lines) List[RuleParsing] [source]¶
Parse the
lines
into rules.While the parsed rules are syntactically correct, they have to be yet semantically verified.
- Requires
all(RULE_RE.match(line) for line in lines)
- parse_nearby_tickets(lines: List[str]) List[List[int]] [source]¶
Parse the nearby tickets from
lines
to list of field values.- Requires
all( re.match(r'^(0|[1-9][0-9]*)(,(0|[1-9][0-9]*))+\Z', line) for line in lines )
- invalid_fields(rules: List[Rule], ticket: List[int]) List[int] [source]¶
Select the invalid fields from a
ticket
according torules
.- Ensures
all( value in ticket for value in result )