Add gridutil and alter PYTHONPATH to ensure it can be imported
This commit is contained in:
parent
58557e0496
commit
d1186bb5e3
4 changed files with 47 additions and 1 deletions
2
aoc
2
aoc
|
@ -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
1
gridutil/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .grid import Coordinate, Grid, add_coords, parse
|
40
gridutil/grid.py
Normal file
40
gridutil/grid.py
Normal 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
5
runners/py.sh
Normal file
|
@ -0,0 +1,5 @@
|
|||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
PYTHONPATH=. python3 $@
|
Loading…
Add table
Add a link
Reference in a new issue