Advent of Code 2022: Day 1
β’ 123 words β’ 1 min β’ updated
β οΈ This post is over one year old. It may no longer be up to date or relevant. Opinions may have changed.
Refer to the previous post about AoC, and to the git repository with my solutions in Python 3.
Link to Day #1 puzzle.
Find the elf carrying the most calories. Part two finds the top three elves.
python
#!/usr/bin/env python3
import itertools
import sys
def main():
with open(sys.argv[1]) as input:
lines = input.read().splitlines()
# ['1', '2', '', '3'] -> [1, 2, '', 3]
lines = [int(line) if line != "" else line for line in lines]
# [1, 2, '', 3] -> [[1, 2], [3]]
groups = [list(group) for key, group in itertools.groupby(
lines, lambda a: a == "") if not key]
# Part 1
calories = [sum(group) for group in groups]
print(max(calories))
# Part 2
print(sum(sorted(calories, reverse=True)[:3]))
if __name__ == '__main__':
main()