day17.lib package

Submodules

day17.lib.classes module

classes for day 17.

class day17.lib.classes.SolutionCache(num_rows: int, num_cols: int, cache_min: int, cache_max: int)[source]

Bases: object

2d array of tilecaches.

add_solution(step: Step) bool[source]

Adds solution to cache returns whether an improvement was made.

cache: list[list[TileCache]]
class day17.lib.classes.Step(total_cost: int, row: int, col: int, direction: Direction, consecutive_steps: int, src_step: Step | None)[source]

Bases: object

Represents one “step”, which could be a multi-step.

col: int
consecutive_steps: int
direction: Direction
row: int
src_step: Step | None
total_cost: int
class day17.lib.classes.TileCache(cache_min: int, cache_max: int)[source]

Bases: object

A cache of shortest routes to a tile from each direction.

cache: dict[Direction, list[Step | None]]
cache_max: int
cache_min: int
class day17.lib.classes.WorldPart1(costs: list[list[int]])[source]

Bases: object

World for part1.

costs: list[list[int]]
create_step(step: Step, direction: Direction) Step | None[source]

Create step from previous step and a given direction.

Returns None if the step is invalid or suboptimal

is_oob(row: int, col: int) bool[source]

Returns if we are out of bounds.

Parameters:
  • row (int) – row to check

  • col (int) – col to check

Returns:

if we are out of bounds.

Return type:

bool

num_cols: int
num_rows: int
solve() Step[source]

Solve using dynamic programming.

Returns final step which contains src steps; so we have the entire path

class day17.lib.classes.WorldPart2(costs: list[list[int]])[source]

Bases: WorldPart1

Extension of part1 with a few overrides.

create_step(step: Step, direction: Direction) Step | None[source]

Create step from previous step and a given direction.

solve() Step[source]

Solve using DP.

Returns final step which contains src steps; so we have the entire path

day17.lib.direction module

Direction class.

class day17.lib.direction.Direction(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: IntEnum

Simple direction enum.

EAST = 1
NORTH = 0
SOUTH = 2
WEST = 3
offset(row: int, col: int) tuple[int, int][source]

Offset a position by our direction.

offset_list(row: int, col: int, size: int = 4) list[tuple[int, int]][source]

Offset a position by our direction and size vector.

opposite() Direction[source]

Return opposite direction W``<->``E S``<->``N.

day17.lib.parsers module

parse input file.

day17.lib.parsers.get_input(path: str) list[list[int]][source]

Convert input into world dataclass.

Module contents

library modules for day 17.