Skip to content

Commit 26d19eb

Browse files
committed
AoC 2025 Day 1 - refactor
1 parent ce016e6 commit 26d19eb

File tree

1 file changed

+33
-19
lines changed

1 file changed

+33
-19
lines changed

src/main/python/AoC2025_01.py

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
#
55

66
import sys
7+
from enum import Enum
8+
from enum import auto
9+
from enum import unique
710

811
from aoc.common import InputData
912
from aoc.common import SolutionBase
1013
from aoc.common import aoc_samples
1114

12-
Input = list[int]
13-
Output1 = int
14-
Output2 = int
15-
16-
1715
TEST = """\
1816
L68
1917
L30
@@ -31,31 +29,47 @@
3129
TOTAL = 100
3230

3331

32+
@unique
33+
class Count(Enum):
34+
LANDED_ON_ZERO = auto()
35+
PASSED_BY_ZERO = auto()
36+
37+
def count(self, dial: int, rotation: int) -> int:
38+
match self:
39+
case Count.LANDED_ON_ZERO:
40+
return 1 if (dial + rotation) % TOTAL == 0 else 0
41+
case Count.PASSED_BY_ZERO:
42+
if rotation >= 0:
43+
return (dial + rotation) // TOTAL
44+
return ((TOTAL - dial) % TOTAL - rotation) // TOTAL
45+
46+
47+
Input = list[int]
48+
Output1 = int
49+
Output2 = int
50+
51+
3452
class Solution(SolutionBase[Input, Output1, Output2]):
3553
def parse_input(self, input_data: InputData) -> Input:
3654
return [
3755
(1 if line[0] == "R" else -1) * int(line[1:])
3856
for line in input_data
3957
]
4058

41-
def part_1(self, rotations: Input) -> Output1:
42-
dial = START
43-
pos = (dial := (dial + r) % TOTAL for r in rotations)
44-
return sum(p == 0 for p in pos)
45-
46-
def part_2(self, rotations: Input) -> Output2:
59+
def solve(self, rotations: Input, count: Count) -> int:
4760
dial = START
4861
ans = 0
49-
for r in rotations:
50-
div, mod = divmod(r, TOTAL if r > 0 else -TOTAL)
51-
ans += div
52-
if (r < 0 and dial != 0 and dial + mod <= 0) or (
53-
r > 0 and dial + mod >= TOTAL
54-
):
55-
ans += 1
56-
dial = (dial + r) % TOTAL
62+
for rotation in rotations:
63+
ans += count.count(dial, rotation)
64+
dial = (dial + rotation) % TOTAL
5765
return ans
5866

67+
def part_1(self, rotations: Input) -> Output1:
68+
return self.solve(rotations, Count.LANDED_ON_ZERO)
69+
70+
def part_2(self, rotations: Input) -> Output2:
71+
return self.solve(rotations, Count.PASSED_BY_ZERO)
72+
5973
@aoc_samples(
6074
(
6175
("part_1", TEST, 3),

0 commit comments

Comments
 (0)