Part One
Most common IDs
- Puzzle answer
- 281,965,740
- Sample result
- 186,276
DAY 01 ADVENT OF CODE 2026
A retired Christmas train. A shed full of anonymous crates. A lesson in finding what matters by counting what repeats.

01 THE PUZZLE
Storms have grounded the North Pole's supply planes. The Elves bring an old train out of retirement so they can restock by December 12. Six types of supplies are still missing, and the labels on the containers are just numbers: the records that say what those numbers mean are gone.
The Elves remember three items being abundant and three being scarce. Count each distinct ID, sort those counts, and multiply the three selected IDs, not their counts. Once sorted, nothing outside the first three can outrank a selected ID. For ties, the solver puts the lower numeric ID first.
Read the original puzzle on Advent of CodePart One
Part Two
02 THE INTERACTIVE LAB
Start with the puzzle's example or bring your own IDs. Choose a part and see which solver code takes the data from input to answer.
Split the input at whitespace and turn each token into an integer. Repeated IDs stay in the list.
export function parseIds(input: string): readonly number[] {
return input.trim().split(/\s+/).map((token) => {
const id = Number(token);
if (!/^[+-]?\d+$/.test(token) || !Number.isSafeInteger(id)) {
throw new Error(`Invalid ID: ${JSON.stringify(token)}`);
}
return id;
});
}Group equal IDs and record how often each one appears. The sizes of the IDs do not affect the counts.
| ID | Count |
|---|
const counts = countBy(ids, (id) => id);Sort by count in the direction for this part. If two counts tie, the lower numeric ID goes first.
| ID | Count | Selection |
|---|
const ranked = sortBy(
entries(counts),
[([, count]) => count, order],
([id]) => Number(id),
);Take the first three IDs from the ranking and multiply the labels themselves, not their counts.
if (ranked.length < 3) {
throw new Error("Expected at least three distinct IDs");
}
const selected = ranked.slice(0, 3);
const product = selected.reduce((total, [id]) => total * Number(id), 1);
return { ranked, selected, product };PERFORMANCE
Measure both parts on the current input. Try a larger input to see how runtime changes.
Each result is the per-run average from the middle of five batches sorted by total time. Timings exclude rendering and vary with your device, browser, and other running work. Small differences on short inputs may be noise.
03 THE IMPLEMENTATION
The workbench runs the same TypeScript calculation as the two command-line solutions. Each block below pairs the real source with an explanation of what it does and why.
LINE 1
Remeda provides three operations we need: count repeated items, turn that count object into pairs, and sort the pairs by more than one key.
import { countBy, entries, sortBy } from "remeda";LINES 3–11
Split at whitespace, so a space works just as well as a new line. Check each token is a decimal integer that JavaScript can represent exactly; otherwise stop and name the bad token. Keep duplicates because every label still needs to be counted.
export function parseIds(input: string): readonly number[] {
return input.trim().split(/\s+/).map((token) => {
const id = Number(token);
if (!/^[+-]?\d+$/.test(token) || !Number.isSafeInteger(id)) {
throw new Error(`Invalid ID: ${JSON.stringify(token)}`);
}
return id;
});
}LINES 13–22
Use each ID as the grouping key, then turn the resulting counts into [ID, count] pairs. Sort by count in the direction the caller requests. If counts tie, compare IDs as numbers so 2 comes before 10, not after.
export function analyzeFrequencies(
ids: readonly number[],
order: "asc" | "desc",
) {
const counts = countBy(ids, (id) => id);
const ranked = sortBy(
entries(counts),
[([, count]) => count, order],
([id]) => Number(id),
);LINES 24–26
Three labels are not necessarily three IDs: 57, 57, 43 would give only two ranked entries. Reject that input rather than silently multiplying fewer than three IDs.
if (ranked.length < 3) {
throw new Error("Expected at least three distinct IDs");
}LINES 28–31
The first three entries are already the ones we want. Multiply their IDs, not the counts, using 1 as the starting value. Return the ranking and selection as well as the product so the workbench can show all three.
const selected = ranked.slice(0, 3);
const product = selected.reduce((total, [id]) => total * Number(id), 1);
return { ranked, selected, product };
}LINES 33–39
Both wrappers use the same calculation. Part One puts larger counts first; Part Two puts smaller counts first. The command-line solutions only need the resulting number.
export function topThreeProduct(ids: readonly number[]): number {
return analyzeFrequencies(ids, "desc").product;
}
export function leastThreeProduct(ids: readonly number[]): number {
return analyzeFrequencies(ids, "asc").product;
}LINES 1–3
Import the shared parser and Part One solver, then use Bun to read the input file next to the script. The website does not load this file.
import { parseIds, topThreeProduct } from "./base";
const input = await Bun.file(`${import.meta.dir}/input.txt`).text();LINE 4
Parse the labels, run the descending-count calculation, and print the product of the three most frequent IDs.
console.log(topThreeProduct(parseIds(input)));LINES 1–3
Use the shared parser and the same private file as Part One, but import the least-common solver.
import { leastThreeProduct, parseIds } from "./base";
const input = await Bun.file(`${import.meta.dir}/input.txt`).text();LINE 4
Parse the labels, run the ascending-count calculation, and print the product of the three least frequent IDs.
console.log(leastThreeProduct(parseIds(input)));04 THE PUZZLE LOG
Published proofs, in order. New days will be added as they are solved.