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 = async (day: number) => {
const input = await readInputLines(day);
return input;
};
The input is just lines of text — we do the grouping logic in the solution functions:
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. First group the lines into elves, then find the maximum sum:
const partOne = (input: string[], debug: boolean) => {
const elves: number[][] = [[]];
let currentElf = elves[0];
for (const line of input) {
if (line === "") {
currentElf = [];
elves.push(currentElf);
} else {
currentElf.push(parseInt(line));
}
}
const max = elves.map(elf => elf.sum()).max();
return max;
};
I have a bunch of array helper methods I've built up over time — sum, max, maxBy, and others. They're prototype extensions (slightly shameful pollution), but incredibly useful for AoC.
Part Two: Top Three
Find the sum of calories carried by the top three elves:
const partTwo = (input: string[], debug: boolean) => {
const elves: number[][] = [[]];
let currentElf = elves[0];
for (const line of input) {
if (line === "") {
currentElf = [];
elves.push(currentElf);
} else {
currentElf.push(parseInt(line));
}
}
const elvesTotal = elves.map(elf => elf.sum()).sort((a, b) => b - a);
return elvesTotal[0] + elvesTotal[1] + elvesTotal[2];
};
Yes, I duplicate the elf-grouping logic between parts. I keep parts independent — it makes timing easier and lets me mutate freely.
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.