Rust
fn parts(input: &str) -> (String, String) {
let counts = input
.split(|x| x == '\n')
.flat_map(|numst| numst.parse::<u64>().ok())
.counts();
let part1: u64 = counts
.iter()
.k_largest_by_key(3, |x| x.1)
.map(|x| x.0)
.product();
let part2: u64 = counts
.iter()
.k_smallest_by_key(3, |x| x.1)
.map(|x| x.0)
.product();
(part1.to_string(), part2.to_string())
}
Rust
For part 1, I count the number of wrap-arounds as consecutive colors that are not increasing and check the last color to see where mod 6 it ends up. To make this work nicely, I moved the
rto the end of the colors and added a dummy color at the beginning, such that a singlerends up at 6.For part 2, the resulting program would always be a sorted list of colors, in the reverse order of my character list. Most characters will add 6 to the total. Only at the transition between color segments is the distance reduced, and the reduction corresponds exactly to the difference between the color values. Because the program must be sorted, the total reduction is the difference between the first and the last color. And since the sorting order would be the reverse of my character list, I end up with the inverse difference (mod 6) and add it instead of subtracting.