-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAoC2025_03.py
More file actions
70 lines (51 loc) · 1.47 KB
/
Copy pathAoC2025_03.py
File metadata and controls
70 lines (51 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#! /usr/bin/env python3
#
# Advent of Code 2025 Day 3
#
import sys
from functools import cache
from aoc.common import InputData
from aoc.common import SolutionBase
from aoc.common import aoc_samples
Input = InputData
Output1 = int
Output2 = int
TEST = """\
987654321111111
811111111111119
234234234234278
818181911112111
"""
class Solution(SolutionBase[Input, Output1, Output2]):
def parse_input(self, input_data: InputData) -> Input:
return input_data
def solve_line(self, line: str, digits: int) -> int:
@cache
def dfs(start: int, left: int) -> int:
if left == 0:
return 0
if len(line) - start == left:
return int(line[start:])
a = int(line[start]) * 10 ** (left - 1) + dfs(start + 1, left - 1)
b = dfs(start + 1, left)
return max(int(a), b)
return dfs(0, digits)
def solve(self, inputs: Input, digits: int) -> int:
return sum(self.solve_line(line, digits) for line in inputs)
def part_1(self, inputs: Input) -> Output1:
return self.solve(inputs, digits=2)
def part_2(self, inputs: Input) -> Output2:
return self.solve(inputs, digits=12)
@aoc_samples(
(
("part_1", TEST, 357),
("part_2", TEST, 3121910778619),
)
)
def samples(self) -> None:
pass
solution = Solution(2025, 3)
def main() -> None:
solution.run(sys.argv)
if __name__ == "__main__":
main()