rust(day15): Optimize day 15 to ~450 ms

This commit is contained in:
Sijmen 2020-12-25 21:12:18 +01:00
parent fc9cfec98d
commit b8fd7080b6
Signed by: vijfhoek
GPG Key ID: DAF7821E067D9C48
1 changed files with 12 additions and 11 deletions

View File

@ -1,23 +1,24 @@
use std::time::{Duration, Instant};
use std::boxed::Box;
const END: usize = 30_000_000;
const END: u32 = 30_000_000;
pub fn run(print: bool) -> Duration {
let input: Vec<usize> = "0,1,5,10,3,12,19"
let input: Vec<u32> = "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 history = vec![0u32; END as usize];
let mut last = input[0];
for turn in 0..input.len() {
history[last] = turn;
last = input[turn];
for turn in 0..input.len() as u32 {
history[last as usize] = turn as u32;
last = input[turn as usize];
}
for turn in input.len()..2020 {
let stored = history[last];
history[last] = turn;
for turn in input.len() as u32..2020 {
let stored = history[last as usize];
history[last as usize] = turn;
last = if stored == 0 { 0 } else { turn - stored };
}
if print {
@ -25,8 +26,8 @@ pub fn run(print: bool) -> Duration {
}
for turn in 2020..END {
let stored = history[last];
history[last] = turn;
let stored = history[last as usize];
history[last as usize] = turn;
last = if stored == 0 { 0 } else { turn - stored };
}
if print {