From f2d6ff15e8934ea4677f4e9016501d1b52677667 Mon Sep 17 00:00:00 2001 From: "max.nuding" Date: Mon, 12 Dec 2022 13:47:54 +0100 Subject: [PATCH] dedpulicated coord checking --- src/day12/mod.rs | 45 +++++++++++---------------------------------- 1 file changed, 11 insertions(+), 34 deletions(-) diff --git a/src/day12/mod.rs b/src/day12/mod.rs index 21b2de6..9d6eebd 100644 --- a/src/day12/mod.rs +++ b/src/day12/mod.rs @@ -77,48 +77,25 @@ fn bfs(map: &Vec>, start: Coord, target: Coord) -> Option { if next == target { return Some(path_length); } - match make_coord((next.row as isize) + 1, next.col as isize, &map, &visited) { - Some(down) if map[next.row][next.col] + 1 - >= map[down.row][down.col] => { - visited[down.row][down.col] = true; - queue.push_back((down, path_length + 1)); - }, - _ => {} - } - match make_coord((next.row as isize) - 1, next.col as isize, &map, &visited) { - Some(up) if map[next.row][next.col] + 1 - >= map[up.row][up.col] => { - visited[up.row][up.col] = true; - queue.push_back((up, path_length + 1)); - }, - _ => {} - } - match make_coord(next.row as isize, (next.col as isize) - 1, &map, &visited) { - Some(left) if map[next.row][next.col] + 1 - >= map[left.row][left.col] => { - visited[left.row][left.col] = true; - queue.push_back((left, path_length + 1)); - }, - _ => {} - } - match make_coord(next.row as isize, (next.col as isize) + 1, &map, &visited) { - Some(right) if map[next.row][next.col] + 1 - >= map[right.row][right.col] => { - visited[right.row][right.col] = true; - queue.push_back((right, path_length + 1)); - }, - _ => {} + for coord in [(1, 0), (-1, 0), (0, 1), (0, -1)] + .iter() + .filter_map(|(dr, dc)|make_coord((next.row as isize) + dr, (next.col as isize) + dc, &map)) { + if map[next.row][next.col] + 1 + < map[coord.row][coord.col] || visited[coord.row][coord.col] { + continue; + } + visited[coord.row][coord.col] = true; + queue.push_back((coord, path_length + 1)); } } None } -fn make_coord(row: isize, col: isize, map: &Vec>, visited: &Vec>) -> Option { +fn make_coord(row: isize, col: isize, map: &Vec>) -> Option { if row >= 0 && col >= 0 && row < map.len() as isize - && (col as usize) < map[row as usize].len() - && !visited[row as usize][col as usize] { + && (col as usize) < map[row as usize].len() { Some(Coord { row: row as usize, col: col as usize