Add 1st solution
This commit is contained in:
commit
8f53d4f557
31
01/calories.py
Normal file
31
01/calories.py
Normal 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()))
|
Loading…
Reference in New Issue
Block a user