mirror of
https://github.com/rjNemo/advent_of_code_2022
synced 2026-06-09 03:56:49 +00:00
day1 part 2
This commit is contained in:
parent
58d1088518
commit
b78eb75e4d
2 changed files with 30 additions and 2 deletions
|
|
@ -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?
|
||||
19
day1/main.py
19
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())
|
||||
|
|
|
|||
Loading…
Reference in a new issue