day20.lib package

Submodules

day20.lib.classes module

Classes for day20.

class day20.lib.classes.BaseModule(name: str, outputs: list[str])[source]

Bases: ABC

Abstract base module.

add_to_graph(dot: Digraph) None[source]

Adds edges only to the graph. inheritors need to handle their repr.

arrow_color() str[source]

Return arrow color for graphviz.

handle_pulse(input: str, pulse: Pulse) list[PulseTarget][source]

Keep track of lows/highs through all modules.

abstract is_initial_state() bool[source]

Returns if the module is in the initial state.

name: str
num_high: int = 0
num_low: int = 0
outputs: list[str]
class day20.lib.classes.BroadcastModule(name: str, outputs: list[str])[source]

Bases: BaseModule

Broadcasts to all outputs.

add_to_graph(dot: Digraph) None[source]

Add node to graphviz digraph.

handle_pulse(input: str, pulse: Pulse) list[PulseTarget][source]

Broadcasts to all outputs immediately.

is_initial_state() bool[source]

Always true.

class day20.lib.classes.ConjunctionModule(name: str, outputs: list[str])[source]

Bases: BaseModule

Keeps track of all inputs.

Changes internal state, then sends high/low based on internal state

add_to_graph(dot: Digraph) None[source]

Add this module to a GraphViz Digraph.

arrow_color() str[source]

Returns red.

current_count() int[source]

Returns current count of inputs that sent high.

handle_pulse(input: str, pulse: Pulse) list[PulseTarget][source]

Store pulse, then send based on current state.

If all our values are high, we send low to all our outputs. Otherwise, we send LOW to all our outputs.

inputs: dict[str, Pulse]
is_initial_state() bool[source]

Returns True if all our inputs are LOW.

set_inputs(inputs: list[str]) None[source]

Sets our list of input modules.

Initializes their values to Low.

class day20.lib.classes.FlipFlopModule(name: str, outputs: list[str], state: Pulse = Pulse.LOW)[source]

Bases: BaseModule

If we receive HIGH, we are a sink (do nothing).

If we receive LOW, flip our current value and send it to everyone

add_to_graph(dot: Digraph) None[source]

Adds ourselves to a graphviz digraph.

handle_pulse(input: str, pulse: Pulse) list[PulseTarget][source]

Handle pulse by forwarding if we receive low.

is_initial_state() bool[source]

Returns true if we are in our initial state.

state: Pulse = False
class day20.lib.classes.LoopCounter(target_loop_count: int)[source]

Bases: object

Keeps track of loop lengths.

add_result(loop_name: str, value: int) None[source]

Adds a result to our loop_count.

If we already had a loop with that name, we ignore it.

property finished: bool

Returns True if our loop_lengths are equal to our target.

loop_lengths: dict[str, int]
property num_results: int

Returns number of loop_lenghts submitted.

target_loop_count: int
class day20.lib.classes.MappingModule(name: str, outputs: list[str])[source]

Bases: object

map to a list of outputs.

name: str
outputs: list[str]
class day20.lib.classes.ModuleGroups(head: BroadcastModule, loops: list[list[BaseModule]], loop_tails: list[ConjunctionModule], penultimate: ConjunctionModule, sink: SinkModule)[source]

Bases: object

A group of modules for part2.

all_nodes: list[BaseModule]
head: BroadcastModule
loop_tails: list[ConjunctionModule]
loops: list[list[BaseModule]]
penultimate: ConjunctionModule
sink: SinkModule
class day20.lib.classes.Pulse(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Flag

Simple True/False enum.

HIGH = True
LOW = False
class day20.lib.classes.PulseTarget(pulse: Pulse, src: str, target: str)[source]

Bases: object

A pulse(low/high) from src to dest.

pulse: Pulse
src: str
target: str
class day20.lib.classes.SinkModule(name: str, outputs: list[str])[source]

Bases: BaseModule

Sink module, gets something but sends it no where else.

add_to_graph(dot: Digraph) None[source]

Adds this node to the graph.

handle_pulse(input: str, pulse: Pulse) list[PulseTarget][source]

Always eats inputs and never sends onwards.

is_initial_state() bool[source]

Always true.

day20.lib.parsers module

Day20 parsers.

day20.lib.parsers.finalize_modules(modules: list[BaseModule]) list[BaseModule][source]

Finalize construction of all modules.

For each module, calculate its inputs. Then inject the inputs into our conjunction modules Modifies modules inplace, and returns it

day20.lib.parsers.get_modules(filename: str) list[BaseModule][source]

Opens a file and returns all the modules.

Parameters:

filename (str) – name of file to open

Returns:

list of modules.

Return type:

list[BaseModule]

day20.lib.parsers.parse_line(line: str) BaseModule[source]

Parses a line into a BaseModule.

e.g. %a -> inv, con.

Module contents

Library modules for day20.