From 4f1f838d6296bb313d57f35bf5c551a1f6c90c41 Mon Sep 17 00:00:00 2001 From: Keenan Tims Date: Thu, 14 Dec 2023 15:41:34 -0800 Subject: [PATCH] day14: simple Vec lookup is faster than HashMap since the cycle is short --- 14/src/main.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/14/src/main.rs b/14/src/main.rs index 45c7375..e75b1e5 100644 --- a/14/src/main.rs +++ b/14/src/main.rs @@ -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)); } } }