mirror of
https://github.com/bertptrs/adventofcode.git
synced 2025-12-25 21:00:31 +01:00
Implement day 1
This commit is contained in:
0
2024/inputs/.gitkeep
Normal file
0
2024/inputs/.gitkeep
Normal file
@@ -1,5 +1,6 @@
|
|||||||
import datetime
|
import datetime
|
||||||
import time
|
import time
|
||||||
|
from typing import IO
|
||||||
|
|
||||||
import click
|
import click
|
||||||
|
|
||||||
@@ -19,8 +20,9 @@ from aoc import days
|
|||||||
"-t", "--time", "timing", is_flag=True, help="Print elapsed time afterwards"
|
"-t", "--time", "timing", is_flag=True, help="Print elapsed time afterwards"
|
||||||
)
|
)
|
||||||
@click.argument("day", required=True)
|
@click.argument("day", required=True)
|
||||||
def main(day: int, timing: bool, data: str) -> None:
|
def main(day: int, timing: bool, data: IO[str]) -> None:
|
||||||
runner_class = days.get_runner(day)
|
runner_class = days.get_runner(day)
|
||||||
|
data = data.read()
|
||||||
|
|
||||||
start = time.perf_counter_ns()
|
start = time.perf_counter_ns()
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,28 @@
|
|||||||
|
from collections import defaultdict
|
||||||
|
from io import StringIO
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from . import SeparateRunner
|
import numpy
|
||||||
|
|
||||||
|
from . import CombinedRunner
|
||||||
|
|
||||||
|
|
||||||
class DayRunner(SeparateRunner):
|
class DayRunner(CombinedRunner):
|
||||||
@classmethod
|
@classmethod
|
||||||
def part1(cls, _data: str) -> Any:
|
def run_both(cls, data: str) -> tuple[Any, Any]:
|
||||||
return "Hello"
|
data = StringIO(data)
|
||||||
|
nums = numpy.loadtxt(data, dtype=numpy.int32)
|
||||||
|
|
||||||
@classmethod
|
left = nums[..., 0]
|
||||||
def part2(cls, _data: str) -> Any:
|
right = nums[..., 1]
|
||||||
return "world!"
|
|
||||||
|
left.sort()
|
||||||
|
right.sort()
|
||||||
|
|
||||||
|
diff = numpy.abs(left - right).sum()
|
||||||
|
|
||||||
|
counts = defaultdict(int)
|
||||||
|
for val in right:
|
||||||
|
counts[val] += 1
|
||||||
|
|
||||||
|
return diff, sum(counts[v] * v for v in left)
|
||||||
|
|||||||
6
2024/tests/samples/01.txt
Normal file
6
2024/tests/samples/01.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
3 4
|
||||||
|
4 3
|
||||||
|
2 5
|
||||||
|
1 3
|
||||||
|
3 9
|
||||||
|
3 3
|
||||||
14
2024/tests/test_day1.py
Normal file
14
2024/tests/test_day1.py
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from aoc.days.day1 import DayRunner
|
||||||
|
|
||||||
|
def get_data() -> str:
|
||||||
|
sample = os.path.dirname(__file__) + "/samples/01.txt"
|
||||||
|
with open(sample, mode="rt", encoding="utf-8") as f:
|
||||||
|
return f.read()
|
||||||
|
|
||||||
|
def test_sample_part1():
|
||||||
|
assert DayRunner.part1(get_data()) == 11
|
||||||
|
|
||||||
|
def test_sample_part2():
|
||||||
|
assert DayRunner.part2(get_data()) == 31
|
||||||
Reference in New Issue
Block a user