Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions puzzles/solutions/2022/d01/p1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sys


def get_inventories(input_text: str) -> list[list[int]]:
"""
:param input_text: puzzle input
:return: list of elves' inventories
"""
inventories = input_text.split("\n\n") # Inventories are split by a blank line.
return [[int(item) for item in inventory.splitlines()] for inventory in inventories]


def get_calories_sums(inventories: list[list[int]]):
"""
:param: inventories: list of inventories
:return: list containing the sum of each inventory
"""
return list(map(sum, inventories))


def get_answer(input_text: str):
"""Return the maximal amount of calories carried by a single elf."""
inventories = get_inventories(input_text)
sums = get_calories_sums(inventories)
return max(sums)


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.
19 changes: 19 additions & 0 deletions puzzles/solutions/2022/d01/p2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import sys

import p1


def get_answer(input_text: str):
"""Return the total calories carried by the top three Elves carrying the most calories."""
inventories = p1.get_inventories(input_text)
sums = p1.get_calories_sums(inventories)

descending_calories = sorted(sums, reverse=True)
return sum(descending_calories[:3])


if __name__ == "__main__":
try:
print(get_answer(sys.argv[1]))
except IndexError:
pass # Don't crash if no input was passed through command line arguments.