1
0

Compare commits

..

2 Commits

Author SHA1 Message Date
fbc80cac85 day 16 part 2 2024-12-16 12:47:16 +01:00
fd3629acfd day 16 part 1
day 16 part 1
2024-12-16 12:32:59 +01:00
3 changed files with 162 additions and 0 deletions

160
src/day16.rs Normal file
View File

@@ -0,0 +1,160 @@
use std::{
collections::{BinaryHeap, HashMap},
fs::read_to_string,
};
use itertools::Itertools;
use crate::utils::grid::{Coord, Grid};
pub fn day_main() {
let input = read_to_string("input/day16.txt").unwrap();
let input = input.trim();
println!(" part1: {}", part1(input));
println!(" part2: {}", part2(input));
}
type RiddleResult = usize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Dir {
N,
E,
S,
W,
}
impl Dir {
fn nexts(&self) -> [Self; 2] {
match self {
Dir::N | Dir::S => [Dir::W, Dir::E],
Dir::E | Dir::W => [Dir::S, Dir::N],
}
}
}
struct Step(Coord, Dir, usize, Vec<Coord>);
impl Ord for Step {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
other.2.cmp(&self.2)
}
}
impl PartialOrd for Step {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl PartialEq for Step {
fn eq(&self, other: &Self) -> bool {
other.2.eq(&self.2)
}
}
impl Eq for Step {}
fn part1(input: &str) -> RiddleResult {
use Dir::*;
let maze = Grid::parse(input);
let start = maze.entries().find(|(_p, c)| **c == 'S').unwrap().0;
let end = maze.entries().find(|(_p, c)| **c == 'E').unwrap().0;
let mut visited = HashMap::<(Coord, Dir), usize>::new();
let mut stack: BinaryHeap<Step> = BinaryHeap::new();
stack.push(Step(start, E, 0, vec![]));
while let Some(Step(np, nd, cost, _)) = stack.pop() {
if visited.contains_key(&(np, nd)) {
continue;
}
visited.insert((np, nd), cost);
if np == end {
return cost;
}
for d in nd.nexts() {
if !visited.contains_key(&(np, d)) {
stack.push(Step(np, d, cost + 1000, vec![]));
}
}
let forward = match nd {
N => (np.0, np.1 - 1),
E => (np.0 + 1, np.1),
S => (np.0, np.1 + 1),
W => (np.0 - 1, np.1),
};
if maze[forward] != '#' && !visited.contains_key(&(forward, nd)) {
stack.push(Step(forward, nd, cost + 1, vec![]));
}
}
panic!("no path found")
}
fn part2(input: &str) -> RiddleResult {
use Dir::*;
let maze = Grid::parse(input);
let start = maze.entries().find(|(_p, c)| **c == 'S').unwrap().0;
let end = maze.entries().find(|(_p, c)| **c == 'E').unwrap().0;
let mut visited = HashMap::<(Coord, Dir), usize>::new();
let mut stack: BinaryHeap<Step> = BinaryHeap::new();
stack.push(Step(start, E, 0, vec![]));
let mut best: Option<usize> = None;
let mut visited_by_best: Vec<Coord> = vec![start, end];
while let Some(Step(np, nd, cost, mut pred)) = stack.pop() {
if let Some(b) = best {
if b < cost {
break; // can't reach the end point with best cost anymore
}
}
visited.insert((np, nd), cost);
if np == end {
best = Some(cost);
visited_by_best.append(&mut pred);
}
pred.push(np);
for d in nd.nexts() {
if !visited.contains_key(&(np, d)) {
stack.push(Step(np, d, cost + 1000, pred.clone()));
}
}
let forward = match nd {
N => (np.0, np.1 - 1),
E => (np.0 + 1, np.1),
S => (np.0, np.1 + 1),
W => (np.0 - 1, np.1),
};
if maze[forward] != '#' && !visited.contains_key(&(forward, nd)) {
stack.push(Step(forward, nd, cost + 1, pred));
}
}
visited_by_best.into_iter().unique().count()
}
#[cfg(test)]
mod test {
use super::{part1, part2};
const TEST_INPUT: &str = r"###############
#.......#....E#
#.#.###.#.###.#
#.....#.#...#.#
#.###.#####.#.#
#.#.#.......#.#
#.#.#####.###.#
#...........#.#
###.#.#####.#.#
#...#.....#.#.#
#.#.#.###.#.#.#
#.....#...#.#.#
#.###.#.#.#.#.#
#S..#.....#...#
###############
";
#[test]
fn test1() {
assert_eq!(part1(TEST_INPUT), 7036);
}
#[test]
fn test2() {
assert_eq!(part2(TEST_INPUT), 45);
}
}

View File

@@ -13,5 +13,6 @@ pub mod day12;
pub mod day13; pub mod day13;
pub mod day14; pub mod day14;
pub mod day15; pub mod day15;
pub mod day16;
// PLACEHOLDER // PLACEHOLDER
pub mod utils; pub mod utils;

View File

@@ -20,6 +20,7 @@ fn main() {
(13, day13::day_main), (13, day13::day_main),
(14, day14::day_main), (14, day14::day_main),
(15, day15::day_main), (15, day15::day_main),
(16, day16::day_main),
// PLACEHOLDER // PLACEHOLDER
]); ]);
let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok()); let day: Option<u8> = args().nth(1).and_then(|a| a.parse().ok());