From b8fd7080b68176a31a9d6ec6d094a61e41204fe1 Mon Sep 17 00:00:00 2001 From: Sijmen Schoon Date: Fri, 25 Dec 2020 21:12:18 +0100 Subject: [PATCH] rust(day15): Optimize day 15 to ~450 ms --- rust/src/day15.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/rust/src/day15.rs b/rust/src/day15.rs index e8e307c..6234319 100644 --- a/rust/src/day15.rs +++ b/rust/src/day15.rs @@ -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 = "0,1,5,10,3,12,19" + let input: Vec = "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 {