Add gridutil and alter PYTHONPATH to ensure it can be imported

This commit is contained in:
akp 2023-12-13 12:38:26 +00:00
parent 58557e0496
commit d1186bb5e3
No known key found for this signature in database
GPG key ID: CF8D58F3DEB20755
4 changed files with 47 additions and 1 deletions

2
aoc
View file

@ -15,7 +15,7 @@ from glob import glob
CHALLENGES_DIR = "challenges"
SAMPLE_TEST_JSON = "{}"
RUNNERS = {
"py": ["python3"],
"py": ["./runners/py.sh"],
"go": ["go", "run"],
}

1
gridutil/__init__.py Normal file
View file

@ -0,0 +1 @@
from .grid import Coordinate, Grid, add_coords, parse

40
gridutil/grid.py Normal file
View file

@ -0,0 +1,40 @@
from typing import TypeVar, Callable, Optional
T = TypeVar("T")
Coordinate = tuple[int, int]
Grid = dict[Coordinate, T]
def add_coords(a: Coordinate, b: Coordinate) -> Coordinate:
xa, ya = a
xb, yb = b
return xa + xb, ya + yb
def parse(instr: str, filter_fn: Optional[Callable[[str], bool]] = None) -> Grid:
if filter is None:
filter = lambda _: True
res = {}
for y, line in enumerate(instr.splitlines()):
for x, char in enumerate(line):
if filter_fn(char):
res[(x, y)] = char
return res
def _get_max(grid: Grid, idx: str, filter_fn: Optional[Callable[[T], bool]] = None) -> int:
g = grid
if filter is not None:
g = filter(filter_fn, grid)
return max(map(lambda x: x[idx], g))
def get_max_x(grid: Grid, filter_fn: Optional[Callable[[T], bool]] = None) -> int:
return _get_max(grid, 0, filter_fn=filter_fn)
def get_max_y(grid: Grid, filter_fn: Optional[Callable[[T], bool]] = None) -> int:
return _get_max(grid, 1, filter_fn=filter_fn)

5
runners/py.sh Normal file
View file

@ -0,0 +1,5 @@
#!/usr/bin/env bash
set -e
PYTHONPATH=. python3 $@