|
| 1 | +use crate::solutions::year2024::day21::Key::{Activate, Dir}; |
| 2 | +use crate::solutions::Solution; |
| 3 | +use crate::utils::direction::Direction; |
| 4 | +use crate::utils::graphs::a_star::AStarBuilder; |
| 5 | +use crate::utils::grid::Grid; |
| 6 | +use crate::utils::point::Point; |
| 7 | +use itertools::Itertools; |
| 8 | +use std::collections::HashMap; |
| 9 | +use std::fmt::{Display, Formatter}; |
| 10 | + |
| 11 | +type Positions = HashMap<u8, Point>; |
| 12 | +type Adjacent = HashMap<Point, Vec<Point>>; |
| 13 | + |
| 14 | +const NUM_PAD: &str = r#"789 |
| 15 | +456 |
| 16 | +123 |
| 17 | +.0A"#; |
| 18 | +const NUM_PAD_ELEMENTS: [u8; 11] = [ |
| 19 | + b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', |
| 20 | +]; |
| 21 | +const ARROW_PAD: &str = r#".^A |
| 22 | +<v>"#; |
| 23 | +const ARROW_PAD_ELEMENTS: [u8; 5] = [b'^', b'v', b'<', b'>', b'A']; |
| 24 | + |
| 25 | +pub struct Day21; |
| 26 | + |
| 27 | +impl Solution for Day21 { |
| 28 | + fn part_one(&self, input: &str) -> String { |
| 29 | + let pads = vec![Pad::numeric(), Pad::arrow(), Pad::arrow()]; |
| 30 | + |
| 31 | + input |
| 32 | + .lines() |
| 33 | + .map(|line| { |
| 34 | + let path_len = self.path_len(line, &pads); |
| 35 | + let num: usize = line.trim_end_matches('A').parse().unwrap(); |
| 36 | + |
| 37 | + // println!("{} * {}", path_len, num); |
| 38 | + num * path_len |
| 39 | + }) |
| 40 | + .sum::<usize>() |
| 41 | + .to_string() |
| 42 | + } |
| 43 | + |
| 44 | + fn part_two(&self, _input: &str) -> String { |
| 45 | + String::from('0') |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +impl Day21 { |
| 50 | + fn path_len(&self, code: &str, pads: &Vec<Pad>) -> usize { |
| 51 | + let mut current = code.to_string(); |
| 52 | + |
| 53 | + for pad in pads { |
| 54 | + let path = self.path_for_str(¤t, pad); |
| 55 | + |
| 56 | + current = path.iter().map(|key| key.to_string()).collect::<String>(); |
| 57 | + |
| 58 | + // println!("{}", current); |
| 59 | + } |
| 60 | + |
| 61 | + current.chars().count() |
| 62 | + } |
| 63 | + fn path_for_str(&self, code: &str, pad: &Pad) -> Vec<Key> { |
| 64 | + let code = "A".to_owned() + code; |
| 65 | + let a_position = pad.position(b'A').unwrap(); |
| 66 | + |
| 67 | + let neighbours = |p: Point| pad.adjacent(&p); |
| 68 | + let distance = |p1: Point, p2: Point| { |
| 69 | + p1.manhattan_distance(&p2) as usize + p2.manhattan_distance(&a_position) as usize |
| 70 | + }; |
| 71 | + |
| 72 | + let a_star = AStarBuilder::init(&neighbours, &distance).build(); |
| 73 | + |
| 74 | + code.chars() |
| 75 | + .tuple_windows() |
| 76 | + .flat_map(|(from, to)| { |
| 77 | + let path = a_star |
| 78 | + .path( |
| 79 | + pad.position(from as u8).unwrap(), |
| 80 | + pad.position(to as u8).unwrap(), |
| 81 | + ) |
| 82 | + .unwrap(); |
| 83 | + let mut directions: Vec<Key> = path |
| 84 | + .windows(2) |
| 85 | + .map(|pair| Dir(pair[0].direction(&pair[1]).unwrap())) |
| 86 | + .collect(); |
| 87 | + directions.push(Activate); |
| 88 | + |
| 89 | + // println!("{from} {to} -> {:?}", directions.iter().map(|d| d.to_string()).collect::<String>()); |
| 90 | + |
| 91 | + directions.into_iter() |
| 92 | + }) |
| 93 | + .collect() |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[derive(Debug, PartialEq)] |
| 98 | +enum Key { |
| 99 | + Dir(Direction), |
| 100 | + Activate, |
| 101 | +} |
| 102 | + |
| 103 | +impl Display for Key { |
| 104 | + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { |
| 105 | + let v = match self { |
| 106 | + Dir(d) => match d { |
| 107 | + Direction::North => "^", |
| 108 | + Direction::South => "v", |
| 109 | + Direction::East => ">", |
| 110 | + Direction::West => "<", |
| 111 | + _ => unreachable!(), |
| 112 | + }, |
| 113 | + Activate => "A", |
| 114 | + }; |
| 115 | + |
| 116 | + write!(f, "{}", v) |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +struct Pad { |
| 121 | + positions: Positions, |
| 122 | + adjacent: Adjacent, |
| 123 | +} |
| 124 | + |
| 125 | +impl Pad { |
| 126 | + fn numeric() -> Self { |
| 127 | + let positions = Self::build_positions(NUM_PAD, &NUM_PAD_ELEMENTS); |
| 128 | + let adjacent = Self::build_adjacent(&positions); |
| 129 | + |
| 130 | + Self { |
| 131 | + positions, |
| 132 | + adjacent, |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + fn arrow() -> Self { |
| 137 | + let positions = Self::build_positions(ARROW_PAD, &ARROW_PAD_ELEMENTS); |
| 138 | + let adjacent = Self::build_adjacent(&positions); |
| 139 | + |
| 140 | + Self { |
| 141 | + positions, |
| 142 | + adjacent, |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + fn build_positions(map: &str, elements: &[u8]) -> Positions { |
| 147 | + let num_grid: Grid<u8> = Grid::from_custom(map, |c| c as u8); |
| 148 | + let mut num_pad_positions: Positions = HashMap::with_capacity(num_grid.surface().area()); |
| 149 | + |
| 150 | + for i in elements { |
| 151 | + num_pad_positions.insert(*i, num_grid.get_first_position(i).unwrap()); |
| 152 | + } |
| 153 | + |
| 154 | + num_pad_positions |
| 155 | + } |
| 156 | + |
| 157 | + fn build_adjacent(num_pad_map: &Positions) -> Adjacent { |
| 158 | + let mut adjacent_map = HashMap::with_capacity(num_pad_map.len()); |
| 159 | + |
| 160 | + for pos in num_pad_map.values() { |
| 161 | + let adjacent: Vec<Point> = pos |
| 162 | + .adjacent() |
| 163 | + .iter() |
| 164 | + .filter_map(|p| num_pad_map.iter().find(|(_, v)| **v == *p)) |
| 165 | + .map(|(_, p)| *p) |
| 166 | + .collect(); |
| 167 | + |
| 168 | + adjacent_map.insert(*pos, adjacent); |
| 169 | + } |
| 170 | + |
| 171 | + adjacent_map |
| 172 | + } |
| 173 | + |
| 174 | + fn position(&self, element: u8) -> Option<Point> { |
| 175 | + self.positions.get(&element).copied() |
| 176 | + } |
| 177 | + |
| 178 | + fn adjacent(&self, position: &Point) -> Vec<Point> { |
| 179 | + self.adjacent.get(position).unwrap().to_vec() |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +#[cfg(test)] |
| 184 | +mod tests { |
| 185 | + use crate::solutions::year2024::day21::Key::Activate; |
| 186 | + use crate::solutions::year2024::day21::{Day21, Pad}; |
| 187 | + use crate::solutions::Solution; |
| 188 | + |
| 189 | + const EXAMPLE: &str = r#"029A |
| 190 | +980A |
| 191 | +179A |
| 192 | +456A |
| 193 | +379A"#; |
| 194 | + |
| 195 | + #[test] |
| 196 | + #[ignore] |
| 197 | + fn part_one_example() { |
| 198 | + assert_eq!("126384", Day21.part_one(EXAMPLE)); |
| 199 | + } |
| 200 | + |
| 201 | + #[test] |
| 202 | + #[ignore] |
| 203 | + fn path_len() { |
| 204 | + let pads = vec![Pad::numeric(), Pad::arrow(), Pad::arrow()]; |
| 205 | + |
| 206 | + assert_eq!(68, Day21.path_len("029A", &pads)); |
| 207 | + assert_eq!(60, Day21.path_len("980A", &pads)); |
| 208 | + assert_eq!(68, Day21.path_len("179A", &pads)); |
| 209 | + assert_eq!(64, Day21.path_len("456A", &pads)); |
| 210 | + assert_eq!(64, Day21.path_len("379A", &pads)); |
| 211 | + } |
| 212 | + |
| 213 | + #[test] |
| 214 | + fn path_for_str() { |
| 215 | + assert_eq!( |
| 216 | + Day21.path_for_str("AA", &Pad::numeric()), |
| 217 | + vec![Activate, Activate] |
| 218 | + ); |
| 219 | + } |
| 220 | +} |
0 commit comments