use nalgebra::Point2; use nom::{ branch::alt, character::complete::{char, line_ending}, combinator::{map, value}, multi::{many1, separated_list1}, IResult, }; use std::{collections::HashSet, fs}; fn main() -> Result<(), Box> { let input = fs::read_to_string("inputs/day_23.txt")?; let forest_map = ForestMap::parser(&input).unwrap().1; dbg!(forest_map.longest_end_path_length(true)); dbg!(forest_map.longest_end_path_length(false)); Ok(()) } #[derive(Debug)] struct ForestMap(Vec>); #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum ForestTile { Wall, Open, SlopeUp, SlopeDown, SlopeLeft, SlopeRight, } #[derive(Debug, Clone)] struct DecisionNode { explored: HashSet>, current: Point2, } impl ForestMap { fn parser(input: &str) -> IResult<&str, Self> { map( separated_list1(line_ending, many1(ForestTile::parser)), ForestMap, )(input) } fn longest_end_path_length(&self, slippery: bool) -> usize { let start_point = Point2::new(1, 0); let end_point = Point2::new(self.0[0].len() - 2, self.0.len() - 1); let mut active_nodes = vec![DecisionNode { explored: HashSet::new(), current: start_point, }]; active_nodes[0].explored.insert(start_point); let mut longest_end_path_length: Option = None; while let Some(mut node) = active_nodes.pop() { let mut all_adjacent = self.adjacent(&node.current, &node.explored, slippery); while all_adjacent.len() == 1 { let adjacent = all_adjacent[0]; node.explored.insert(adjacent); node.current = adjacent; if node.current == end_point { let end_path_length = node.path_length(); longest_end_path_length = if let Some(current_longest) = longest_end_path_length { Some(current_longest.max(end_path_length)) } else { Some(end_path_length) }; all_adjacent = vec![]; } else { all_adjacent = self.adjacent(&node.current, &node.explored, slippery); } } for adjacent in all_adjacent { let mut new_node = node.clone(); new_node.explored.insert(adjacent); new_node.current = adjacent; if new_node.current == end_point { let end_path_length = new_node.path_length(); longest_end_path_length = if let Some(current_longest) = longest_end_path_length { Some(current_longest.max(end_path_length)) } else { Some(end_path_length) }; } else { active_nodes.push(new_node); } } } longest_end_path_length.unwrap() } fn adjacent( &self, p: &Point2, not_these: &HashSet>, slippery: bool, ) -> Vec> { let mut adjacent = Vec::with_capacity(4); let tile = self.at(p); if p.x > 0 && (!slippery || matches!(tile, ForestTile::Open | ForestTile::SlopeLeft)) { adjacent.push(Point2::new(p.x - 1, p.y)); } if p.y > 0 && (!slippery || matches!(tile, ForestTile::Open | ForestTile::SlopeUp)) { adjacent.push(Point2::new(p.x, p.y - 1)); } if p.x < self.0[p.y].len() - 1 && (!slippery || matches!(tile, ForestTile::Open | ForestTile::SlopeRight)) { adjacent.push(Point2::new(p.x + 1, p.y)); } if p.y < self.0.len() - 1 && (!slippery || matches!(tile, ForestTile::Open | ForestTile::SlopeDown)) { adjacent.push(Point2::new(p.x, p.y + 1)); } adjacent.retain(|adj_p| self.at(adj_p) != ForestTile::Wall && !not_these.contains(adj_p)); adjacent } fn at(&self, p: &Point2) -> ForestTile { self.0[p.y][p.x] } } impl ForestTile { fn parser(input: &str) -> IResult<&str, Self> { alt(( value(ForestTile::Wall, char('#')), value(ForestTile::Open, char('.')), value(ForestTile::SlopeUp, char('^')), value(ForestTile::SlopeDown, char('v')), value(ForestTile::SlopeLeft, char('<')), value(ForestTile::SlopeRight, char('>')), ))(input) } } impl DecisionNode { fn path_length(&self) -> usize { self.explored.len() - 1 } }