|
| 1 | +use heapless::Vec as HeaplessVec; |
| 2 | +use itertools::Itertools; |
| 3 | +use mygrid::{ |
| 4 | + direction::{Direction, LEFT, RIGHT}, |
| 5 | + grid::Grid, |
| 6 | + point::Point, |
| 7 | +}; |
| 8 | + |
| 9 | +advent_of_code::solution!(15); |
| 10 | + |
| 11 | +#[inline] |
| 12 | +fn parse_input(input: &str) -> (Grid<char>, Point, Vec<Direction>) { |
| 13 | + let (grid_str, path_str) = input.split_once("\n\n").unwrap(); |
| 14 | + let grid = Grid::new_char_grid_from_str(grid_str); |
| 15 | + let start = grid |
| 16 | + .iter_item_and_position() |
| 17 | + .find(|&(_, v)| *v == '@') |
| 18 | + .map(|(p, _)| p) |
| 19 | + .unwrap(); |
| 20 | + let path = path_str |
| 21 | + .chars() |
| 22 | + .filter(|c| !c.is_whitespace()) |
| 23 | + .map(|c| Direction::from(c)) |
| 24 | + .collect(); |
| 25 | + (grid, start, path) |
| 26 | +} |
| 27 | + |
| 28 | +pub fn part_one(input: &str) -> Option<u32> { |
| 29 | + let (mut grid, start, path) = parse_input(input); |
| 30 | + |
| 31 | + let mut robot_pos = start; |
| 32 | + for direction in path { |
| 33 | + //dbg!(&grid, &robot_pos, &direction.to_string()); |
| 34 | + // find the first hole |
| 35 | + let mut lookup_pos = robot_pos; |
| 36 | + while grid[lookup_pos] != '.' && grid[lookup_pos] != '#' { |
| 37 | + lookup_pos = lookup_pos + direction; |
| 38 | + } |
| 39 | + if grid[lookup_pos] == '.' { |
| 40 | + //dbg!("found hole", lookup_pos); |
| 41 | + // backtrack to the robot position, moving everything in the way |
| 42 | + let mut backtrack_pos = lookup_pos; |
| 43 | + while backtrack_pos != robot_pos { |
| 44 | + let next_pos = backtrack_pos - direction; |
| 45 | + grid[backtrack_pos] = grid[next_pos]; |
| 46 | + backtrack_pos = next_pos; |
| 47 | + } |
| 48 | + grid[robot_pos] = '.'; |
| 49 | + robot_pos = robot_pos + direction; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Some( |
| 54 | + grid.iter_item_and_position() |
| 55 | + .filter(|&(_, v)| *v == 'O') |
| 56 | + .map(|(p, _)| 100 * p.line as u32 + p.column as u32) |
| 57 | + .sum(), |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +pub fn part_two(input: &str) -> Option<u32> { |
| 62 | + let (base_grid, base_start, path) = parse_input(input); |
| 63 | + |
| 64 | + // transform grid for part 2 |
| 65 | + let mut start: Point = base_start; |
| 66 | + let mut grid = Grid::new(base_grid.width * 2, base_grid.height, '.'); |
| 67 | + base_grid.iter_item_and_position().for_each(|(p, v)| { |
| 68 | + let p = Point::new(p.line, p.column * 2); |
| 69 | + (grid[p], grid[p + RIGHT]) = match *v { |
| 70 | + 'O' => ('[', ']'), |
| 71 | + '@' => { |
| 72 | + start = p; |
| 73 | + ('@', '.') |
| 74 | + } |
| 75 | + _ => (*v, *v), |
| 76 | + }; |
| 77 | + }); |
| 78 | + |
| 79 | + let mut robot_pos = start; |
| 80 | + for direction in path { |
| 81 | + // fast path when the next position is a hole |
| 82 | + let next_pos = robot_pos + direction; |
| 83 | + if grid[next_pos] == '.' { |
| 84 | + grid[robot_pos] = '.'; |
| 85 | + grid[next_pos] = '@'; |
| 86 | + robot_pos = robot_pos + direction; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + // horizontal movement is just like p1 |
| 91 | + if direction == LEFT || direction == RIGHT { |
| 92 | + let mut lookup_pos = robot_pos; |
| 93 | + while grid[lookup_pos] != '.' && grid[lookup_pos] != '#' { |
| 94 | + lookup_pos = lookup_pos + direction; |
| 95 | + } |
| 96 | + if grid[lookup_pos] == '.' { |
| 97 | + let mut backtrack_pos = lookup_pos; |
| 98 | + while backtrack_pos != robot_pos { |
| 99 | + let next_pos = backtrack_pos - direction; |
| 100 | + grid[backtrack_pos] = grid[next_pos]; |
| 101 | + backtrack_pos = next_pos; |
| 102 | + } |
| 103 | + grid[robot_pos] = '.'; |
| 104 | + robot_pos = robot_pos + direction; |
| 105 | + } |
| 106 | + |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * vertical movement is backtracking but remembering positions at each steps |
| 112 | + * |
| 113 | + * Interesting cases |
| 114 | + * |
| 115 | + * Standard multi-push: |
| 116 | + * ########## ########## |
| 117 | + * ##......## ##.[][].## |
| 118 | + * ##.[][].## -> ##..[]..## |
| 119 | + * ##..[]..## ##...@..## |
| 120 | + * ##...@..## ##......## |
| 121 | + * ########## ########## |
| 122 | + * |
| 123 | + * Extraction: |
| 124 | + * ########## ########## |
| 125 | + * ##......## ##.[][].## |
| 126 | + * ##.[][].## -> ##..[]..## |
| 127 | + * ##[][][]## ##[].@[]## |
| 128 | + * ##...@..## ##......## |
| 129 | + * ########## ########## |
| 130 | + * |
| 131 | + * The snake |
| 132 | + * ########## ########## |
| 133 | + * ##......## ##..[]..## |
| 134 | + * ##..[]..## ##...[].## |
| 135 | + * ##...[].## -> ##..[]..## |
| 136 | + * ##..[]..## ##...@..## |
| 137 | + * ##...@..## ##......## |
| 138 | + * ########## ########## |
| 139 | + * |
| 140 | + * moving up from @ will hit the hole at the bottom |
| 141 | + * moving down from @ will hit the hole at the top |
| 142 | + */ |
| 143 | + type LookupSteps = HeaplessVec<LookupPos, 16>; |
| 144 | + type LookupPos = HeaplessVec<Point, 128>; |
| 145 | + let mut lookup_steps: LookupSteps = LookupSteps::new(); |
| 146 | + let mut lookup_pos: LookupPos = LookupPos::new(); |
| 147 | + lookup_pos.push(robot_pos).unwrap(); |
| 148 | + lookup_steps.push(lookup_pos.clone()).unwrap(); |
| 149 | + let mut can_move = true; |
| 150 | + |
| 151 | + while lookup_pos.len() > 0 && can_move { |
| 152 | + let mut next_lookup_pos: LookupPos = LookupPos::new(); |
| 153 | + for &p in lookup_pos.iter() { |
| 154 | + if grid[p] == '.' { |
| 155 | + continue; |
| 156 | + } |
| 157 | + if grid[p] == '#' { |
| 158 | + can_move = false; |
| 159 | + break; |
| 160 | + } |
| 161 | + let next_pos = p + direction; |
| 162 | + match grid[next_pos] { |
| 163 | + '[' => { |
| 164 | + next_lookup_pos.push(next_pos).unwrap(); |
| 165 | + next_lookup_pos.push(next_pos + RIGHT).unwrap(); |
| 166 | + } |
| 167 | + ']' => { |
| 168 | + next_lookup_pos.push(next_pos).unwrap(); |
| 169 | + next_lookup_pos.push(next_pos + LEFT).unwrap(); |
| 170 | + } |
| 171 | + _ => next_lookup_pos.push(next_pos).unwrap(), |
| 172 | + } |
| 173 | + } |
| 174 | + lookup_steps.push(next_lookup_pos.clone()).unwrap(); |
| 175 | + lookup_pos = next_lookup_pos; |
| 176 | + } |
| 177 | + |
| 178 | + if can_move { |
| 179 | + for (lookup_pos, prev_lookup_pos) in lookup_steps.iter().rev().tuple_windows() { |
| 180 | + for &p in lookup_pos.iter() { |
| 181 | + if prev_lookup_pos.contains(&(p - direction)) { |
| 182 | + grid[p] = grid[p - direction]; |
| 183 | + } else { |
| 184 | + grid[p] = '.'; |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + grid[robot_pos] = '.'; |
| 190 | + robot_pos = robot_pos + direction; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + Some( |
| 195 | + grid.iter_item_and_position() |
| 196 | + .filter(|&(_, v)| *v == '[') |
| 197 | + .map(|(p, _)| 100 * p.line as u32 + p.column as u32) |
| 198 | + .sum(), |
| 199 | + ) |
| 200 | +} |
| 201 | + |
| 202 | +#[cfg(test)] |
| 203 | +mod tests { |
| 204 | + use super::*; |
| 205 | + |
| 206 | + #[test] |
| 207 | + fn test_part_one_1() { |
| 208 | + let result = part_one(&advent_of_code::template::read_file_part( |
| 209 | + "examples", DAY, 1, |
| 210 | + )); |
| 211 | + assert_eq!(result, Some(2028)); |
| 212 | + } |
| 213 | + |
| 214 | + #[test] |
| 215 | + fn test_part_one_2() { |
| 216 | + let result = part_one(&advent_of_code::template::read_file_part( |
| 217 | + "examples", DAY, 2, |
| 218 | + )); |
| 219 | + assert_eq!(result, Some(10092)); |
| 220 | + } |
| 221 | + |
| 222 | + #[test] |
| 223 | + fn test_part_two_1() { |
| 224 | + let result = part_two(&advent_of_code::template::read_file_part( |
| 225 | + "examples", DAY, 3, |
| 226 | + )); |
| 227 | + assert_eq!(result, Some(618)); |
| 228 | + } |
| 229 | + |
| 230 | + #[test] |
| 231 | + fn test_part_two_2() { |
| 232 | + let result = part_two(&advent_of_code::template::read_file_part( |
| 233 | + "examples", DAY, 2, |
| 234 | + )); |
| 235 | + assert_eq!(result, Some(9021)); |
| 236 | + } |
| 237 | +} |
0 commit comments