day14: simple Vec lookup is faster than HashMap since the cycle is short
This commit is contained in:
		@@ -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));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user