Skip to content

Instantly share code, notes, and snippets.

@josfam
Created June 5, 2023 10:40
Show Gist options
  • Save josfam/8c80b6586293a718244bc10267a8a766 to your computer and use it in GitHub Desktop.
Save josfam/8c80b6586293a718244bc10267a8a766 to your computer and use it in GitHub Desktop.
Accompanying code for Advent of Code 2022 Day 1: Calorie Counting Medium article
sample_input = """
1000
2000
3000
4000
5000
6000
7000
8000
9000
10000
""".strip().splitlines()
total_calories_per_elf = []
current_elf_total = 0
for calorie in sample_input:
if calorie == '': # denotes next elf's input
total_calories_per_elf.append(current_elf_total)
current_elf_total = 0 # reset before the next batch of calories
else:
current_elf_total += int(calorie)
# add any left-over calories to `total_calories_per_elf`
total_calories_per_elf.append(current_elf_total)
# sort in ascending order
sorted_calories = sorted(total_calories_per_elf, reverse=True)
top_three_calories = sorted_calories[0:3]
print(sum(top_three_calories))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment