33 lines
908 B
Python
33 lines
908 B
Python
#!/bin/env python3
|
|
|
|
|
|
def turn_on_batteries(series: str, amount: int) -> list:
|
|
batteries = list(series.strip())
|
|
batteries.sort(reverse=True)
|
|
highest = batteries[0]
|
|
|
|
if series.strip().__len__() == series.strip().index(highest) + 1:
|
|
highest = batteries[1]
|
|
|
|
highest_at = series.strip().index(highest)
|
|
for battery in batteries:
|
|
occurances = [
|
|
i for i, x in enumerate(series.strip()) if x == battery and i > highest_at
|
|
]
|
|
# print(battery, occurances)
|
|
if occurances.__len__() == 0:
|
|
continue
|
|
second_highest = battery
|
|
break
|
|
return [highest, second_highest]
|
|
|
|
|
|
with open("example", "+r") as file:
|
|
result = 0
|
|
for line in file.readlines():
|
|
batteries = turn_on_batteries(line, 2)
|
|
print(line.strip(), batteries)
|
|
result = result + int("".join(batteries))
|
|
|
|
print("result:", result)
|