Files
aoc2016/src/days/d14.rs
T

72 lines
1.8 KiB
Rust
Raw Normal View History

2024-11-25 17:23:45 +03:00
use crate::prelude::*;
pub type O = usize;
2024-11-25 17:29:25 +03:00
pub const INPUT: &'static str = "qzyelonm";
2024-11-25 17:23:45 +03:00
2024-11-25 17:29:25 +03:00
fn _solve (extra: u32) -> O {
2024-11-25 17:23:45 +03:00
let mut candidates: M<char, Vec<usize>> = M::new();
let mut done: Vec<usize> = vec![];
for i in 0.. {
2024-11-25 17:29:25 +03:00
let s = format!("{INPUT}{i}");
2024-11-25 17:23:45 +03:00
let mut hash = md5::compute(s);
let mut repr = format!("{:x}", hash);
2024-11-25 17:29:25 +03:00
for _ in 0..extra {
2024-11-25 17:23:45 +03:00
hash = md5::compute(repr);
repr = format!("{:x}", hash);
}
let three = repr.chars().tuple_windows().find(|&(a, b, c)| a == b && b == c);
if let Some((c, _, _)) = three {
if !candidates.is_empty() {
let five = repr.chars().tuple_windows().find(|&(a, b, c, d, e)| a == b && a == c && a == d && a == e);
if let Some((c, _, _, _, _)) = five {
if let Some(candidates) = candidates.get_mut(&c) {
let pos = candidates.iter().position(|pos| pos + 1000 > i);
if let Some(pos) = pos {
2024-11-25 17:29:25 +03:00
done.extend(candidates.drain(pos..));
2024-11-25 17:23:45 +03:00
candidates.clear();
2024-11-25 17:29:25 +03:00
if done.len() >= 100 { // should be pretty solid, but not sure if I can prove this!
2024-11-25 17:23:45 +03:00
done.sort();
return done[63];
}
}
}
}
}
candidates.entry(c).or_default().push(i);
}
}
0
}
2024-11-25 17:29:25 +03:00
fn _silver () -> O {
_solve(0)
}
fn _gold () -> O {
_solve(2016)
}
2024-11-25 17:23:45 +03:00
#[cfg(test)]
mod test {
use super::*;
#[test]
fn silver () {
2024-11-25 17:29:25 +03:00
let ans = _silver();
2024-11-25 17:23:45 +03:00
assert_eq!(ans, 15168)
}
#[test]
fn gold () {
2024-11-25 17:29:25 +03:00
let ans = _gold();
2024-11-25 17:23:45 +03:00
assert_eq!(ans, 20864)
}
}