diff --git a/day1/README.md b/day1/README.md index 0003c67..1c8f0ef 100644 --- a/day1/README.md +++ b/day1/README.md @@ -51,3 +51,16 @@ Calories are being carried by the Elf carrying the most Calories. In the example fourth Elf). Find the Elf carrying the most Calories. How many total Calories is that Elf carrying? + +## --- Part Two --- + +By the time you calculate the answer to the Elves' question, they've already realized that the Elf carrying the most +Calories of food might eventually run out of snacks. + +To avoid this unacceptable situation, the Elves would instead like to know the total Calories carried by the top three +Elves carrying the most Calories. That way, even if one of those Elves runs out of snacks, they still have two backups. + +In the example above, the top three Elves are the fourth Elf (with 24000 Calories), then the third Elf (with 11000 +Calories), then the fifth Elf (with 10000 Calories). The sum of the Calories carried by these three elves is 45000. + +Find the top three Elves carrying the most Calories. How many Calories are those Elves carrying in total? \ No newline at end of file diff --git a/day1/main.py b/day1/main.py index 5877d33..f823bdf 100644 --- a/day1/main.py +++ b/day1/main.py @@ -1,4 +1,4 @@ -def max_calories() -> int: +def max_calories_1() -> int: with open("./input.txt", "r") as f: calories = 0 tmp = 0 @@ -11,5 +11,20 @@ def max_calories() -> int: return calories +def max_calories_2() -> int: + with open("./input.txt", "r") as f: + calories = [] + tmp = 0 + for line in f.readlines(): + if (c := line.strip()) != "": + tmp += int(c) + else: + calories.append(tmp) + calories.sort(reverse=True) + calories = calories[:3] + tmp = 0 + return sum(calories) + + if __name__ == "__main__": - print(max_calories()) + print(max_calories_2())