day19.lib package

Submodules

day19.lib.classes module

well defined classes for day19.

class day19.lib.classes.Comparator(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

Well defined comparators < and >.

GreaterThan = '>'
LessThan = '<'
class day19.lib.classes.Component(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: StrEnum

A well defined component inside a part.

A = 'a'
M = 'm'
S = 's'
X = 'x'
class day19.lib.classes.Condition(component: Component, sign: Comparator, value: int)[source]

Bases: object

A condition for a part to succeed/fail.

component: Component
process_part(part: Part) bool[source]

Checks a part to see if it matches our condition.

Parameters:

part (Part) – part to check

Raises:

AssertionError – if component/sign are unsupported

Returns:

True if the part passes our condition.

Return type:

bool

process_part_range(part_range: PartRange) tuple[PartRange | None, PartRange | None][source]

Splits a part range based on success/fail.

Parameters:

part_range (PartRange) – Partrange to check.

Raises:

AssertionError – If we have an unknown comparator

Returns:

successful part range, failed partrange

Return type:

tuple[Optional[PartRange], Optional[PartRange]]

sign: Comparator
value: int
class day19.lib.classes.Part(x: int, m: int, a: int, s: int)[source]

Bases: object

Well defined part with x,m,a,s values.

a: int
clone_modify(component: Component, value: int) Part[source]

Clones this part and modifies one component.

Parameters:
  • component (Component) – component to change

  • value (int) – new value of component

Returns:

clone of this part with one component changed.

Return type:

Part

get_value(component: Component) int[source]

Returns value of a component inside this part.

m: int
property rating: int

Returns rating of part (sum of xmas).

s: int
x: int
class day19.lib.classes.PartRange(min_values: Part, max_values: Part)[source]

Bases: object

A range of parts (min/max) based on component values.

max_values: Part
min_values: Part
size() int[source]

Returns the size of the partrange.

split(component: Component, split_value: int) tuple[PartRange | None, PartRange | None][source]

Split a partrange in two, using a chosen component and splitvalue.

In the case that our range falls on one whole side, we return None. E.g. range = 0-100; split == 200 -> return [(0-100), None] range = 100-200; split == 50 -> return [None, (100-200)] range = 100-200, split == 150 -> return [(100-150), (150-200)]

class day19.lib.classes.PartRangeDest(part_range: PartRange, destination: str)[source]

Bases: object

Combinatoin of partrange and a destination workflow.

destination: str
part_range: PartRange
class day19.lib.classes.Rule(destination: str, condition: Condition | None = None)[source]

Bases: object

A Rule consists of a condition + destination.

condition: Condition | None = None
destination: str
process_part(part: Part) str | None[source]

Processes a part.

Returns next workflow if successful, or None if we failed this rule

process_part_range(part_range: PartRange) tuple[PartRangeDest | None, PartRange | None][source]

Processes a PartRange.

Returns next workflow and partrange for succeeding parts. Returns the remainder partrange that failed.

Parameters:

part_range (PartRange) – base partrange.

Returns:

success, fail

Return type:

tuple[Optional[PartRangeDest], Optional[PartRange]]

class day19.lib.classes.Workflow(name: str, rules: list[Rule])[source]

Bases: object

The name of the workflow + a bunch of rules for parts to follow.

name: str
process_part(part: Part) str[source]

Processes a part, returns the next workflow.

process_part_range(part_range: PartRange) list[PartRangeDest][source]

Follow rule list, splitting off PartRanges.

Each success has to branch off. Each failure continues down the chain.

rules: list[Rule]

day19.lib.parsers module

functions to create classes from pure text.

day19.lib.parsers.parse_condition_string(cond_string: str) Condition[source]

e.g. a<2006.

day19.lib.parsers.parse_part_string(part_string: str) Part[source]

Returns a part from a string representation.

e.g. {x=787,m=2655,a=1222,s=2876}\n

day19.lib.parsers.parse_rule_string(rule_string: str) Rule[source]

e.g. a<2006:qkq or rfg.

day19.lib.parsers.parse_workflow_string(workflow_string: str) Workflow[source]

Returns a workflow from a string representation.

e.g. px{a<2006:qkq,m>2090:A,rfg}\n

Module contents

Library modules for part19.