the unidiomatic rust solution
pub fn part_1(input: &str) -> String {
input
.chars()
.map(|x| match x {
'r' => 0,
'o' => 1,
'y' => 2,
'g' => 3,
'b' => 4,
'v' => 5,
_ => unreachable!(),
})
.fold(0, |acc, x| {
let macc = acc % 6;
let jump = if macc >= x { x - macc + 6 } else { x - macc };
acc + jump
})
.to_string()
}
pub fn part_2(input: &str) -> String {
let instr: Vec<i32> = input
.chars()
.map(|x| match x {
'r' => 0,
'o' => 1,
'y' => 2,
'g' => 3,
'b' => 4,
'v' => 5,
_ => unreachable!(),
})
.collect();
let freq: Vec<(i32, usize)> = lib::sort(instr, |a, b| a.cmp(b))
.chunk_by(|a, b| a == b)
.map(|x| (*x.first().unwrap(), x.len()))
.collect();
(0..freq.len())
.fold((freq, 0), |acc, _| {
let (freq, n) = acc;
let mn = &(n % 6);
let (k, nk) = freq
.iter()
.max_by_key(|(k, _)| if mn >= k { k - mn + 6 } else { k - mn })
.unwrap();
let jump = if mn >= k { k - mn + 6 } else { k - mn } + 6 * (*nk as i32 - 1);
(
freq.iter().filter(|(a, _)| a != k).copied().collect(),
n + jump,
)
})
.1
.to_string()
}