summaryrefslogtreecommitdiffstats
path: root/aoc
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2023-12-04 10:40:34 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2023-12-04 10:41:07 +0100
commite688c2b674fc7ad6a964a48df379e5abd01843a7 (patch)
treee73ee82805e5521463706d117bd036cc1ae13ac8 /aoc
parent1de244cfb7ef2017981402c7d1eaa1b5a0aa16b7 (diff)
download2023-e688c2b674fc7ad6a964a48df379e5abd01843a7.tar.gz
2023-e688c2b674fc7ad6a964a48df379e5abd01843a7.tar.bz2
2023-e688c2b674fc7ad6a964a48df379e5abd01843a7.zip
Day4
Diffstat (limited to 'aoc')
-rw-r--r--aoc/__main__.py22
-rw-r--r--aoc/decorators.py1
-rw-r--r--aoc/tests/test_datastructures.py63
3 files changed, 62 insertions, 24 deletions
diff --git a/aoc/__main__.py b/aoc/__main__.py
index f5f08d3..596abee 100644
--- a/aoc/__main__.py
+++ b/aoc/__main__.py
@@ -1,3 +1,4 @@
1# -*- coding: utf-8 -*-
1import enum 2import enum
2import typing 3import typing
3import importlib 4import importlib
@@ -25,15 +26,17 @@ def kwargs(kwarg: str) -> List:
25 26
26 27
27@app.command() 28@app.command()
28def run(day: str, 29def run(
29 part: str = typer.Option("One", '--part', help="Assignment part. Defaults to 'One'.", show_choices=True), 30 day: str,
30 example: bool = typer.Option(False, '--example', help="Use an example input file"), 31 part: str = typer.Option(
31 kwargs: List[str] = typer.Argument(None) 32 "1", "--part", help="Assignment part. Defaults to 'One'.", show_choices=True
32 ): 33 ),
33 34 example: bool = typer.Option(False, "--example", help="Use an example input file"),
35 kwargs: List[str] = typer.Argument(None),
36):
34 assignment_day = importlib.import_module(day) 37 assignment_day = importlib.import_module(day)
35 38
36 Assignment = getattr(assignment_day, f"Assignment{part}") 39 Assignment = getattr(assignment_day, f"Assignment{AssignmentPart(part).name}")
37 assignment = Assignment( 40 assignment = Assignment(
38 path=os.path.dirname(assignment_day.__file__), **dict(kwargs) 41 path=os.path.dirname(assignment_day.__file__), **dict(kwargs)
39 ) 42 )
@@ -46,7 +49,10 @@ def run(day: str,
46 ) 49 )
47 end = perf_counter() 50 end = perf_counter()
48 delta = end - start 51 delta = end - start
49 typer.secho(f'\n{(delta if delta > 1 else delta * 1000):.3f}{"s" if delta > 1 else "ms"}', fg="green") 52 typer.secho(
53 f'\n{(delta if delta > 1 else delta * 1000):.3f}{"s" if delta > 1 else "ms"}',
54 fg="green",
55 )
50 56
51 57
52@app.command() 58@app.command()
diff --git a/aoc/decorators.py b/aoc/decorators.py
index 42603c2..98246e6 100644
--- a/aoc/decorators.py
+++ b/aoc/decorators.py
@@ -24,7 +24,6 @@ def infinite_generator(func: AssignmentRun) -> AssignmentRun:
24def list_input(func: AssignmentRun) -> AssignmentRunList: 24def list_input(func: AssignmentRun) -> AssignmentRunList:
25 @wraps(func) 25 @wraps(func)
26 def wrapper(self, input: Iterator, *args, **kwargs) -> T: 26 def wrapper(self, input: Iterator, *args, **kwargs) -> T:
27
28 return func(self, list(input), *args, **kwargs) 27 return func(self, list(input), *args, **kwargs)
29 28
30 return wrapper 29 return wrapper
diff --git a/aoc/tests/test_datastructures.py b/aoc/tests/test_datastructures.py
index 42cc44d..1c291cc 100644
--- a/aoc/tests/test_datastructures.py
+++ b/aoc/tests/test_datastructures.py
@@ -24,13 +24,26 @@ class TestCoordinate:
24 24
25 def test_neighbours(self): 25 def test_neighbours(self):
26 c = Coordinate(0, 0) 26 c = Coordinate(0, 0)
27 neighbours = {Coordinate(-1, 0), Coordinate(1, 0), Coordinate(0, -1), Coordinate(0, 1), 27 neighbours = {
28 Coordinate(-1, -1), Coordinate(-1, 1), Coordinate(1, -1), Coordinate(1, 1)} 28 Coordinate(-1, 0),
29 Coordinate(1, 0),
30 Coordinate(0, -1),
31 Coordinate(0, 1),
32 Coordinate(-1, -1),
33 Coordinate(-1, 1),
34 Coordinate(1, -1),
35 Coordinate(1, 1),
36 }
29 assert set(c.neighbours()) == neighbours 37 assert set(c.neighbours()) == neighbours
30 38
31 def test_neighbours_no_diagonal(self): 39 def test_neighbours_no_diagonal(self):
32 c = Coordinate(0, 0) 40 c = Coordinate(0, 0)
33 neighbours_no_diagonal = {Coordinate(-1, 0), Coordinate(1, 0), Coordinate(0, -1), Coordinate(0, 1)} 41 neighbours_no_diagonal = {
42 Coordinate(-1, 0),
43 Coordinate(1, 0),
44 Coordinate(0, -1),
45 Coordinate(0, 1),
46 }
34 assert set(c.neighbours(no_diagonal=True)) == neighbours_no_diagonal 47 assert set(c.neighbours(no_diagonal=True)) == neighbours_no_diagonal
35 48
36 49
@@ -57,23 +70,43 @@ class TestCoordinate3:
57 def test_neighbours(self): 70 def test_neighbours(self):
58 c = Coordinate3(0, 0, 0) 71 c = Coordinate3(0, 0, 0)
59 neighbours = { 72 neighbours = {
60 Coordinate3(-1, -1, -1), Coordinate3(-1, -1, 0), Coordinate3(-1, -1, 1), 73 Coordinate3(-1, -1, -1),
61 Coordinate3(-1, 0, -1), Coordinate3(-1, 0, 0), Coordinate3(-1, 0, 1), 74 Coordinate3(-1, -1, 0),
62 Coordinate3(-1, 1, -1), Coordinate3(-1, 1, 0), Coordinate3(-1, 1, 1), 75 Coordinate3(-1, -1, 1),
63 Coordinate3(0, -1, -1), Coordinate3(0, -1, 0), Coordinate3(0, -1, 1), 76 Coordinate3(-1, 0, -1),
64 Coordinate3(0, 0, -1), Coordinate3(0, 0, 1), 77 Coordinate3(-1, 0, 0),
65 Coordinate3(0, 1, -1), Coordinate3(0, 1, 0), Coordinate3(0, 1, 1), 78 Coordinate3(-1, 0, 1),
66 Coordinate3(1, -1, -1), Coordinate3(1, -1, 0), Coordinate3(1, -1, 1), 79 Coordinate3(-1, 1, -1),
67 Coordinate3(1, 0, -1), Coordinate3(1, 0, 0), Coordinate3(1, 0, 1), 80 Coordinate3(-1, 1, 0),
68 Coordinate3(1, 1, -1), Coordinate3(1, 1, 0), Coordinate3(1, 1, 1) 81 Coordinate3(-1, 1, 1),
82 Coordinate3(0, -1, -1),
83 Coordinate3(0, -1, 0),
84 Coordinate3(0, -1, 1),
85 Coordinate3(0, 0, -1),
86 Coordinate3(0, 0, 1),
87 Coordinate3(0, 1, -1),
88 Coordinate3(0, 1, 0),
89 Coordinate3(0, 1, 1),
90 Coordinate3(1, -1, -1),
91 Coordinate3(1, -1, 0),
92 Coordinate3(1, -1, 1),
93 Coordinate3(1, 0, -1),
94 Coordinate3(1, 0, 0),
95 Coordinate3(1, 0, 1),
96 Coordinate3(1, 1, -1),
97 Coordinate3(1, 1, 0),
98 Coordinate3(1, 1, 1),
69 } 99 }
70 assert set(c.neighbours()) == neighbours 100 assert set(c.neighbours()) == neighbours
71 101
72 def test_neighbours_no_diagonal(self): 102 def test_neighbours_no_diagonal(self):
73 c = Coordinate3(0, 0, 0) 103 c = Coordinate3(0, 0, 0)
74 neighbours_no_diagonal = { 104 neighbours_no_diagonal = {
75 Coordinate3(-1, 0, 0), Coordinate3(1, 0, 0), 105 Coordinate3(-1, 0, 0),
76 Coordinate3(0, -1, 0), Coordinate3(0, 1, 0), 106 Coordinate3(1, 0, 0),
77 Coordinate3(0, 0, -1), Coordinate3(0, 0, 1) 107 Coordinate3(0, -1, 0),
108 Coordinate3(0, 1, 0),
109 Coordinate3(0, 0, -1),
110 Coordinate3(0, 0, 1),
78 } 111 }
79 assert set(c.neighbours(no_diagonal=True)) == neighbours_no_diagonal 112 assert set(c.neighbours(no_diagonal=True)) == neighbours_no_diagonal