TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/cj81499/advent-of-code/llms.txt
Use this file to discover all available pages before exploring further.
aoc_cj.util subpackage is a small but focused shared library that appears throughout the yearly solution modules. It exports helpers that address the patterns that recur most often in Advent of Code puzzles: navigating 2D and 3D grids, extracting numbers from messy input strings, checking primality, and running priority-queue (Dijkstra-style) searches. Rather than re-implementing these from scratch in each day module, solutions import them from aoc_cj.util to keep day files concise and readable.
Importing
You can import the namespace object or pull in individual names:Grid Helpers
AoC grid problems are frequently solved by representing the grid as adict[complex, T], where the real part of the key is the column (x) and the imaginary part is the row (y). For example, the cell at column 3, row 2 is the complex number 3 + 2j. This representation makes direction arithmetic trivial: moving east is +1, west is -1, north is -1j, south is +1j.
adj_4(p: complex) -> Generator[complex]
Yields the four cardinal neighbors (west, east, north, south) of a complex grid point.
adj_8(p: complex) -> Generator[complex]
Yields all eight neighbors of p, including the four diagonals.
adj_4 to enumerate valid next positions during the BFS walk:
adj_4 and adj_8 return generators, not lists. Wrap with list() if you
need to iterate more than once, but in most search loops a single-pass
generator is exactly what you want.Input Parsers
Puzzle inputs frequently embed numbers inside longer strings of prose or structured text.aoc_cj.util provides several create_regex_parser-generated helpers that extract those numbers in a single call.
create_regex_parser(pattern, converter) -> Callable
A factory function that creates a parser by compiling a regex pattern and mapping a converter function over every match. All the built-in parsers below are created with this factory:
ints(s: str) -> Generator[int]
Extracts all integers — including negative numbers — from s using the pattern r'-?\d+'.
positive_ints(s: str) -> Generator[int]
Extracts only non-negative integers (no leading minus sign) using r'\d+'. Not exported in __all__ — importable directly as from aoc_cj.util import positive_ints.
digits(s: str) -> Generator[int]
Extracts individual digits (0–9) one character at a time.
floats(s: str) -> Generator[float]
Extracts decimal floating-point numbers (e.g. 3.14, -2.5) using r'-?\d+\.\d+'.
parse_range(r: str) -> range
Parses a "start-stop" string (inclusive on both ends) into a Python range. Uses positive_ints internally. Not exported in __all__ — importable directly as from aoc_cj.util import parse_range.
parse_range returns an inclusive range: "3-7" maps to
range(3, 8) so that 7 in parse_range("3-7") is True. This matches the
convention used in most AoC range-overlap puzzles.Math Helpers
clamp(n: int, min_n: int, max_n: int) -> int
Clamps n to the closed interval [min_n, max_n]. Asserts that min_n <= max_n.
is_prime(n: int) -> bool
Trial-division primality test. Returns True if and only if n is a prime number.
6k ± 1 optimization: after ruling out multiples of 2 and 3, it only tests divisors of the form 6k − 1 and 6k + 1 up to √n, giving O(√n / 3) trial divisions.
Point3D
Point3D, defined in util/_point.py, is a frozen dataclass for three-dimensional integer coordinates. It is useful for 3D grid puzzles like 2022 Day 18 (lava droplet surface area).
Key members
| Member | Description |
|---|---|
Point3D(x, y, z) | Construct a point directly. Frozen — immutable and hashable. |
Point3D.parse(s) | Parse any string containing three integers, separated by any non-digit characters (e.g. "1,2,3" or "x=1 y=2 z=3"). |
point.adj() | Generator of the 6 face-adjacent neighbors (±x, ±y, ±z). |
Because
Point3D is frozen=True, instances are hashable and can be used
directly as keys in a dict or elements of a set — the natural
representation for 3D grids.PriorityQueue[T]
PriorityQueue, defined in util/_priority_queue.py, is a generic min-heap wrapper around Python’s heapq module. It avoids the need to push raw tuples and manually unpack them, and it works cleanly with items that are not themselves orderable (since priority is tracked separately).
Methods
| Method | Signature | Description |
|---|---|---|
__init__ | () -> None | Create an empty priority queue. |
push | (priority: int, item: T) -> None | Push item with the given integer priority. |
pop | () -> T | Pop and return the item with the lowest priority value. |
pop_with_priority | () -> tuple[T, int] | Pop and return (item, priority) together. |
__len__ | () -> int | Number of items currently in the queue. |
Usage example
Public API Summary
The following names are exported fromaoc_cj.util (listed in __all__):
| Export | Type | Description |
|---|---|---|
adj_4 | (complex) -> Generator[complex] | 4 cardinal neighbors of a complex grid point |
adj_8 | (complex) -> Generator[complex] | 8 neighbors (cardinal + diagonal) of a complex grid point |
clamp | (int, int, int) -> int | Clamp a value to [min_n, max_n] |
create_regex_parser | (pattern, converter) -> Callable | Factory that builds a regex-based parser |
digits | (str) -> Generator[int] | Extract individual digits from a string |
floats | (str) -> Generator[float] | Extract decimal floating-point numbers from a string |
ints | (str) -> Generator[int] | Extract all integers (including negative) from a string |
is_prime | (int) -> bool | Trial-division primality test |
Point3D | dataclass | Frozen 3D integer coordinate with .parse() and .adj() |
PriorityQueue[T] | class | Generic integer-priority min-heap |
positive_ints and parse_range are used throughout the codebase but are
not listed in __all__. They are still importable directly:
from aoc_cj.util import positive_ints, parse_range.