diff options
Diffstat (limited to '2024_rust/src/bin/day6.rs')
-rw-r--r-- | 2024_rust/src/bin/day6.rs | 39 |
1 files changed, 15 insertions, 24 deletions
diff --git a/2024_rust/src/bin/day6.rs b/2024_rust/src/bin/day6.rs index 5c78781..d8a4580 100644 --- a/2024_rust/src/bin/day6.rs +++ b/2024_rust/src/bin/day6.rs @@ -75,14 +75,11 @@ impl M { let mut visited = HashMap::new(); for i in 0..matrix.limit.0 { for j in 0..matrix.limit.1 { - match matrix.get((i, j)) { - Guard(dir) => { - let mut dirhs = HashSet::new(); - dirhs.insert(*dir); - visited.insert(guard, dirhs); - guard = (i, j); - } - _ => (), + if let Guard(dir) = matrix.get((i, j)) { + let mut dirhs = HashSet::new(); + dirhs.insert(*dir); + visited.insert(guard, dirhs); + guard = (i, j); } } } @@ -140,10 +137,7 @@ impl M { fn step_forever(&mut self) -> SimResult { loop { - match self.step() { - Some(res) => return res, - None => (), - } + if let Some(res) = self.step() { return res } } } @@ -165,7 +159,7 @@ impl M { impl fmt::Display for M { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // write!(f, "{}[2J", 27 as char)?; - write!(f, "{}", self.matrix.to_string())?; + write!(f, "{}", self.matrix)?; write!(f, "[Guard position: {:?}]", self.guard) } } @@ -184,18 +178,15 @@ fn count_loops(m: M) -> u32 { // let mut simulations = 0; for i in 0..m.matrix.limit.0 { for j in 0..m.matrix.limit.1 { - match m.matrix.get((i, j)) { - Empty => { - // simulations += 1; - let mut m1 = m.clone(); - m1.matrix.set((i, j), Obstacle); - // println!("Simulation {simulations} @ ({i},{j}) ({result})..."); - // println!("{m1}"); - if let SimResult::Loop = m1.step_forever() { - result += 1; - } + if let Empty = m.matrix.get((i, j)) { + // simulations += 1; + let mut m1 = m.clone(); + m1.matrix.set((i, j), Obstacle); + // println!("Simulation {simulations} @ ({i},{j}) ({result})..."); + // println!("{m1}"); + if let SimResult::Loop = m1.step_forever() { + result += 1; } - _ => (), } } } |