Josiah Winslow solves Advent of Code

Dusty Shed

Published: 2026-12-01 Original Prompt

Welcome to my Advent of Code solution blog! The season has come fast, and shows no signs of slowing down, so I’ll try not to waste any more time. I recommend that you read the homepage if you’re new to this site so you know what to expect, and that you visit my AoC GitHub repo if you want to run these solutions directly.

‘Tis the season to be coding!

Part 1

Looks like the Elves need help sorting through ID numbers. They’re looking for the three most common IDs in a giant list of them. This looks like a job for collections.Counter, which is made for counting things (as the name suggests); in fact, Counter objects literally have a method called most_common(), which lists the n most common elements of the Counter and their counts.

>>> from collections import Counter
>>> counts = Counter("cabbagefeed")
>>> counts.most_common(3)
[('b', 3), ('e', 3), ('a', 2)]

Let’s try using this method and see how far it can take us.

My solution framework allows me to automatically convert the puzzle input into a list of ints; you’ll want to do that manually using some list comprehension. But once we have our list of IDs, we’ll want to create a Counter from it and get its 3 most common IDs — which, because each ID is paired with its count here, we’ll want to access by themselves using some other list comprehension.

2026\day01\solution.py
from collections import Counter
class Solution(IntSplitSolution):
def part_1(self) -> int:
counts = Counter(self.input)
three_most_common = [id_ for id_,_ in counts.most_common(3)]
...

Once these IDs have been obtained, we’re asked to multiply them together, which we can do using math.prod().

2026\day01\solution.py
from collections import Counter
from math import prod
class Solution(IntSplitSolution):
def part_1(self) -> int:
counts = Counter(self.input)
three_most_common = [id_ for id_,_ in counts.most_common(3)]
return prod(three_most_common)

What a nice and easy start to the day!

Part 2

Turns out the elves also want the three least common IDs. Oof, Counter doesn’t have a least_common() method for that.

So instead, let’s turn our attention to an underrated part of the Python standard library: the heapq module. heapq provides a number of algorithms you can use with a heap, a tree-like data structure that can be used to efficiently sort items.

A full introduction to the ins and outs of the heap data structure is way beyond the scope of this puzzle; the only two functions we need to know about for our purposes are heapq.nlargest() and heapq.nsmallest(). They do exactly what they say they do: they list the n smallest/largest items of an iterable. And just like similar functions such as sorted(), min(), and max(), they let you provide an optional key argument to order the items by a custom comparison key instead of their actual value.

In this case, we want to order the keys from our Counter by their associated values from that Counter. We can create the necessary key function using the lambda keyword,1 which does the job nice and succinctly.

>>> from collections import Counter
>>> from heapq import nlargest, nsmallest
>>> counts = Counter("beefcabbage")
>>> nlargest(3, counts, key=lambda n: counts[n])
['b', 'e', 'a']
>>> nsmallest(3, counts, key=lambda n: counts[n])
['f', 'c', 'g']

Now to rework our solution to use these two functions instead (remembering that we want to return both ID lists’ products as the answer)…

2026\day01\solution.py
from collections import Counter
from heapq import nlargest, nsmallest
from math import prod
class Solution(IntSplitSolution):
def solve(self) -> tuple[int, int]:
counts = Counter(self.input)
three_most_common = nlargest(3, counts, key=lambda n: counts[n])
three_least_common = nsmallest(3, counts, key=lambda n: counts[n])
return prod(three_most_common), prod(three_least_common)

…and would you look at that, we’re already finished with Day 1! How refreshing, especially since it gave me a chance to showcase two functions I liked from heapq.

Footnotes

  1. The Counter.get method would’ve worked all the same, but the static type checker I use is very unhappy with the idea that this method could return None instead of an int. ↩