From ff1a6851669f0548e8d5ea7eb844a9db2e66e902 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 15 Dec 2022 16:37:50 +0100 Subject: [PATCH] [rs] Optimize 2022_12 --- rs/src/y2022/d12.rs | 71 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 13 deletions(-) diff --git a/rs/src/y2022/d12.rs b/rs/src/y2022/d12.rs index 8ddc697..cb5eb05 100644 --- a/rs/src/y2022/d12.rs +++ b/rs/src/y2022/d12.rs @@ -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, +} -fn neighbours(grid: &Grid, (x, y): (i32, i32)) -> impl Iterator + '_ { - let height = grid.get(&(x, y)).cloned().unwrap_or(u32::MAX); +impl Grid { + 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 + '_ { + // 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)] .into_iter() - .filter(move |n| { - 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 - }) + .filter(move |n| grid.contains(*n) && grid[*n].saturating_add(1) >= height) } 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; } - for neighbour in neighbours(grid, pos) { + for neighbour in backwards_neighbours(grid, pos) { if !visited.contains(&neighbour) { visited.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) { + 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 end = (-1, -1); - let mut grid = HashMap::new(); for (y, line) in input.lines().enumerate() { for (x, c) in line.chars().enumerate() { let pos = (x as i32, y as i32); @@ -58,13 +103,13 @@ pub fn solve(input: String) { _ => c, }; let height = height as u32 - 'a' as u32; - grid.insert(pos, height); + grid[pos] = height; } } let part1 = bfs(&grid, end, |pos| pos == start); 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}"); }