From 8f53d4f557ac03d88514fb3e6a5014deb50f744c Mon Sep 17 00:00:00 2001 From: Michal Kunc Date: Thu, 1 Dec 2022 18:30:42 +0100 Subject: [PATCH] Add 1st solution --- 01/calories.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 01/calories.py diff --git a/01/calories.py b/01/calories.py new file mode 100644 index 0000000..f90d5b4 --- /dev/null +++ b/01/calories.py @@ -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()))