2025: d03: ex2: add solution

This commit is contained in:
Bruno BELANYI 2025-12-03 08:19:08 +00:00
parent 7e056b9036
commit a753c65f2d

35
2025/d03/ex2/ex2.py Executable file
View 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()