Skip to content

Commit b7244ad

Browse files
committed
Attempt at a harness
1 parent 1f41f4b commit b7244ad

File tree

7 files changed

+265
-0
lines changed

7 files changed

+265
-0
lines changed

2024/README.md

Whitespace-only changes.

2024/aoc/__init__.py

Whitespace-only changes.

2024/aoc/__main__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import datetime
2+
3+
import click
4+
5+
from . import days
6+
7+
8+
@click.command()
9+
@click.option(
10+
"-i",
11+
"--input",
12+
"data",
13+
type=click.File(mode="rt", encoding="utf8"),
14+
default="-",
15+
help="Problem input file",
16+
)
17+
@click.option("-t", "--time", is_flag=True, help="Print elapsed time afterwards")
18+
@click.argument("day", required=True)
19+
def main(day: int, time: bool, data: str) -> None:
20+
runner_class = days.get_runner(day)
21+
22+
start = datetime.datetime.now()
23+
24+
part1, part2 = runner_class.run_both(data)
25+
26+
if time:
27+
click.echo(f"Elapsed: {datetime.datetime.now() - start}", err=True)
28+
29+
click.echo(part1)
30+
click.echo(part2)
31+
32+
33+
if __name__ == "__main__":
34+
main()

2024/aoc/days/__init__.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import importlib
2+
from abc import ABC, abstractmethod
3+
from typing import Any, Tuple, cast
4+
5+
6+
class Runner(ABC):
7+
@classmethod
8+
@abstractmethod
9+
def run_both(cls, data: str) -> Tuple[Any, Any]:
10+
pass
11+
12+
@classmethod
13+
@abstractmethod
14+
def part1(cls, data: str) -> Any:
15+
pass
16+
17+
@classmethod
18+
@abstractmethod
19+
def part2(cls, data: str) -> Any:
20+
pass
21+
22+
23+
class SeparateRunner(Runner):
24+
@classmethod
25+
def run_both(cls, data: str) -> Tuple[Any, Any]:
26+
return (cls.part1(data), cls.part2(data))
27+
28+
29+
class CombinedRunner(Runner):
30+
@classmethod
31+
def part1(cls, data: str) -> Any:
32+
return cls.run_both(data)[0]
33+
34+
@classmethod
35+
def part2(cls, data: str) -> Any:
36+
return cls.run_both(data)[1]
37+
38+
39+
def get_runner(day: int) -> type[Runner]:
40+
runner_module = importlib.import_module(f".day{day}", package=__name__)
41+
42+
assert issubclass(runner_module.DayRunner, Runner)
43+
44+
return cast(type[Runner], runner_module.DayRunner)

2024/aoc/days/day1.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from typing import Any
2+
3+
from . import SeparateRunner
4+
5+
6+
class DayRunner(SeparateRunner):
7+
@classmethod
8+
def part1(cls, _data: str) -> Any:
9+
return "Hello"
10+
11+
@classmethod
12+
def part2(cls, _data: str) -> Any:
13+
return "world!"

2024/pyproject.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[project]
2+
name = "aoc-2024"
3+
version = "0.1.0"
4+
description = "Advent of Code 2024 solutions programs"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = [
8+
"click>=8.1.7",
9+
"numpy>=2.1.2",
10+
]
11+
12+
[dependency-groups]
13+
dev = [
14+
"mypy>=1.13.0",
15+
"ruff>=0.7.3",
16+
]
17+
18+
[tool.ruff.lint]
19+
select = ["F", "E", "I"]
20+
21+
[tool.mypy]
22+
strict = true

2024/uv.lock

Lines changed: 152 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)