Page Navigation
Problem Intro
Welcome to Advent of Code 2026! I’ve been travelling a lot and had so many deadlines for work. So, for the first time in SIX YEARS, I totally forgot about Advent of Code until well into January. Doh!
In fact, it’s now midnight and I’m in Kosova, having just done a Angentic Harness Engineering talk as part of DevFest 2026 for the Google Developer Community here.
Better late than never!
So, here’s the story…
Bad weather has grounded the North Pole supply planes, leaving the Elves dangerously short on parts for this year’s Christmas presents. Fortunately, there’s a backup plan tucked away behind a rusty door on the edge of the complex — a giant Christmas train sitting in a dusty, cobweb-filled shed, ready to run along the old rail network. Naturally, the Elves have already volunteered you to take it out and bring back the supplies by December 12th.
Before the train can depart, the Elves need to locate six remaining types of items among hundreds of crates, tanks, boxes, and bins. Every container is labelled with a numeric ID indicating what it holds, and the Elves have handed you a list of every container’s ID number — except the inventory records are long gone, so nobody remembers which ID corresponds to which item.
Your puzzle input is a list of integer container IDs, one per line. For example:
57
57
55
57
76
78
57
45
43
78
43
76
43
78
57
45
57
76
43
43
76
Counting how many times each container ID appears in that 21-line example gives:
| Container ID | Occurrences |
|---|---|
57 |
6 |
43 |
5 |
76 |
4 |
78 |
3 |
45 |
2 |
55 |
1 |
- Part 1: Find the three most frequently occurring IDs (
57,43, and76in the example, giving186276). What do you get if you multiply together the three IDs which appear most often? - Part 2: Find the three least frequently occurring IDs (
45,78, and55in the example, giving193050). What do you get if you multiply together the three IDs which appear least often?
Part 1
What do you get if you multiply together the three IDs which appear most often?
Day 1 kicks off with a friendly warm-up, giving us a great excuse to revisit a few core Python tools — map, Counter, and math.prod — that come up again and again throughout Advent of Code.
First, we convert each line of text into an integer using map(int, data) (covered in Map, Filter, and Reduce). Rather than building a separate list in memory first, map() produces integers lazily on demand as we iterate over them.
Next, we hand that map iterator straight to collections.Counter (see Counting with Counter). Think of Counter as a specialised Python dictionary built for tallying frequencies: pass it any iterable, and it automatically counts how many times each unique item appears — replacing the usual boilerplate loop of counts[item] = counts.get(item, 0) + 1.
Even better, Counter gives us .most_common(n), which returns a list of (item, count) tuples sorted from highest count to lowest. Calling id_counts.most_common(3) returns our top three IDs along with their frequencies. Finally, we pass a generator expression (see Comprehensions) into math.prod() to multiply those three IDs together:
def part1(data: list[str]) -> int | str | None:
"""Return the product of the three most frequently occurring container IDs."""
ids = map(int, data)
id_counts = Counter(ids)
top_3 = id_counts.most_common(3)
return prod(item_id for item_id, _ in top_3)
Notice the _ in for item_id, _ in top_3 — unpacking the frequency count into _ makes it clear to readers (and linters like ruff) that we only need the container ID itself.
Alternatives to math.prod()
Python has had sum() forever, while math.prod() was only added in Python 3.8. If you didn’t reach for math.prod(), here are two classic ways to multiply a sequence of numbers together:
-
A straightforward
forloop — completely readable and needs no extra imports:total = 1 for item_id, _ in top_3: total *= item_id return total -
functools.reducewithoperator.mul— the functional-programming classic (see Map, Filter, and Reduce):from functools import reduce from operator import mul return reduce(mul, (item_id for item_id, _ in top_3))
Between the three, math.prod() is the cleanest and easiest to read at a glance.
Part 2
What do you get if you multiply together the three IDs which appear least often?
With the contraptions, apparatuses, and gizmos loaded onto the train, the Elves still need the rare items — thingamabobs, thingamajigs, and screws — which means flipping our search from the three most common IDs to the three least common.
A really handy detail of collections.Counter is what happens when you call .most_common() with no argument: instead of returning just the top n, it returns every (item_id, count) pair sorted from highest count down to lowest. That puts the rarest IDs right at the end of the list!
To grab them, we use Python’s negative slicing from the end ([-3:] — covered in Indexing and Slicing):
- Negative indices count backwards from the end of a list (
-1is the last element,-2is second-to-last,-3is third-to-last). - A slice
seq[-3:]starts three items from the back and runs all the way to the end, returning the last three items.
Because multiplication order doesn’t matter (a × b × c equals c × b × a), we don’t even need to reverse those three items — though if you ever wanted the very rarest item first, id_counts.most_common()[::-1][:3] reverses the list before taking the first three:
def part2(data: list[str]) -> int | str | None:
"""Return the product of the three least frequently occurring container IDs."""
ids = map(int, data)
id_counts = Counter(ids)
bottom_3 = id_counts.most_common()[-3:]
return prod(item_id for item_id, _ in bottom_3)
Results
Running make run 1 with Template v3 validates both sample assertions before solving the puzzle input — the output looks like this:
INFO 23:31:33.391 Part 1 soln=142857312 (158.3 µs)
INFO 23:31:33.392 Part 2 soln=119402658 (170.4 µs)
| Part | Status | Samples | Answer | Time |
|---|---|---|---|---|
| Part 1 | ok |
1 | 142857312 |
158.3 µs |
| Part 2 | ok |
1 | 119402658 |
170.4 µs |