32 lines
871 B
Python
Executable file
32 lines
871 B
Python
Executable file
#!/usr/bin/env python
|
|
|
|
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:
|
|
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)
|
|
first = bank.index(max(bank[: -length + 1]))
|
|
rest = bank[first + 1 :]
|
|
return bank[first] * 10 ** (length - 1) + recurse(rest, 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()
|