-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from bertptrs/prepare-2024
Prepare for 2024
- Loading branch information
Showing
11 changed files
with
367 additions
and
70 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
on: | ||
- push | ||
|
||
name: Advent of Code 2024 | ||
|
||
jobs: | ||
ci: | ||
strategy: | ||
matrix: | ||
python-version: | ||
- "3.12" | ||
- "3.13" | ||
|
||
name: Continuous Integration | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
|
||
- name: Install uv | ||
uses: astral-sh/setup-uv@v3 | ||
with: | ||
enable-cache: true | ||
cache-dependency-glob: "2024/uv.lock" | ||
|
||
- name: Check formatting | ||
working-directory: "2024" | ||
run: > | ||
uv run ruff format --check | ||
- name: Run lints | ||
working-directory: "2024" | ||
run: > | ||
uv run ruff check | ||
- name: Run tests | ||
working-directory: "2024" | ||
run: > | ||
uv run pytest tests |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[project] | ||
name = "aoc-2024" | ||
version = "0.1.0" | ||
description = "Advent of Code 2024 solutions programs" | ||
readme = "README.md" | ||
requires-python = ">=3.12" | ||
dependencies = [ | ||
"click>=8.1.7", | ||
"numpy>=2.1.2", | ||
] | ||
|
||
[dependency-groups] | ||
dev = [ | ||
"mypy>=1.13.0", | ||
"pytest>=8.3.3", | ||
"ruff>=0.7.3", | ||
] | ||
|
||
[tool.uv] | ||
package = true | ||
|
||
[tool.ruff.lint] | ||
select = ["F", "E", "I"] | ||
|
||
[tool.mypy] | ||
strict = true |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import datetime | ||
import time | ||
|
||
import click | ||
|
||
from aoc import days | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"-i", | ||
"--input", | ||
"data", | ||
type=click.File(mode="rt", encoding="utf8"), | ||
default="-", | ||
help="Problem input file", | ||
) | ||
@click.option( | ||
"-t", "--time", "timing", is_flag=True, help="Print elapsed time afterwards" | ||
) | ||
@click.argument("day", required=True) | ||
def main(day: int, timing: bool, data: str) -> None: | ||
runner_class = days.get_runner(day) | ||
|
||
start = time.perf_counter_ns() | ||
|
||
part1, part2 = runner_class.run_both(data) | ||
|
||
if timing: | ||
elapsed = time.perf_counter_ns() - start | ||
delta = datetime.timedelta(microseconds=elapsed / 1000) | ||
click.echo(f"Elapsed: {delta}", err=True) | ||
|
||
click.echo(part1) | ||
click.echo(part2) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import importlib | ||
from abc import ABC, abstractmethod | ||
from typing import Any, cast | ||
|
||
|
||
class Runner(ABC): | ||
@classmethod | ||
@abstractmethod | ||
def run_both(cls, data: str) -> tuple[Any, Any]: | ||
pass | ||
|
||
@classmethod | ||
@abstractmethod | ||
def part1(cls, data: str) -> Any: | ||
pass | ||
|
||
@classmethod | ||
@abstractmethod | ||
def part2(cls, data: str) -> Any: | ||
pass | ||
|
||
|
||
class SeparateRunner(Runner): | ||
@classmethod | ||
def run_both(cls, data: str) -> tuple[Any, Any]: | ||
return (cls.part1(data), cls.part2(data)) | ||
|
||
|
||
class CombinedRunner(Runner): | ||
@classmethod | ||
def part1(cls, data: str) -> Any: | ||
return cls.run_both(data)[0] | ||
|
||
@classmethod | ||
def part2(cls, data: str) -> Any: | ||
return cls.run_both(data)[1] | ||
|
||
|
||
def get_runner(day: int) -> type[Runner]: | ||
runner_module = importlib.import_module(f".day{day}", package=__name__) | ||
|
||
assert issubclass(runner_module.DayRunner, Runner) | ||
|
||
return cast(type[Runner], runner_module.DayRunner) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from typing import Any | ||
|
||
from . import SeparateRunner | ||
|
||
|
||
class DayRunner(SeparateRunner): | ||
@classmethod | ||
def part1(cls, _data: str) -> Any: | ||
return "Hello" | ||
|
||
@classmethod | ||
def part2(cls, _data: str) -> Any: | ||
return "world!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
from aoc import days | ||
|
||
|
||
def test_harness_works() -> None: | ||
runner = days.get_runner(1) | ||
assert runner is not None |
Oops, something went wrong.