Nick Belvin

← blog

Advent of Code 2026: Day 1 - Dusty Shed

2026-12-01 by Nick Belvin

Day 1: Dusty Shed

Advent of Code 2026 starts with a shed full of containers labeled by ID. Part One asks for the product of the three most common IDs; Part Two asks for the product of the three least common.

My Rust implementation handles both with the same idea: count each ID, put the counts into a heap, and pop three entries.

1. Count the IDs with a HashMap

Both functions begin with this code:

let lines: Vec<&str> = input.lines().collect();
let occurances = lines.iter().fold(HashMap::new(), |mut acc, &value| {
    *acc.entry(value).or_insert(0) += 1;
    acc
});

input.lines() splits the input into borrowed string slices. Keeping IDs as &str means I don't need to parse every line into a number just to count it.

The fold builds a frequency map. For each ID, entry(value).or_insert(0) returns a mutable reference to its existing count, or inserts zero if this is its first appearance. Dereferencing and incrementing that count records another container. Returning acc passes the updated map into the next iteration.

2. Part One: Most Common First

let mut heap = occurances.iter().fold(BinaryHeap::new(), |mut acc, (&key, &value)| {
    acc.push((value, key));
    acc
});

Rust's BinaryHeap is a max-heap: pop() removes the greatest entry. I store each entry as (count, id), so tuple ordering compares the frequency first. That puts the most common IDs at the front without sorting the original input.

let mut result = 1;
for _ in 0..3 {
    if let Some((count, key)) = heap.pop() {
        result *= key.parse::<i32>().unwrap_or(0);
    } else {
        break;
    }
}

Starting at one gives multiplication its identity value. Each pop selects another ID, which is then parsed and multiplied into the result. The count determines selection but isn't needed in the multiplication.

My Part One answer was 485695285.

3. Part Two: Flip the Frequency Ordering

The counting step stays the same. The change is in the heap entries:

let mut heap = occurances.iter().fold(BinaryHeap::new(), |mut acc, (&key, &value)| {
    acc.push((Reverse(value), key));
    acc
});

Reverse comes from std::cmp. Wrapping the count reverses its ordering, so smaller frequencies now have higher priority in the max-heap.

The multiplication loop follows the same pattern, unpacking the wrapper:

if let Some((Reverse(_), key)) = heap.pop() {
    result *= key.parse::<i32>().unwrap_or(0);
} else {
    break;
}

Only the frequency is reversed, not the whole tuple. Equal frequencies therefore still use the ID's descending string order as a tiebreaker in both parts.

My Part Two answer was 104035328, completing both stars!

Complexity and Takeaways

For n input lines and k distinct IDs, counting takes expected O(n) time in the usual model that treats ID hashing as constant-cost. Building the heap with repeated push calls takes O(k log k) worst-case time, and the three pops take O(log k) overall. The vector of lines uses O(n) extra space, while the map and heap each use O(k).

There are a couple of assumptions worth noting. The loops stop early if fewer than three distinct IDs exist, and unwrap_or(0) silently turns an invalid or out-of-range i32 ID into zero. Those would be good places for explicit validation in a reusable implementation. Folding directly over input.lines() would also avoid the intermediate vector.

The useful Rust takeaway here is how little needs to change between the two parts: the same frequency map and max-heap work for both, with Reverse changing which counts come out first.