From 476f34e2b42aac2e6c65df985a13d58c0a5e65f8 Mon Sep 17 00:00:00 2001 From: Johannes Date: Fri, 6 Dec 2024 07:32:13 +0100 Subject: [PATCH] day 6 part 2 perf --- src/day06.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/day06.rs b/src/day06.rs index 2a78c20..841f5ca 100644 --- a/src/day06.rs +++ b/src/day06.rs @@ -82,7 +82,7 @@ fn parse(input: &str) -> (HashMap, Point) { fn is_loop(m: &HashMap, block: Point, mut pos: Point, mut dir: char) -> bool { let mut visited = HashSet::new(); - while m.contains_key(&pos) { + loop { let (x, y) = pos; let next = match dir { @@ -92,8 +92,10 @@ fn is_loop(m: &HashMap, block: Point, mut pos: Point, mut dir: char '>' => (x + 1, y), _ => unreachable!(), }; - let blocked = next == block || Some(&'#') == m.get(&next); - if blocked { + let next_tile = m.get(&next); + if None == next_tile { + return false; + } else if next == block || Some(&'#') == next_tile { // we only check for loops on a collision to speed things up if visited.contains(&(pos, dir)) { return true; @@ -111,7 +113,6 @@ fn is_loop(m: &HashMap, block: Point, mut pos: Point, mut dir: char pos = next; } } - false } #[cfg(test)]