Day 4: Part 1

This commit is contained in:
2025-12-05 06:56:02 +01:00
parent 71217a3d4c
commit 5057e2e8eb
3 changed files with 163 additions and 0 deletions

30
day3/main.py Normal file
View File

@@ -0,0 +1,30 @@
batteries = []
with open("day3/input.txt") as file:
for line in file:
batteries.append([int(c) for c in line.strip()])
def max_12_digit_number(digits):
target_len = 12
to_remove = len(digits) - target_len
stack = []
for d in digits:
while stack and to_remove > 0 and stack[-1] < d:
stack.pop()
to_remove -= 1
stack.append(d)
# Falls noch nicht genug entfernt wurde (am Ende nur große Zahlen)
return stack[:target_len]
total = 0
for bank in batteries:
best_digits = max_12_digit_number(bank)
joltage = int("".join(map(str, best_digits)))
total += joltage
print(total)