rust(day15): Optimize day 15 to ~450 ms
This commit is contained in:
parent
fc9cfec98d
commit
b8fd7080b6
|
@ -1,23 +1,24 @@
|
||||||
use std::time::{Duration, Instant};
|
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 {
|
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(",")
|
.split(",")
|
||||||
.map(|i| i.parse().unwrap())
|
.map(|i| i.parse().unwrap())
|
||||||
.collect();
|
.collect();
|
||||||
let instant = Instant::now();
|
let instant = Instant::now();
|
||||||
|
|
||||||
let mut history = vec![0; END];
|
let mut history = vec![0u32; END as usize];
|
||||||
let mut last = input[0];
|
let mut last = input[0];
|
||||||
for turn in 0..input.len() {
|
for turn in 0..input.len() as u32 {
|
||||||
history[last] = turn;
|
history[last as usize] = turn as u32;
|
||||||
last = input[turn];
|
last = input[turn as usize];
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn in input.len()..2020 {
|
for turn in input.len() as u32..2020 {
|
||||||
let stored = history[last];
|
let stored = history[last as usize];
|
||||||
history[last] = turn;
|
history[last as usize] = turn;
|
||||||
last = if stored == 0 { 0 } else { turn - stored };
|
last = if stored == 0 { 0 } else { turn - stored };
|
||||||
}
|
}
|
||||||
if print {
|
if print {
|
||||||
|
@ -25,8 +26,8 @@ pub fn run(print: bool) -> Duration {
|
||||||
}
|
}
|
||||||
|
|
||||||
for turn in 2020..END {
|
for turn in 2020..END {
|
||||||
let stored = history[last];
|
let stored = history[last as usize];
|
||||||
history[last] = turn;
|
history[last as usize] = turn;
|
||||||
last = if stored == 0 { 0 } else { turn - stored };
|
last = if stored == 0 { 0 } else { turn - stored };
|
||||||
}
|
}
|
||||||
if print {
|
if print {
|
||||||
|
|
Loading…
Reference in New Issue