@xavdid does Advent of Code

Dusty Shed

Published: 2026-11-30Original Puzzle

Welcome one and all to this year’s Advent of Code! It’s one of my favorite holiday traditions. Now more than ever, working our brains and fingers a bit is good for the soul.

Just like previous years, I’ll be walking through (polished versions of) my Python solutions to all the days I finish. I focus on writing readable, maintainable Python that takes full advantage of the standard library. Each solution uses my AoC Python Template which has utils for basic input parsing without doing too much. You’re welcome to use it and follow along!

These posts are aimed at people with (at least) basic programming experience, especially those who are interested in upping their Python skills or software fundamentals in general.

I’m not sure how many days I’ll solve this year (family, work, etc) but following the RSS feed is the best way to get notified when new posts go up.

With that out of the way, let’s get to it!

Part 1

Our first day asks us to count the ocurrances of each number in a list. It wouldn’t be too hard to write a loop, but Python actually includes a class for this exact case: collections.Counter (docs). Given an iterable, it can produce an ordered list of how many times each element appears in the list. With our sample input:

from collections import Counter
Counter([57, 57, 55, ..., 43, 43, 76])
# => Counter({57: 6, 43: 5, 76: 4, 78: 3, 45: 2, 55: 1})

It also includes a most_common method which gives us the most common N elements:

c.most_common(3)
# => [(57, 6), (43, 5), (76, 4)]

By extracting those numbers (we don’t care about the counts), we have our answer. We’ll use math.prod (the multiplication version of sum) and a small list comprehension:

from collections import Counter
from math import prod
from ...base import IntSplitSolution # my custom class
class Solution(IntSplitSolution):
def part_1(self):
counts = Counter(self.input).most_common(3)
return prod(num for num, _ in counts)

Part 2

Our next solution is generically the same as before, but we need the least common items instead. Counter.most_common(), when invoked without an argument, returns the full list of elements with their counts. We want the last 3 elements of that list:

class Solution(IntSplitSolution):
def part_2(self) -> int:
counts = Counter(self.input).most_common(3)
counts = Counter(self.input).most_common()[-3:]
return prod(num for num, _ in counts)

Cleanup

If we wanted to DRY this up a little bit, we could solve both parts in a single go. We could get the full most_common list and pass the parts we need to our prod comprehension, wrapped in a function:

def multiply_counts(l: list[tuple[int, int]]) -> int:
return prod(num for num, _ in l)
class Solution(IntSplitSolution):
def solve(self) -> tuple[int, int]:
counts = Counter(self.input).most_common()
return multiply_counts(counts[:3]), multiply_counts(counts[-3:])

For code this short it probably doesn’t matter, but it’s nice to be organized!