commit 8f53d4f557ac03d88514fb3e6a5014deb50f744c Author: Michal Kunc Date: Thu Dec 1 18:30:42 2022 +0100 Add 1st solution 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()))