diff --git a/2022/01/main.py b/2022/01/main.py new file mode 100644 index 0000000..2cdc46d --- /dev/null +++ b/2022/01/main.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# -*- encoding: utf-8; py-indent-offset: 4 -*- + +# Author: Sebastian Mark +# CC-BY-SA (https://creativecommons.org/licenses/by-sa/4.0/deed.de) + +# pylint: disable=missing-module-docstring,missing-function-docstring,consider-using-f-string + + +def readinput(): + with open("input", "r", encoding="utf-8") as file: + lines = [] + for line in file.read().splitlines(): + lines.append(int(line) if line.isdigit() else "") + lines.append("") + return lines + + +def sum_carried_calories(calories: list) -> list: + maxcals = [] + cur = 0 + for cal in calories: + if cal != "": + cur += cal + else: + maxcals.append(cur) + cur = 0 + return maxcals + + +def main(): + calories = readinput() + carried = sum_carried_calories(calories) + + # part1 + maxcarried = max(carried) + print("Max carried calories: %d" % maxcarried) + + # part2 + topthree = sum(sorted(carried, reverse=True)[:3]) + print("Max 3 carried calories: %d" % topthree) + + +if __name__ == "__main__": + main()