1
Fork 0
advent-of-code/source/year_2021/day_15/mod.rs

114 lines
2.9 KiB
Rust

//! Day 15 of 2021.
use crate::prelude::*;
/// Get the solution for day 15 of 2021.
pub fn solution() -> Solution {
Solution::new(Day::new(15, 2021), part_1, part_2).with_expected(386, 2806)
}
/// Type alias for a map of coordinates and risk levels.
type Grid = HashMap<Coordinate, isize>;
/// A coordinate in the grid.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
struct Coordinate(isize, isize);
impl Coordinate {
/// Get the distance between two [`Coordinate`]s.
fn distance(&self, target: &Coordinate) -> usize {
self.0.abs_diff(target.0) + self.1.abs_diff(target.1)
}
/// Calculate the successors of the grid based on this [`Coordinate`].
fn successors(&self, grid: &Grid) -> Vec<(Coordinate, isize)> {
let &Coordinate(x, y) = self;
let mut successors = vec![];
for coordinate in [
Coordinate(x, y - 1), // Up
Coordinate(x - 1, y), // Left
Coordinate(x, y + 1), // Down
Coordinate(x + 1, y), // Right
] {
if let Some(value) = grid.get(&coordinate) {
successors.push((coordinate, *value));
}
}
successors
}
}
/// Parse the [`Grid`] and end [`Coordinate`] from the puzzle input.
fn parse(input: &str) -> Result<(Grid, Coordinate)> {
let mut grid = Grid::new();
let mut end = Coordinate(0, 0);
for (y, line) in input.lines().enumerate() {
let y = (y + 1).try_into()?;
for (x, character) in line.char_indices() {
let x = (x + 1).try_into()?;
grid.insert(Coordinate(x, y), character.to_string().parse()?);
if end.0 < x && end.1 < y {
end = Coordinate(x, y);
}
}
}
Ok((grid, end))
}
/// Enlarge the grid by five times its size, accounting for the additional rules
/// specified in the puzzle.
fn enlarge_grid(grid: Grid, end: Coordinate) -> (Grid, Coordinate) {
let Coordinate(width, height) = end;
let mut larger_grid = grid.clone();
for (Coordinate(x, y), risk) in grid {
for step_x in 0..5 {
for step_y in 0..5 {
let risk = match (risk + step_x + step_y) % 9 {
0 => 9,
n => n,
};
let x = x + (width * step_x);
let y = y + (height * step_y);
larger_grid.insert(Coordinate(x, y), risk);
}
}
}
(larger_grid, Coordinate(width * 5, height * 5))
}
/// Solve the grid using [`astar`].
fn run(grid: Grid, end: Coordinate) -> Result<String> {
Ok(
astar(
&Coordinate(1, 1),
|p| p.successors(&grid),
|p| p.distance(&end).try_into().unwrap(),
|p| p == &end,
)
.ok_or_else(|| eyre!("No path found"))?
.1
.to_string(),
)
}
/// The logic to solve part one.
fn part_1(input: &str) -> Result<String> {
let (grid, end) = parse(input)?;
run(grid, end)
}
/// The logic to solve part two.
fn part_2(input: &str) -> Result<String> {
let (grid, end) = parse(input)?;
let (grid, end) = enlarge_grid(grid, end);
run(grid, end)
}