summaryrefslogtreecommitdiffstats
path: root/day1
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <tom@vanderlee.io>2025-12-02 07:57:38 +0100
committerGravatar Tom van der Lee <tom@vanderlee.io>2025-12-02 09:40:04 +0100
commitf315ef05c548da80410cb4f6665a9bba7a953f94 (patch)
treea727c6ad862a4a9c237c320705483b3def7acc11 /day1
parentd7e30321ae6ae4c82a8ab7455f6ce33afd719c67 (diff)
download2025-f315ef05c548da80410cb4f6665a9bba7a953f94.tar.gz
2025-f315ef05c548da80410cb4f6665a9bba7a953f94.tar.bz2
2025-f315ef05c548da80410cb4f6665a9bba7a953f94.zip
Day 1 and 2
Diffstat (limited to 'day1')
-rw-r--r--day1/__init__.py60
-rw-r--r--day1/example.txt10
-rw-r--r--day1/test_init.py38
3 files changed, 108 insertions, 0 deletions
diff --git a/day1/__init__.py b/day1/__init__.py
new file mode 100644
index 0000000..6e95329
--- /dev/null
+++ b/day1/__init__.py
@@ -0,0 +1,60 @@
1# -*- coding: utf-8 -*-
2from abc import ABC
3from typing import Iterator
4
5from aoc import BaseAssignment, I
6
7
8class Assignment(BaseAssignment, ABC):
9 def parse_item(self, item: str) -> Iterator[I]:
10 [rotation, *places] = item
11 places = int("".join(places))
12
13 yield -places if rotation == "L" else places
14
15
16class AssignmentOne(Assignment):
17 example_result = 3
18
19 def run(self, input: Iterator[int]) -> int:
20 seen_0 = 0
21 position = 50
22 for rotation in input:
23 position = (position + rotation) % 100
24 if position == 0:
25 seen_0 += 1
26
27 return seen_0
28
29
30class AssignmentTwo(Assignment):
31 example_result = 6
32
33 @staticmethod
34 def calculate_new_position(position, rotation) -> tuple[int, int]:
35 previous_position = position
36 new_position = position + rotation
37
38 overturned = new_position >= 100 or new_position < 0
39
40 multiplier = position - (0 - -previous_position) // 100
41
42 if overturned:
43 new_position = new_position % 100
44
45 seen_0 = 0
46 if new_position == 0 and multiplier == 0:
47 seen_0 += multiplier + 1
48 elif overturned:
49 seen_0 += multiplier
50
51 return new_position, seen_0
52
53 def run(self, input: Iterator[int]) -> int:
54 seen_0_total = 0
55 position = 50
56 for rotation in input:
57 position, seen_0 = self.calculate_new_position(position, rotation)
58 seen_0_total += seen_0
59
60 return seen_0_total
diff --git a/day1/example.txt b/day1/example.txt
new file mode 100644
index 0000000..53287c7
--- /dev/null
+++ b/day1/example.txt
@@ -0,0 +1,10 @@
1L68
2L30
3R48
4L5
5R60
6L55
7L1
8L99
9R14
10L82
diff --git a/day1/test_init.py b/day1/test_init.py
new file mode 100644
index 0000000..a24e272
--- /dev/null
+++ b/day1/test_init.py
@@ -0,0 +1,38 @@
1# -*- coding: utf-8 -*-
2import pytest
3
4from day1 import AssignmentTwo
5
6
7class TestAssignmentTwo:
8 data = [
9 # Example Data
10 (50, -68, 82, 1),
11 (82, -30, 52, 0),
12 (52, 48, 0, 1),
13 (0, -5, 95, 0),
14 (95, 60, 55, 1),
15 (55, -55, 0, 1),
16 (0, -1, 99, 0),
17 (99, -99, 0, 1),
18 (0, 14, 14, 0),
19 (14, -82, 32, 1),
20 # Additional cases
21 (0, 10, 10, 0),
22 (0, -10, 90, 0),
23 (0, 100, 0, 1),
24 (0, -100, 0, 1),
25 (80, 40, 20, 1),
26 (80, 140, 20, 2),
27 (80, 100, 80, 1),
28 (0, -300, 0, 3),
29 (0, 299, 99, 2),
30 (0, -299, 1, 2),
31 ]
32
33 @pytest.mark.parametrize("position,rotation,new_position,seen_0", data)
34 def test_calculate_new_position(self, position, rotation, new_position, seen_0):
35 assert AssignmentTwo.calculate_new_position(position, rotation) == (
36 new_position,
37 seen_0,
38 )