Compare commits

..

No commits in common. "7ea1b80afea7366a995ee35425fd8f22940098bf" and "1c41e885ad262fef7bc60d13a5264c4b9a8f459b" have entirely different histories.

View File

@ -14,20 +14,20 @@ pub fn run() {
let mut target: Coord = Default::default(); let mut target: Coord = Default::default();
let map = lines let map = lines
.enumerate() .enumerate()
.map(|(row, l)| { .map(|(r, l)| {
l.chars() l.chars()
.enumerate() .enumerate()
.map(|(col, c)| match c { .map(|(col, c)| match c {
'S' => { 'S' => {
start = Coord { start = Coord {
row, row: r,
col, col,
}; };
0 0
} }
'E' => { 'E' => {
target = Coord { target = Coord {
row, row: r,
col, col,
}; };
25 25
@ -45,7 +45,7 @@ pub fn run() {
#[cfg(feature = "part2")] #[cfg(feature = "part2")]
{ {
let min = map let starting_points = map
.iter() .iter()
.enumerate() .enumerate()
.flat_map(|(row, v)| { .flat_map(|(row, v)| {
@ -58,6 +58,9 @@ pub fn run() {
}) })
.collect_vec() .collect_vec()
}) })
.collect_vec();
let min = starting_points
.iter()
.filter_map(|start| bfs(&map, start.clone(), target.clone())) .filter_map(|start| bfs(&map, start.clone(), target.clone()))
.min() .min()
.unwrap(); .unwrap();
@ -74,25 +77,48 @@ fn bfs(map: &Vec<Vec<u8>>, start: Coord, target: Coord) -> Option<usize> {
if next == target { if next == target {
return Some(path_length); return Some(path_length);
} }
for coord in [(1, 0), (-1, 0), (0, 1), (0, -1)] match make_coord((next.row as isize) + 1, next.col as isize, &map, &visited) {
.iter() Some(down) if map[next.row][next.col] + 1
.filter_map(|(dr, dc)|make_coord((next.row as isize) + dr, (next.col as isize) + dc, &map)) { >= map[down.row][down.col] => {
if map[next.row][next.col] + 1 visited[down.row][down.col] = true;
< map[coord.row][coord.col] || visited[coord.row][coord.col] { queue.push_back((down, path_length + 1));
continue; },
} _ => {}
visited[coord.row][coord.col] = true; }
queue.push_back((coord, 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));
},
_ => {}
} }
} }
None None
} }
fn make_coord(row: isize, col: isize, map: &Vec<Vec<u8>>) -> Option<Coord> { fn make_coord(row: isize, col: isize, map: &Vec<Vec<u8>>, visited: &Vec<Vec<bool>>) -> Option<Coord> {
if row >= 0 if row >= 0
&& col >= 0 && col >= 0
&& row < map.len() as isize && row < map.len() as isize
&& (col as usize) < map[row as usize].len() { && (col as usize) < map[row as usize].len()
&& !visited[row as usize][col as usize] {
Some(Coord { Some(Coord {
row: row as usize, row: row as usize,
col: col as usize col: col as usize