(update) day 3 part 1
This commit is contained in:
32
2025/03/main.py
Normal file
32
2025/03/main.py
Normal file
@ -0,0 +1,32 @@
|
||||
#!/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)
|
||||
Reference in New Issue
Block a user