Advent of Code 2026
This year I'm focusing on solving puzzles in a more functional style (as opposed to last year where I solved everything using fairly standard C). I'm also going to take the opportunity to learn more about third-party Rust crates. So far in my Rust exploration I've avoided crates, so hopefully this will be a good learning experience.
Day 1
For day 1 we are given a list of ID's and need to report the product of the least/most frequent IDs.
My initial solution for day 1 was a simple fold over the data collecting counts in a HashMap, sorting based on counts, then taking the product of the front 3 and back 3 elements. Adding in 2 crates (itertools and tap) allowed me to remove some of the imperative code and reduce everything to a single functional chain as seen below.
itertools provides counts which replaces my manual frequency count fold loop. Once we have frequency counts we have a few options. My initial solution would have just collected these into a vector, sorted, and taken the front/back slices. However, this requires collecting and then taking the slices. We can instead avoid collecting (thus keeping memory usage "constant") and take a single iteration over the data keeping track of the least/most frequent 3 ids. There are a number of ways to accomplish this, but simplest imo is to use 2 heaps, and min-heap and a max-heap. We fill each heap (making sure to enforce the ordering) and pop excess element. Thankfully we don't actually care about the individual ordering of elements, so a heap is good enough.
Once we have our 2 heaps we can either bind these to local variables, convert them to iterators, and take their product. Or we can include the helpful tap crate, which provides a pipe method to directly consume the (front, back) tuple. This allows us to represent the entire solve function as a single chain of method calls.
For good measure I added a unit test which just asserts we can pass the 2 provided examples.
So far, the fold (or reduce) pattern is my favorite technique for solving puzzles like these. They allow me to avoid creating local variables, and from the few 2020 puzzles I went back to solves over the past week, the pattern can be applied to most problems.
use std::{cmp::Reverse, collections::BinaryHeap};
use itertools::Itertools;
use tap::Pipe;
const INPUT: &str = include_str!("../part1.input");
fn main() {
let (p1, p2) = solve(INPUT);
println!("part1: {p1}");
println!("part2: {p2}");
}
fn solve(input: &str) -> (u64, u64) {
input
.trim()
.lines()
.map(|x| x.parse::<u64>().unwrap())
.counts()
.into_iter()
.fold(
(BinaryHeap::new(), BinaryHeap::new()),
|mut acc, (value, count)| {
// .0 is 3 smallest
// .1 is 3 largest
acc.0.push((count, value));
acc.1.push(Reverse((count, value)));
if acc.0.len() > 3 {
acc.0.pop();
}
if acc.1.len() > 3 {
acc.1.pop();
}
acc
},
)
.pipe(|(front, back)| {
(
back.iter().map(|Reverse((_, value))| value).product(),
front.iter().map(|(_, x)| x).product(),
)
})
}
#[cfg(test)]
mod tests {
use super::*;
const EXAMPLE_INPUT: &str = "57
57
55
57
76
78
57
45
43
78
43
76
43
78
57
45
57
76
43
43
76
";
#[test]
fn examples() {
let (part1, part2) = solve(EXAMPLE_INPUT);
assert_eq!(part1, 186276);
assert_eq!(part2, 193050);
}
}
Bash
Just because we can…
sort aoc2026/day1/part1.input | # Produces a sorted list of the input IDs
uniq --count | # Produces tuples of count, ID
sort -n | # Sort tuples by count
tr -s ' ' | # `uniq` pads the front with many spaces, compress into 1 space
cut -d' ' -f3 | # Line is (space, count, ID), grab the third element (ID)
{
mapfile -t n; # Collect IDs into a bash array
echo part1: $((n[-3] * n[-2] * n[-1])); echo part2: $((n[0] * n[1] * n[2])); # Product of back 3 and front 3
}
part1: 186276 part2: 193050