day14: simple Vec lookup is faster than HashMap since the cycle is short

This commit is contained in:
Keenan Tims 2023-12-14 15:41:34 -08:00
parent ca3254e1e1
commit 4f1f838d62
No known key found for this signature in database
GPG Key ID: B8FDD4AD6B193F06

View File

@ -1,5 +1,5 @@
use ndarray::*;
use std::collections::HashMap;
use std::fmt::Display;
use std::fs::File;
@ -162,14 +162,16 @@ impl<'a> Platform {
// find the first loop, return the iteration count when we first saw it and when we saw it again
fn find_loop(&mut self) -> (usize, usize) {
let mut first_seen = HashMap::new();
first_seen.insert(self.matrix.clone(), 0);
let mut first_seen = Vec::new();
first_seen.push((self.matrix.clone(), 0));
let mut i = 0;
loop {
self.roll_cycle();
i += 1;
if let Some(first_idx) = first_seen.insert(self.matrix.clone(), i) {
return (first_idx, i);
if let Some((_, first_idx)) = first_seen.iter().find(|(val, _)| *val == self.matrix) {
return (*first_idx, i);
} else {
first_seen.push((self.matrix.clone(), i));
}
}
}