add 2022/day1
This commit is contained in:
parent
137880a94d
commit
f3d1c219f1
1 changed files with 45 additions and 0 deletions
45
2022/01/main.py
Normal file
45
2022/01/main.py
Normal file
|
@ -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()
|
Loading…
Reference in a new issue