The first puzzle of 2022's AoC (available at https://adventofcode.com/2022/day/1) sends us into the jungle with a group of elves. Welcome to the jungle — we're here to find magical star fruit, but first we need to figure out who's carrying the most snacks.
Each elf has written down the calorie values of their snacks, separated by blank lines between elves:
1000
2000
3000
4000
5000
6000
The first elf has three items (1000 + 2000 + 3000 = 6000 calories), the second has one (4000), and so on.
Parsing the Input
The input format is the tricky part here. We have numbers separated by blank lines, where blank lines mark the boundary between elves. This requires a different approach than the typical "one item per line" parsing:
const processInput = (lines: string[]): number[][] => {
const elves: number[][] = [[]];
for (const line of lines) {
if (line === "") {
elves.push([]); // Start a new elf
} else {
elves[elves.length - 1].push(Number(line));
}
}
return elves;
};
We start with one empty array (the first elf), then either add to the current elf or start a new one when we hit a blank line. The result is an array of arrays — each inner array contains one elf's snack calories.
Part One: Hungriest Elf
Find the elf carrying the most total calories. Map each elf to their sum, then find the maximum:
const partOne = (elves: number[][], debug: boolean) => {
const totals = elves.map(elf => elf.reduce((a, b) => a + b, 0));
return Math.max(...totals);
};
I actually have a bunch of array helper methods I've built up over time — sum, max, maxBy, and others. Slightly shameful prototype pollution, but incredibly useful for AoC:
const partOne = (elves: number[][], debug: boolean) => {
return elves.map(elf => elf.sum()).max();
};
Same logic, cleaner syntax.
Part Two: Top Three
Find the sum of calories carried by the top three elves. Sort the totals in descending order, take the first three, sum them:
const partTwo = (elves: number[][], debug: boolean) => {
const totals = elves.map(elf => elf.reduce((a, b) => a + b, 0));
const sorted = totals.sort((a, b) => b - a);
return sorted[0] + sorted[1] + sorted[2];
};
Note: this code assumes we have at least three elves. In production, you'd add a check. For AoC, I know the input has plenty of elves, so I'm comfortable with this "bad code that makes documented assumptions."
Performance
Both parts run instantly. We're just summing arrays and finding maximums — O(n) operations on small data.
Takeaways
Blank lines as separators: When input uses blank lines to group data, accumulate into the current group and start a new group on empty lines.
Array helpers pay off: Having
sum()andmax()methods on arrays makes the code read like the problem description. Worth the prototype pollution for AoC.Assumptions are fine when documented: Hardcoding
sorted[0] + sorted[1] + sorted[2]is fragile but clear. Just know your input.Keep parts independent: I copy-paste code between part one and part two rather than sharing. It makes timing easier and lets me mutate freely.
The full solution is available in the repository.