Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added 2024/README.md
Empty file.
Empty file added 2024/aoc/__init__.py
Empty file.
34 changes: 34 additions & 0 deletions 2024/aoc/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import datetime

import click

from . 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", is_flag=True, help="Print elapsed time afterwards")
@click.argument("day", required=True)
def main(day: int, time: bool, data: str) -> None:
runner_class = days.get_runner(day)

start = datetime.datetime.now()

part1, part2 = runner_class.run_both(data)

if time:
click.echo(f"Elapsed: {datetime.datetime.now() - start}", err=True)

click.echo(part1)
click.echo(part2)


if __name__ == "__main__":
main()
44 changes: 44 additions & 0 deletions 2024/aoc/days/__init__.py
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, Tuple, 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)
13 changes: 13 additions & 0 deletions 2024/aoc/days/day1.py
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!"
22 changes: 22 additions & 0 deletions 2024/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[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",
"ruff>=0.7.3",
]

[tool.ruff.lint]
select = ["F", "E", "I"]

[tool.mypy]
strict = true
152 changes: 152 additions & 0 deletions 2024/uv.lock

Large diffs are not rendered by default.

Loading