day23.lib package

Submodules

day23.lib.classes module

Classes for part1.

class day23.lib.classes.Maze(data: list[list[str]])[source]

Bases: object

2d array of chars.

copy() Maze[source]

Copies us.

get_cell_branches(position: Position) int[source]

Returns how many branches come out of this tile.

grid: list[list[str]]
is_oob(position: Position) bool[source]

True if position is out of bounds.

num_cols: int
num_rows: int
class day23.lib.classes.Path[source]

Bases: object

A list of Positions.

add(position: Position) None[source]

Add a position to the path.

can_add(position: Position) bool[source]

Whether we can add a given position.

If we have already visited the position, we can’t

copy() Path[source]

Copy us.

flip() Path[source]

Flip a path by reversing the order.

last() Position[source]

Return last position in path.

Raises:

ValueError – if we have no positions.

Returns:

last position in path

Return type:

Position

nodes: set[Position]
overlay(maze: Maze) str[source]

Overlay us onto a maze, return a neat string.

Parameters:

maze (Maze) – maze to overlay.

Returns:

a nice string to print.

Return type:

str

route: list[Position]
class day23.lib.classes.Position(row: int, col: int)[source]

Bases: object

Simple 2d Vector.

col: int
copy_modify(row: int | None = None, col: int | None = None) Position[source]

Copies us and offsets by the given offset.

expand() list[Position][source]

Expand in 4 cardinal directions.

row: int
class day23.lib.classes.Solver1(maze: Maze, handle_hills: bool = True)[source]

Bases: object

Solver for part1.

expand_hill(position: Position, tile: str) list[Position][source]

Expand valid positions based on our current tile.

expand_path(path: Path) list[Path][source]

Expand path based on current path.

handle_hills: bool
maze: Maze
solve() list[Path][source]

Solve the maze, using bfs.

day23.lib.classes.generate_paths(path: Path, expansions: list[Position]) list[Path][source]

Given a path and valid expansions, (optionally) copies the path.

Returns a list of new paths. If there is only one expansion, modifies it in-place

day23.lib.classes2 module

part 2 solution.

class day23.lib.classes2.Edge(node1: int, node2: int, path: Path, length: int = 0)[source]

Bases: object

Edge class, representing a path between nodes.

flip() Edge[source]

Reverse a path.

length: int = 0
node1: int
node2: int
path: Path
class day23.lib.classes2.Node(name: int, position: ~day23.lib.classes.Position, edges: list[~day23.lib.classes2.Edge] = <factory>)[source]

Bases: object

Node representing a fork to another.

edges: list[Edge]
name: int
position: Position
class day23.lib.classes2.Solver2(maze: Maze)[source]

Bases: object

Solver for part 2.

build_nodes() list[Node][source]

Build nodes and edges on a copy of the maze.

static calculate_edges(start_node: Node, nodes: dict[Position, Node], maze: Maze) None[source]

Calculate edges of the maze.

Modifies the maze inplace, filling it in with #. Modifies the node and its connecting nodes by adding Edges

static expand_path(path: Path, maze: Maze) list[Path][source]

Expands a path, nuking that section of the maze using #.

static get_nodes(maze: Maze) dict[Position, Node][source]

Gets nodes and marks them on the given maze.

Note that the maze is modified in-place! Nodes are not populated with edges

input_maze: Maze
solve() int[source]

Solves the maze.

day23.lib.classes2.solve2(nodes: list[Node], current: int, destination: int, distance: int, seen: set[int], forks_remaining: int) int[source]

Solves a dfs by creating forking into multiprocessing.

day23.lib.classes2.solve2_helper(args: list[Any]) int[source]

ThreadPoolExecutor doesnt have starmap so we use a helper.

day23.lib.parsers module

Day23 parsers.

day23.lib.parsers.get_maze(path: str) Maze[source]

Parse input file and return wellformed maze.

Module contents

Library functions for day23.