-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday3.py
More file actions
61 lines (43 loc) · 1.31 KB
/
day3.py
File metadata and controls
61 lines (43 loc) · 1.31 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
# Advent of Code 2025, Day 3
# (c) blu3r4y
from aocd.models import Puzzle
from funcy import print_calls, print_durations
@print_calls
@print_durations(unit="ms")
def part1(banks):
return solve(banks, num_digits=2)
@print_calls
@print_durations(unit="ms")
def part2(banks):
return solve(banks, num_digits=12)
def solve(banks, num_digits):
total = 0
for digits in banks:
total += find_largest_combination(digits, num_digits)
return total
def find_largest_combination(digits, num_digits):
# 'running' maximum of length k found so far
dp = [0] * num_digits
for curr in digits:
# update lengths from the last index down to 1
for k in range(num_digits - 1, 0, -1):
val = dp[k - 1] * 10 + curr
if val > dp[k]:
dp[k] = val
# update length 1 (just the digit itself)
if curr > dp[0]:
dp[0] = curr
return dp[-1]
def load(data):
banks = []
for line in data.splitlines():
banks.append([int(x) for x in line.strip()])
return banks
if __name__ == "__main__":
puzzle = Puzzle(year=2025, day=3)
ans1 = part1(load(puzzle.input_data))
assert ans1 == 17343
puzzle.answer_a = ans1
ans2 = part2(load(puzzle.input_data))
assert ans2 == 172664333119298
puzzle.answer_b = ans2