2020/rust/src/day15.rs

38 lines
900 B
Rust
Raw Normal View History

2020-12-22 14:40:04 +00:00
use std::time::{Duration, Instant};
const END: usize = 30_000_000;
pub fn run(print: bool) -> Duration {
let input: Vec<usize> = "0,1,5,10,3,12,19"
.split(",")
.map(|i| i.parse().unwrap())
.collect();
let instant = Instant::now();
let mut history = vec![0; END];
let mut last = input[0];
for turn in 0..input.len() {
history[last] = turn;
last = input[turn];
}
for turn in input.len()..2020 {
let stored = history[last];
history[last] = turn;
last = if stored == 0 { 0 } else { turn - stored };
}
if print {
println!("Part 1: {}", last);
}
for turn in 2020..END {
let stored = history[last];
history[last] = turn;
last = if stored == 0 { 0 } else { turn - stored };
}
if print {
println!("Part 2: {}", last);
}
instant.elapsed()
}