use super::super::TRUST;
const CASE: u8 = b'a' - b'A';
pub fn inverses(x: u8, y: u8) -> bool {
CASE == match (x, y) {
(a, b) if a > b => a - b,
(a, b) if b > a => b - a,
_ => 0,
}
}
pub fn part1(input: &[u8]) -> usize {
let mut last: Vec<u8> = Vec::from(input);
let mut current: Vec<u8> = Vec::with_capacity(last.len());
let mut reduced = true;
while reduced {
reduced = false;
let mut index = 0;
while index < last.len() {
let unit = last[index];
let next = if index + 1 < last.len() {
last[index + 1]
} else {
0
};
if inverses(unit, next) {
reduced = true;
index += 2;
continue;
}
current.push(unit);
index += 1;
}
last.clear();
last.extend(current.drain(..))
}
last.len()
}
pub fn part2(input: &str) -> usize {
(b'a'..=b'z')
.map(|u| (u as char, ((u - CASE) as char)))
.map(|(l, u)| part1(input.replace(l, "").replace(u, "").as_bytes()))
.min()
.expect(TRUST)
}
#[test]
fn examples() {
let input = "dabAcCaCBAcCcaDA";
assert_eq!(10, part1(input.as_bytes()));
assert_eq!(4, part2(input));
}
#[test]
fn solution() {
let input = include_str!("input/5");
assert_eq!(11310, part1(input.as_bytes()));
assert_eq!(6020, part2(input))
}