Skip to content
advent of code 2022

day 01

calorie counting

approach

the input is split on blank lines and each group of numbers is summed, giving one total per elf. the totals are sorted once.

part 1 is the last element of the sorted list. part 2 is the sum of the last three.

there is a rust port that does the same with split on empty lines, and a go version that was left half-written: it uses an undeclared variable and has a stray bracket, so it doesn't compile.

the trick

sort the group totals once and both answers are slices off the end.

python
text = "".join(stdin)
groups = [sum(map(int, g.splitlines())) for g in text.split("\n\n")]

groups.sort()

# part1
print(groups[-1])

# part2
print(sum(groups[-3:]))