summaryrefslogtreecommitdiffstats
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
parent1de244cfb7ef2017981402c7d1eaa1b5a0aa16b7 (diff)
download2023-e688c2b674fc7ad6a964a48df379e5abd01843a7.tar.gz
2023-e688c2b674fc7ad6a964a48df379e5abd01843a7.tar.bz2
2023-e688c2b674fc7ad6a964a48df379e5abd01843a7.zip
Day4
-rw-r--r--.pre-commit-config.yaml4
-rw-r--r--aoc/__main__.py22
-rw-r--r--aoc/decorators.py1
-rw-r--r--aoc/tests/test_datastructures.py63
-rw-r--r--day1/__init__.py17
-rw-r--r--day1/test_init.py3
-rw-r--r--day2/__init__ (conflicted copy 2023-12-03 150713).py40
-rw-r--r--day2/__init__.py30
-rw-r--r--day3/__init__.py42
-rw-r--r--day4/__init__.py50
-rw-r--r--day4/example.txt6
-rw-r--r--day4/input.txt206
12 files changed, 365 insertions, 119 deletions
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 1232470..0715fe6 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -2,7 +2,7 @@
2# See https://pre-commit.com/hooks.html for more hooks 2# See https://pre-commit.com/hooks.html for more hooks
3repos: 3repos:
4 - repo: https://github.com/pre-commit/pre-commit-hooks 4 - repo: https://github.com/pre-commit/pre-commit-hooks
5 rev: v4.3.0 5 rev: v4.5.0
6 hooks: 6 hooks:
7 - id: trailing-whitespace 7 - id: trailing-whitespace
8 - id: end-of-file-fixer 8 - id: end-of-file-fixer
@@ -10,6 +10,6 @@ repos:
10 - id: debug-statements 10 - id: debug-statements
11 - id: fix-encoding-pragma 11 - id: fix-encoding-pragma
12 - repo: https://github.com/psf/black 12 - repo: https://github.com/psf/black
13 rev: 22.10.0 13 rev: 23.11.0
14 hooks: 14 hooks:
15 - id: black 15 - id: black
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
diff --git a/day1/__init__.py b/day1/__init__.py
index ee66fd3..ea27c01 100644
--- a/day1/__init__.py
+++ b/day1/__init__.py
@@ -10,10 +10,7 @@ class Assignment(BaseAssignment, ABC):
10 def run(self, input: Iterator[list[int]]) -> int: 10 def run(self, input: Iterator[list[int]]) -> int:
11 input = list(input) 11 input = list(input)
12 print(input) 12 print(input)
13 return sum([ 13 return sum([int(f"{item[0]}{item[-1]}") for item in input])
14 int(f'{item[0]}{item[-1]}')
15 for item in input
16 ])
17 14
18 15
19class AssignmentOne(Assignment): 16class AssignmentOne(Assignment):
@@ -53,18 +50,8 @@ class AssignmentTwo(Assignment):
53 numbers[index] = int(i) 50 numbers[index] = int(i)
54 51
55 return [ 52 return [
56 value 53 value for key, value in sorted(numbers.items(), key=lambda item: item[0])
57 for key, value
58 in sorted(
59 numbers.items(),
60 key=lambda item: item[0]
61 )
62 ] 54 ]
63 55
64 def parse_item(self, item: str) -> list[int]: 56 def parse_item(self, item: str) -> list[int]:
65 return self._parse_item(item) 57 return self._parse_item(item)
66
67