2025: d03: ex2: add solution
This commit is contained in:
parent
7e056b9036
commit
a753c65f2d
1 changed files with 35 additions and 0 deletions
35
2025/d03/ex2/ex2.py
Executable file
35
2025/d03/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,35 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
import functools
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
|
def solve(input: list[str]) -> int:
|
||||||
|
def parse(input: list[str]) -> list[list[int]]:
|
||||||
|
return [[int(c) for c in line] for line in input]
|
||||||
|
|
||||||
|
def max_joltage(bank: list[int]) -> int:
|
||||||
|
@functools.cache
|
||||||
|
def recurse(bank: tuple[int, ...], length: int) -> int:
|
||||||
|
assert length > 0 # Sanity check
|
||||||
|
assert length <= len(bank) # Sanity check
|
||||||
|
if length == 1:
|
||||||
|
return max(bank)
|
||||||
|
return max(
|
||||||
|
bank[i] * 10 ** (length - 1) + recurse(bank[i + 1 :], length - 1)
|
||||||
|
for i in range(len(bank) - length + 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
return recurse(tuple(bank), 12)
|
||||||
|
|
||||||
|
batteries = parse(input)
|
||||||
|
return sum(map(max_joltage, batteries))
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
input = sys.stdin.read().splitlines()
|
||||||
|
print(solve(input))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Loading…
Add table
Add a link
Reference in a new issue