2025: d03: ex2: add solution
This commit is contained in:
parent
7e056b9036
commit
1f357ee9a0
1 changed files with 32 additions and 0 deletions
32
2025/d03/ex2/ex2.py
Executable file
32
2025/d03/ex2/ex2.py
Executable file
|
|
@ -0,0 +1,32 @@
|
||||||
|
#!/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: list[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(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