
AoC 2025, Day 5 - Cafeteria
Friday, December 12, 2025 at 12:00 PM
Holidays are always busy, so this puzzle was definitely done much later than anticipated. Here's Day 5: Cafeteria. Again, spoilers are below for this puzzle. Don't read it if you want to solve this yourself.
After moving loads of paper, you reveal the door to the cafeteria. Great, except that upon entering, you hear Elves going off in the kitchen. They pulled the cardinal sin of using a new inventory management system right before a holiday without any training, so they have no clue how it works.
The system is suppose to help determine which ingredients are fresh and which are spoiled. Your input gives you a range of fresh ingredient IDs, a blank space, and the ingredient IDs they have available:
3-5
10-14
16-20
12-18
1
5
8
11
17
32
The ID ranges are inclusive, so a range of 3-5 means 3, 4, and 5 are fresh ingredients. An ingredient is fresh if it's in any range.
Part 1 determines how many available ingredient IDs are fresh.
Part 2 does away with the available IDs and instead wants a sum of all fresh ingredients.
Once again, more JSON arrays for our input:
data5 = [
"3-5",
"10-14",
"16-20",
"12-18",
"",
"1",
"5",
"8",
"11",
"17",
"32"
];
Our primary driver of parsing this data will be an Array.reduce command:
let freshIdRange = [];
availableFresh = d.reduce((acc, cur) => {
if (cur.indexOf("-") != -1) {
let rangeArr = cur.split("-").map(Number);
freshIdRange.push(rangeArr);
} else if (cur == "") {
} else {
let curNum = parseInt(cur);
}
return acc;
}, 0);
We use if statements to determine the type of data we're handling. We put our fresh ID ranges into an array to reference later, and available IDs are parsed.
Super easy for this solution, it only needs a one line if comparison and increments our acc variable:
if (freshIdRange.find((i) => curNum >= i[0] && curNum <= i[1])) acc++;
Put that underneath our parseInt(cur) line, and boom, Part 1 done.
Now we have to figure out how many total fresh IDs we have. If only we could count them directly when reading the array:
let rangeArr = cur.split("-").map(Number);
totalFreshIds += rangeArr[1] - rangeArr[0] + 1;
Unfortunately, our ranges can intersect, so we need to account for that.
First, we sort our ID ranges by the lower number in the range:
freshIdRange = freshIdRange.sort((a, b) => a[0] - b[0]);
Then, starting at the 2nd element, we get our current range and previous range. If our current range's lower number is less than the previous range's upper number, they intersect. We take the Math.max of the upper numbers from both ranges, then delete the current range:
let i = 1;
while (i < freshIdRange.length) {
let prevRange = freshIdRange[i - 1],
curRange = freshIdRange[i];
if (curRange[0] <= prevRange[1]) {
prevRange[1] = Math.max(curRange[1], prevRange[1]);
freshIdRange.splice(i, 1);
continue;
}
i++;
}
With our ranges consolidated, we can use another reduce function to get our sum of fresh ID values:
totalIds = freshIdRange.reduce(
(sum, rng) => sum + (rng[1] - rng[0] + 1),
0,
);
Boom, part 2 done, and since it's just a consolidation of ranges, it doesn't affect the answer of part 1.
This is one of those gotcha puzzles, where the test could be correct and still not calculate your actual input correctly. Still, all good.
My final code:
var data5 = require("./day05.json");
var Day5A = (d) => {
let freshIdRange = [],
totalIds = 0,
availableFresh = d.reduce((acc, cur) => {
if (cur.indexOf("-") != -1) {
let rangeArr = cur.split("-").map(Number);
freshIdRange.push(rangeArr);
} else if (cur == "") {
freshIdRange = freshIdRange.sort((a, b) => a[0] - b[0]);
let i = 1;
while (i < freshIdRange.length) {
let prevRange = freshIdRange[i - 1],
curRange = freshIdRange[i];
if (curRange[0] <= prevRange[1]) {
prevRange[1] = Math.max(curRange[1], prevRange[1]);
freshIdRange.splice(i, 1);
continue;
}
i++;
}
totalIds = freshIdRange.reduce(
(sum, rng) => sum + (rng[1] - rng[0] + 1),
0,
);
} else {
let curNum = parseInt(cur);
if (freshIdRange.find((i) => curNum >= i[0] && curNum <= i[1])) acc++;
}
return acc;
}, 0);
return `${availableFresh} - ${totalIds}`;
};
console.log(`${Day5A(data5)}`);