TL;DR TL;DR — Quick Summary & Key Takeaways
Day 1 of Advent of Code 2026 is a straightforward frequency-counting warm-up in Python, and it marks the first round of my human-versus-AI series. Google Gemini 4 Hydrogen solved both parts on Medium effort without needing escalation, edging out my hand-written solution on speed while trading away a little readability.
- The puzzle: Count occurrences of integer container IDs, then multiply the three most frequent IDs for Part 1 and the three least frequent IDs for Part 2.
- My approach: Build a frequency map with
collections.defaultdict, sort all(id, count)pairs withsorted, and slice the top or bottom three. - Gemini’s approach: Count frequencies with
collections.Counterand pick the top or bottom three usingheapq.nlargestandheapq.nsmallestto avoid a full sort. - Speed vs readability: Gemini’s code ran faster (
0.135 ms/0.101 msversus0.197 ms/0.174 ms), though I find my simpler sorting helper easier to read at a glance.
December is officially here, which means it’s time to kick off my 12-day Advent of Code 2026 series in Python. As I outlined in my Advent of Code 2026 preparations post, I’m solving every puzzle by hand first before putting Google Gemini 4 Hydrogen (on Medium effort) to the test, escalating to Neon or Carbon only if needed. You can follow all twelve write-ups under the Advent of Code tag and browse the full source code in my AdventOfCode2026 GitHub repository.
Understanding Day 1: Dusty Shed
Bad weather has grounded the North Pole’s supply planes, so the Elves are dusting off a giant backup Christmas train and scrambling to load six missing types of supplies before departure. The inventory records have been lost, leaving only a long list of numeric container IDs where each number represents a type of part. You can read the full story and see the worked example on the Advent of Code Day 1 puzzle page.
From an algorithmic standpoint, Day 1 is a classic frequency-counting problem:
- Part 1: Find the three container IDs that appear most often in the list and multiply them together.
- Part 2: Find the three container IDs that appear least often in the list and multiply them together.
Because both parts rely on the exact same frequency counts, a clean solution only needs to tally each ID once and then select either the highest or lowest three frequencies.
My Hand-Written Python Solution
For my own attempt, I reached for a straightforward hash table approach using collections.defaultdict and Python’s built-in sorted function.
You can view the complete file in by_hand/day01.py on GitHub.
Counting and sorting frequencies
Here’s the core logic of my solution, omitting the imports and input loader:
def sorted_counts(input: PuzzleInput) -> List[Tuple[int, int]]:
counts = defaultdict(int)
for line in input:
counts[int(line)] += 1
return sorted(counts.items(), key=lambda x: x[1])
def part1(input: PuzzleInput) -> None:
print(prod([x[0] for x in sorted_counts(input)[-3:]]))
def part2(input: PuzzleInput) -> None:
print(prod([x[0] for x in sorted_counts(input)[:3]]))
In sorted_counts, you iterate through each line, convert it to an integer, and increment its count inside a defaultdict(int).
Sorting counts.items() by the second element of each tuple (x[1]) puts every (id, count) pair in ascending order of frequency.
From there, part1 slices the last three entries ([-3:]) to grab the three most common IDs, while part2 slices the first three entries ([:3]) for the three least common IDs.
Both parts extract the ID from each tuple and pass the resulting list straight into math.prod to print the final product.
Gemini 4 Hydrogen’s Solution
Next, I handed the Day 1 puzzle over to Gemini 4 Hydrogen on Medium effort. As expected for an opening puzzle, Hydrogen solved both parts cleanly on the first tier, so there was no need to escalate to Gemini 4 Neon or Carbon. You can inspect the full file, including Gemini’s detailed docstrings, in gemini/day01.py on GitHub.
Switching from most_common to heapq
An interesting detail emerged while watching Gemini work through the two parts.
When solving Part 1 on its own, Gemini initially used collections.Counter with its built-in Counter.most_common method.
Once Part 2 asked for the three least frequent IDs, Gemini refactored both parts into a single helper powered by the heapq module:
def _product_of_top_k_by_frequency(
items: list[int], k: int, *, most_common: bool
) -> int:
"""Multiply the `k` most or least frequent IDs in `items`."""
counts = Counter(items)
selector = heapq.nlargest if most_common else heapq.nsmallest
selected = selector(k, counts.items(), key=lambda pair: pair[1])
return prod(item_id for item_id, _ in selected)
def part1(input: PuzzleInput) -> None:
print(_product_of_top_k_by_frequency(input, 3, most_common=True))
def part2(input: PuzzleInput) -> None:
print(_product_of_top_k_by_frequency(input, 3, most_common=False))
Instead of manually looping with a defaultdict, Gemini passes the parsed IDs directly into Counter(items) to build the frequency map in one line.
It then picks either heapq.nlargest or heapq.nsmallest based on the keyword-only most_common flag, pulling out only the 3 target entries without sorting the entire collection.
Finally, it feeds a generator expression into math.prod to compute the result.
Comparing the Two Approaches
Benchmark results
Running both solutions against my puzzle input using the test runner’s time.perf_counter timings produced the following results:
| Solution | Part 1 | Part 2 | Total |
|---|---|---|---|
Hand-written (by_hand/day01.py) | 0.197 ms | 0.174 ms | 0.371 ms |
Gemini 4 Hydrogen (gemini/day01.py) | 0.135 ms | 0.101 ms | 0.236 ms |
Gemini’s solution is noticeably quicker on both parts, finishing roughly 36% faster overall.
Performance versus readability
Two standard-library choices explain why Gemini’s implementation pulls ahead on speed:
- C-backed frequency counting: I completely forgot about
collections.Counterwhen writing my solution! Under the hood in CPython,Counterdelegates its counting loop to an optimised C helper, which avoids the per-iteration Python bytecode overhead of manually updating adefaultdict(int). - Partial selection instead of full sorting: In terms of Big-O notation, sorting all $U$ unique container IDs takes $O(U \log U)$ time. By using a binary heap via
heapq.nlargestandheapq.nsmallest, Gemini finds the top or bottom $k = 3$ items in $O(U \log k) = O(U)$ time.
Even though Gemini wins the micro-benchmark, I think my hand-written solution is a little easier to read.
Seeing sorted_counts(input)[-3:] and sorted_counts(input)[:3] makes the symmetry between Part 1 and Part 2 obvious immediately, without having to trace a keyword-only boolean flag that dynamically selects between two heapq functions.
In production software, that trade-off matters.
When both implementations finish in well under a millisecond, clarity and maintainability are usually far more valuable than shaving off 0.06 milliseconds.
That said, switching my defaultdict loop to Counter would give me most of the performance win while keeping the simplicity intact.
Wrapping Up
Day 1 is a gentle start to Advent of Code 2026, and Gemini 4 Hydrogen on Medium effort passed its first test effortlessly.
While Gemini’s combination of collections.Counter and heapq beat my defaultdict and sorted approach on execution speed, the comparison is a great reminder that the fastest code isn’t always the easiest to read.
You can browse all of my puzzle write-ups under the Advent of Code tag, explore more AI benchmarks under Agentic AI, or check out the full source code in my AdventOfCode2026 repository. Look out for Day 2 tomorrow to see if Gemini 4 Hydrogen keeps its clean streak alive!
