day 6 part 2 perf
This commit is contained in:
@@ -82,7 +82,7 @@ fn parse(input: &str) -> (HashMap<Point, char>, Point) {
|
|||||||
|
|
||||||
fn is_loop(m: &HashMap<Point, char>, block: Point, mut pos: Point, mut dir: char) -> bool {
|
fn is_loop(m: &HashMap<Point, char>, block: Point, mut pos: Point, mut dir: char) -> bool {
|
||||||
let mut visited = HashSet::new();
|
let mut visited = HashSet::new();
|
||||||
while m.contains_key(&pos) {
|
loop {
|
||||||
let (x, y) = pos;
|
let (x, y) = pos;
|
||||||
|
|
||||||
let next = match dir {
|
let next = match dir {
|
||||||
@@ -92,8 +92,10 @@ fn is_loop(m: &HashMap<Point, char>, block: Point, mut pos: Point, mut dir: char
|
|||||||
'>' => (x + 1, y),
|
'>' => (x + 1, y),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
let blocked = next == block || Some(&'#') == m.get(&next);
|
let next_tile = m.get(&next);
|
||||||
if blocked {
|
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
|
// we only check for loops on a collision to speed things up
|
||||||
if visited.contains(&(pos, dir)) {
|
if visited.contains(&(pos, dir)) {
|
||||||
return true;
|
return true;
|
||||||
@@ -111,7 +113,6 @@ fn is_loop(m: &HashMap<Point, char>, block: Point, mut pos: Point, mut dir: char
|
|||||||
pos = next;
|
pos = next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|||||||
Reference in New Issue
Block a user