December 22, 2026 · Advent of MongoDB
22. Two trips Solved
Hard$setWindowFieldsvariables
Two trips. Load the sleigh in delivery order, time zone by time zone, and when the next time zone doesn't fit any more, everything from there goes on the second trip. Capacity is yesterday's seven-reindeer total.
Your task
Using the true list and delivery order (biggest offset first), compute the running total of the load. A time zone goes on trip 1 if the running total up to and including it is at most the capacity of the reindeer that have flown since 2026-11-26, otherwise trip 2. Return utcOffset, cumulativeKg and trip, in delivery order.
Your answer returns 13 documents with the fields utcOffset, cumulativeKg, trip. Order matters.
What happens next
Trip 1 sweeps from the Pacific through Asia and Europe. Trip 2 picks up the rest. Dusty does the maths on carrots and grins. "They can do it. They've trained all year for this."
Solve today's puzzle to read what happens next.
The data
The North Pole's collections, a snapshot taken on December 1, 2026. Times are North Pole time (UTC). A MongoDB-compatible database runs in your browser: use Explore data to browse it.
elves_id, name, department, hiredOn, managerId (→ elves._id, null for the Head Elf), leave: [{ from, to }]children_id, name, country, city, utcOffset, age, wishlist: [toy _id, …] (top wish first)toys_id, name, category, buildMinutes, weightGniceListLog_id, childId, loggedAt, points, reason, recordedBy (→ elves._id), terminal (→ terminals._id)terminals_id (e.g. "LOGI-07"), location, departmentreindeer_id, name, maxLoadKgflights_id, reindeerId, flownOn, km, carrotsbuilds_id, toyId, elfId, builtAt, minutes, passedQccookieBatches_id, bakedAt, elfId, cookiesdoorSwipes_id, elfId, swipedAt, door, direction ("in" or "out")
Hints
$setWindowFields with sortBy: { utcOffset: -1 } and window: { documents: ["unbounded", "current"] }.Solution
const capacity = db.reindeer.aggregate([
{ $lookup: { from: "flights", localField: "_id", foreignField: "reindeerId", as: "f" } },
{ $match: { "f.flownOn": { $gte: ISODate("2026-11-26") } } },
{ $group: { _id: null, kg: { $sum: "$maxLoadKg" } } }
]).toArray()[0].kg;
db.niceListLog.aggregate([
{ $group: { _id: "$childId", score: { $sum: { $cond: [{ $and: [{ $eq: ["$terminal", "LOGI-07"] }, { $lt: [{ $hour: "$loggedAt" }, 5] }] }, 0, "$points"] } } } },
{ $match: { score: { $gte: 0 } } },
{ $lookup: { from: "children", localField: "_id", foreignField: "_id", as: "c" } },
{ $unwind: "$c" },
{ $lookup: { from: "toys", localField: "c.wishlist", foreignField: "_id", as: "gifts" } },
{ $group: { _id: "$c.utcOffset", grams: { $sum: { $sum: "$gifts.weightG" } } } },
{ $setWindowFields: { sortBy: { _id: -1 }, output: { running: { $sum: "$grams", window: { documents: ["unbounded", "current"] } } } } },
{ $project: {
_id: 0, utcOffset: "$_id",
cumulativeKg: { $divide: ["$running", 1000] },
trip: { $cond: [{ $lte: [{ $divide: ["$running", 1000] }, capacity] }, 1, 2] }
} },
{ $sort: { utcOffset: -1 } }
])
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Also on Advent of SQL.