Skip to content

Advent of Code 2026 Day 1: Dusty Shed

from advent import Counter, parse, prod

ordered_ids = [id for id, _ in Counter(parse(1, int)).most_common()]

assert prod(ordered_ids[:3]) == 43117448  # Part 1
assert prod(ordered_ids[-3:]) == 9623400  # Part 2

Problem

This was fairly trivial, even for a "Day 1" puzzle for Advent of Code!

We're given a file containing one ID per line. The main goal is to order the IDs by frequency. For part one, the answer is the product of the 3 most common IDs. For part two, the answer is the product of the 3 least common IDs.

Input Parsing

The input for Day 1 consists of one integer ID per line. parse(1, int) provides the following:

----------------------------------------------------------------------------------------------------
day01.txt ➜ 7995 chars, 1999 lines; first 3 lines:
----------------------------------------------------------------------------------------------------
838
431
335
----------------------------------------------------------------------------------------------------
parse(1) ➜ 1999 entries:
----------------------------------------------------------------------------------------------------
(838, 431, 335, 521, 410, 802, 598, 171, 638, 513, 379, 376, 781, 633, ...  305, 968, 478, 401, 463)
----------------------------------------------------------------------------------------------------

Solution

The key for today's puzzle is using Python's Counter class to count the number of occurrences for each ID. The most_common() method will then give us a list of the most common elements from the most to the least common. Each element of the list is a 2-tuple of (id, count), so we extract just the IDs using pattern matching in the following list comprehension. We don't need the count, so we use _ for it in the pattern:

[id for id, _ in Counter(parse(1, atom)).most_common()]

After that, the solution for the two parts is simply the product (prod()) of either the first 3 elements, or the last 3 elements.

Alternate Solution (tiny use of AI)

After manually coding the above solution, since it was so trivial, I wondered about a possible linear algebra solution, so I interacted with Chat GPT briefly about matrix operations. Although it's impractical in this example, it is possible to compute all products of three consecutive IDs using a dot product.

Consider a vector \(v = [a,b,c,d,e,f,g,h,i]\)

We want the sliding-window products: \([abc, bcd, cde, def, efg, fgh, ghi]\)

There is a matrix formulation using the Hadamard Product.

\[ v_0 = [a, b, c, d, e, f, g] \]
\[ v_1 = [b, c, d, e, f, g, h] \]
\[ v_2 = [c, d, e, f, g, h, i] \]

Then compute:

\[ r = v_0\cdot v_1\cdot v_2 \]

Then, in our case, the product of the 3 most common IDs is \(r_1\) and the product of the 3 least common is \(r_n\)

import numpy as np

v = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])

result = v[:-2] * v[1:-1] * v[2:] # array([  6,  24,  60, 120, 210, 336, 504])

End