Add 1st solution

This commit is contained in:
2022-12-01 18:30:42 +01:00
commit 8f53d4f557
+31
View File
@@ -0,0 +1,31 @@
import fileinput
from typing import Iterable
def solve(lines: Iterable[str]) -> int:
max_calories = 0
current_elf = 0
for line in lines:
if line == "\n":
# End of current elf
max_calories = max(max_calories, current_elf)
current_elf = 0
else:
current_elf += int(line)
return max_calories
def solveb(lines: Iterable[str]) -> int:
max_calories = [0,0,0]
current_elf = 0
for line in lines:
if line == "\n":
# End of current elf
max_calories = sorted(max_calories + [current_elf])[1:4]
current_elf = 0
else:
current_elf += int(line)
return sum(max_calories)
if __name__ == "__main__":
print("Maximum calories: ", solve(fileinput.input()))
print("Top 3: ", solveb(fileinput.input()))