aoc2016/src/days/d15.rs

59 lines
1.1 KiB
Rust
Raw Normal View History

2024-11-26 20:39:03 +00:00
use crate::prelude::*;
pub type I = Vec<(usize, usize)>;
pub type O = usize;
fn _parse (data: &str) -> I {
data.lines()
.map(|e| e.trim().trim_matches('.')
.split_once(" has ")
.map(|(_, r)|
r.split_once(" positions; at time=0, it is at position ")
.map(|(n, p)| (n.parse().unwrap(), p.parse().unwrap()))
)
.flatten()
.unwrap()
)
.collect()
}
fn _solve (data: &I) -> O {
let d = |time, (size, position)| (time + position) % size == 0;
(0..).into_iter().find(|t| data.iter().enumerate().all(|(i, &df)| d(t + i + 1, df))).unwrap()
}
fn _silver (data: I) -> O {
_solve(&data)
}
fn _gold (mut data: I) -> O {
data.push((11, 0));
_solve(&data)
}
#[cfg(test)]
mod test {
use super::*;
fn read () -> I {
let data = inc!(15);
_parse(data)
}
#[test]
fn silver () {
let data = read();
let ans = _silver(data);
assert_eq!(ans, 122318)
}
#[test]
fn gold () {
let data = read();
let ans = _gold(data);
assert_eq!(ans, 3208583)
}
}