From 29ad35f8c97b0adb61987ff51cee122814927c73 Mon Sep 17 00:00:00 2001 From: Justin Wernick Date: Sun, 17 Dec 2023 12:53:56 +0200 Subject: Day 17 part 2 --- 2023/src/bin/day_17.rs | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to '2023/src/bin') diff --git a/2023/src/bin/day_17.rs b/2023/src/bin/day_17.rs index 99d828b..8d839e5 100644 --- a/2023/src/bin/day_17.rs +++ b/2023/src/bin/day_17.rs @@ -10,7 +10,8 @@ use std::{collections::HashSet, fs}; fn main() -> Result<(), Box> { let input = fs::read_to_string("inputs/day_17.txt")?; let parsed = HeatlossMap::parser(&input).unwrap().1; - dbg!(&parsed.find_shortest_heatloss_path()); + dbg!(&parsed.find_shortest_heatloss_path(false)); + dbg!(&parsed.find_shortest_heatloss_path(true)); Ok(()) } @@ -44,7 +45,7 @@ impl HeatlossMap { )(input) } - fn find_shortest_heatloss_path(&self) -> u32 { + fn find_shortest_heatloss_path(&self, is_ultra_crucible: bool) -> u32 { let start = Position { pos: Point2::new(0, 0), facing: Vector2::x_axis(), @@ -64,25 +65,28 @@ impl HeatlossMap { let (front_distance, front_position) = frontier.pop().unwrap(); let mut next_points: Vec = Vec::new(); - { - let facing = rotate_left(&front_position.facing); - let pos = front_position.pos + *facing; + + if !is_ultra_crucible || front_position.duration_with_facing >= 4 { + let facing_l = rotate_left(&front_position.facing); + let pos_l = front_position.pos + *facing_l; next_points.push(Position { - pos, - facing, + pos: pos_l, + facing: facing_l, duration_with_facing: 1, }); - } - { - let facing = rotate_right(&front_position.facing); - let pos = front_position.pos + *facing; + + let facing_r = rotate_right(&front_position.facing); + let pos_r = front_position.pos + *facing_r; next_points.push(Position { - pos, - facing, + pos: pos_r, + facing: facing_r, duration_with_facing: 1, }); } - if front_position.duration_with_facing < 3 { + + if (is_ultra_crucible && front_position.duration_with_facing < 10) + || (!is_ultra_crucible && front_position.duration_with_facing < 3) + { let pos = front_position.pos + *front_position.facing; next_points.push(Position { pos, @@ -99,7 +103,13 @@ impl HeatlossMap { let distance = front_distance + step_distance; if next_point.pos == end_pos { - distance_to_end = Some(distance); + if is_ultra_crucible { + if next_point.duration_with_facing >= 4 { + distance_to_end = Some(distance); + } + } else { + distance_to_end = Some(distance); + } } frontier.push((distance, next_point)); } -- cgit v1.2.3