[rs] Optimize 2022_12

This commit is contained in:
Joscha 2022-12-15 16:37:50 +01:00
parent 3f2e2df74d
commit ff1a685166

View file

@ -1,16 +1,58 @@
use std::collections::{HashMap, HashSet}; use std::collections::HashSet;
use std::ops::{Index, IndexMut};
type Grid = HashMap<(i32, i32), u32>; struct Grid {
width: usize,
height: usize,
cells: Vec<u32>,
}
fn neighbours(grid: &Grid, (x, y): (i32, i32)) -> impl Iterator<Item = (i32, i32)> + '_ { impl Grid {
let height = grid.get(&(x, y)).cloned().unwrap_or(u32::MAX); fn new(width: usize, height: usize, initial: u32) -> Self {
Self {
width,
height,
cells: vec![initial; width * height],
}
}
fn contains(&self, pos: (i32, i32)) -> bool {
let x_in_bounds = 0 <= pos.0 && (pos.0 as usize) < self.width;
let y_in_bounds = 0 <= pos.1 && (pos.1 as usize) < self.height;
x_in_bounds && y_in_bounds
}
}
impl Index<(i32, i32)> for Grid {
type Output = u32;
fn index(&self, index: (i32, i32)) -> &Self::Output {
assert!(index.0 >= 0);
assert!(index.1 >= 0);
let (x, y) = (index.0 as usize, index.1 as usize);
assert!(x < self.width);
assert!(y < self.height);
self.cells.index(y * self.width + x)
}
}
impl IndexMut<(i32, i32)> for Grid {
fn index_mut(&mut self, index: (i32, i32)) -> &mut Self::Output {
assert!(index.0 >= 0);
assert!(index.1 >= 0);
let (x, y) = (index.0 as usize, index.1 as usize);
assert!(x < self.width);
assert!(y < self.height);
self.cells.index_mut(y * self.width + x)
}
}
fn backwards_neighbours(grid: &Grid, (x, y): (i32, i32)) -> impl Iterator<Item = (i32, i32)> + '_ {
// One step down or arbitrarily many steps up are allowed
let height = grid[(x, y)];
[(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)] [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)]
.into_iter() .into_iter()
.filter(move |n| { .filter(move |n| grid.contains(*n) && grid[*n].saturating_add(1) >= height)
let height2 = grid.get(n).cloned().unwrap_or(u32::MAX);
// One step down or arbitrarily many steps up are allowed
height2.saturating_add(1) >= height
})
} }
fn bfs(grid: &Grid, start: (i32, i32), until: impl Fn((i32, i32)) -> bool) -> usize { fn bfs(grid: &Grid, start: (i32, i32), until: impl Fn((i32, i32)) -> bool) -> usize {
@ -27,7 +69,7 @@ fn bfs(grid: &Grid, start: (i32, i32), until: impl Fn((i32, i32)) -> bool) -> us
return steps; return steps;
} }
for neighbour in neighbours(grid, pos) { for neighbour in backwards_neighbours(grid, pos) {
if !visited.contains(&neighbour) { if !visited.contains(&neighbour) {
visited.insert(neighbour); visited.insert(neighbour);
new_queue.insert(neighbour); new_queue.insert(neighbour);
@ -40,9 +82,12 @@ fn bfs(grid: &Grid, start: (i32, i32), until: impl Fn((i32, i32)) -> bool) -> us
} }
pub fn solve(input: String) { pub fn solve(input: String) {
let width = input.lines().next().unwrap().len();
let height = input.lines().count();
let mut grid = Grid::new(width, height, 0);
let mut start = (-1, -1); let mut start = (-1, -1);
let mut end = (-1, -1); let mut end = (-1, -1);
let mut grid = HashMap::new();
for (y, line) in input.lines().enumerate() { for (y, line) in input.lines().enumerate() {
for (x, c) in line.chars().enumerate() { for (x, c) in line.chars().enumerate() {
let pos = (x as i32, y as i32); let pos = (x as i32, y as i32);
@ -58,13 +103,13 @@ pub fn solve(input: String) {
_ => c, _ => c,
}; };
let height = height as u32 - 'a' as u32; let height = height as u32 - 'a' as u32;
grid.insert(pos, height); grid[pos] = height;
} }
} }
let part1 = bfs(&grid, end, |pos| pos == start); let part1 = bfs(&grid, end, |pos| pos == start);
println!("Part 1: {part1}"); println!("Part 1: {part1}");
let part2 = bfs(&grid, end, |pos| grid.get(&pos) == Some(&0)); let part2 = bfs(&grid, end, |pos| grid[pos] == 0);
println!("Part 2: {part2}"); println!("Part 2: {part2}");
} }