santa_helpers package

Submodules

santa_helpers.distances module

santa_helpers.distances.manhattan(p1: Tuple[int, ...], p2: Tuple[int, ...]) int[source]

Calculate Manhattan distance between two points.

santa_helpers.neighbors module

santa_helpers.neighbors.is_point_in_range(p, p_min=None, p_max=None) bool[source]

Check if point lays between two other points.

Args:

p: tuple (x, y) p_min (optional): min border point p_max (optional): max border point

Returns:

True if p_max >= p >= p_min

santa_helpers.neighbors.neighbors(p, n=4, p_min=None, p_max=None)[source]

Point neighbor generator.

Args:

p: tuple (x, y) n: int 4 (no diagonal) or 8 (with diagonal) p_min (optional): min grid point, if not given infinite p_max (optional): max grid point, if not given infinite

Yields:

point (x, y)

santa_helpers.parse module

santa_helpers.parse.parse_grid_to_dict(data: str) dict[source]

Parse grid given as a string to dictionary. k: coordinates (x, y) v: value

Example:

X.O => { (0, 0): ‘X’, (1, 0): ‘.’, (2, 0): ‘O’, … (0, 1): ‘.’, (1, 1): ‘.’, (2, 1): ‘.’, ..O (0, 2): ‘.’, (1, 2): ‘.’, (2, 2): ‘O’, }

santa_helpers.paths module

santa_helpers.paths.get_direction(ch: str) Tuple[int, int][source]

Coordinates point for direction

Args:

ch: str - direction as a single letter UDLR or NEWS

Returns:

tuple (x, y) - direction coordinates. E.g.:

N -> (0, 1) # north S -> (0, -1) # south L -> (-1, 0) # left

Raises KeyError:

if direction char not in allowed directions.

santa_helpers.paths.get_target_point(start: Tuple[int, int], steps: str) Tuple[int, int][source]

Coordinates of target point based on start point and steps.

Args:

start: tuple (x, y) - coordinates of the starting point. steps: string e.g U15 - contains direction (U D L R or N E S W)

and number of steps.

Directions - two direction systems are possible:
Relative orientations:

U - Up -> (0, 1) D - Down -> (0, -1) L - Left -> (-1, 0) R - Right -> (1, 0)

Geographical directions:

N - North -> (0, 1) E - East -> (1, 0) S - South -> (0, -1) W - West -> (-1, 0)

Returns:

tuple (x, y) - coordinates of the target point.

santa_helpers.paths.path_points(start, steps)[source]

Generate coordinates of each path point based on start point and steps.

Args:

start: tuple (x, y) - coordinates of the starting point. steps: string e.g U15 - contains direction (UDLR or NESW)

and number of steps.

Directions - two direction systems are possible:
Relative orientations:

U - Up -> (0, 1) D - Down -> (0, -1) L - Left -> (-1, 0) R - Right -> (1, 0)

Geographical directions:

N - North -> (0, 1) E - East -> (1, 0) S - South -> (0, -1) W - West -> (-1, 0)

Yields:

tuple (x, y) - point.

Module contents

Top-level package for santa-helpers.