diff --git a/README.md b/README.md index 36daa5c..aadf8b2 100644 --- a/README.md +++ b/README.md @@ -31,8 +31,8 @@ My solutions to [Advent of Code](https://adventofcode.com) puzzles. | 12 | [Garden Groups](https://adventofcode.com/2024/day/12) | [day12.rs](aoc2024/src/day12.rs) | 1.319 ms | 1.974 ms | | 13 | [Claw Contraption](https://adventofcode.com/2024/day/13) | [day13.rs](aoc2024/src/day13.rs) | 87.33 µs | 72.88 µs | | 14 | [Restroom Redoubt](https://adventofcode.com/2024/day/14) | [day14.rs](aoc2024/src/day14.rs) | 35.37 µs | 20.60 ms | +| 15 | [Warehouse Woes](https://adventofcode.com/2024/day/15) | [day15.rs](aoc2024/src/day15.rs) | 234.6 µs | 241.8 µs | - diff --git a/aoc2024/input/2024/day15.txt b/aoc2024/input/2024/day15.txt new file mode 100644 index 0000000..baec898 --- /dev/null +++ b/aoc2024/input/2024/day15.txt @@ -0,0 +1,71 @@ +################################################## +#..O...OOO..O..O..O.O.OOOO.#OO...O.O.......OO....# +#O...OO#..#.#O...OOOO...O#O..O#.O..OO...O.O......# +#..OO......OO....#O...OO...O...#O........O......O# +##..#..OOO..O.....OO....##......OO..O.....O.O..O.# +#..O#.O.O..O..O.#OOOO...OO......#OO..OO..#.OOO...# +#O.....O.OOO....O....O#.....O.#....#........O.O..# +#...O....O.OO..O#..#O.#..O...........O#.....#O..O# +#....O....OO.O.##.......#....O.O.O.O.O..#O...O...# +#O......#..OO.....O...OO.OOO.#.O#.#..OO.....#.O.O# +#O....O.#O..O..O...O#.OO..O.........OOO..O...OOO.# +#OO..O....#OOO...#.O..#.....O#O#..#.......O..OO.## +#..O.O.......#O...#.#O..O#.O.O.OO...O......O.....# +#....O...O##.....O..OO#..#O..O.....O...O.#.#O....# +#...#.OO#.O.........O.....O..O.O#O#.....OO.O....## +#..#...OO.O#OOO..O.#..........O.......O..##O...O## +#.O..O.OO#.....OO.O.O....O.#.....O#OO..OOO.##....# +#O...OO#O......O...#.O....O...O.#......OO..O.O...# +#.#..O.......O..OO....OOOO...O#...O....#.O.O.....# +#...OO.OO..OO....OO...........#.O#...O#.O.O#.OO.## +#O....O..#O.....O#.......#.O....O.....O....O.O..O# +##..O..O.O..#..O#......O......O..O#...#.O..O...#O# +#OO..OO.O..OO....OO.O#.OO.O.O#OO.#.OOOO...#.OO#..# +#.#O.O..OO.O....O...O...O.......#O..#O#.........## +#O.#O..#.O....#.#O.O.O..@..O.......O#.O...#..O...# +#O...O..#.#..O.OO....O.....O.O#....O..O.O#.OO..O.# +#....O.....O#..O....#......O.O.#..OO#OOO.O#.....O# +#O.#.O..#O#.....OO.O..O......#O...O...OO.....O...# +##O.O.O....OO.OOOOO..O..#.....O#OO.......OO....#.# +#.OO..O#.OO.......OOO#.O..O.OO.O..OO...O...O..O#.# +#O.#O..#O..O...O...O..O..O..O.....OO..O..O.....OO# +#O......O...O.OO..O.#..O..OO..OO...OOO.O.......O.# +#.OO....#....OO........O..O...OOO...O.O...O#..O..# +#..O....#..OO.O.OO..O#OO...#..O...#....O.....O..O# +#O...O#O.O..OO.#O.OOO.#.O..O#.O.......OO.......O## +#..##...O..O...#O.OOO.OO.#....O..O.O.O...##O...O.# +###......#.OO#.....O.O......O#.O...OO......O..O#.# +#.O.......O#OO#.O.O..#.....OOO....#.....OOO.OO..O# +#.O.#....O....#...O.....O....#....O............O.# +#OO.O..OO..OO...O.O.....O.##O..............O....O# +#....O..OO.O..O.......OOO..O..#..#OOO.O.O....OO..# +#O..O#..O#.#...O......O..O..O.O.#....O....OO#..OO# +#...OO........#.........O........O..O.....O......# +#O..OOO..O...O.O.OO#O#..O##..#OOO.O.OO..O..O...O.# +#.....#..#.O.....O....O#OO....O..O......#........# +#.......O.O....O.OO.#..O#....#.O..O..OO..OO#O...## +#..OO..O..O..O..#OO.OOO.....#.O.O..O.OOO..#O.....# +#O.OOO.#..OO...O......O.#.O...#...O..O......OO...# +#..OOOO.O#..........#.OOO......#....OO..OO..O.O..# +################################################## + +v<^^^<^><>^>>^vvvv>^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^^<^<^>v^<>^>><>^<^v>v^>vv>>^>><^^<>vv^vv<<^v^^^>v><><^<^<^vvvv^>>v<<^>>vv^vv>vv^<^<<<>^vv^<<>v^<^<^>>v>^>>^><><^v^^>^>>v<<>^>><><<<vv>>^vv<><^<<^v^v><<<>v>>^vv>>^><><>^^><^v<<>vv^>^v<^>^^^>><^v>v^v<>^^^^v>>><^><<>v>v>^v^>v^^>^>>><>^<<^<^v^v^>><^><<^<<<v^>>v>v^<>vv<<^vvv<^<<><>vvv<<^^v<>^<<^>>>^>>v<>>^v^v>vvv^<vv><><vv>^^^vv<>v^<><<^v<v^^^v^<<>v><>><<^>^^^v<>>><<>><<>>>>>>>v>^v^>>^>>>>^vv<v<>^<^^>>v<<^v^v<<>v^>^<^vv>v>v<<^v>v<>>^v>>v>^^^v>>^v>v>>>^<>^<>^>^v^v<><^><>vv^<>^^vvvv<<<>^<<^<^v>>><^>v^>><><<>^^^>vvvvv>v<<^vv^<<>^^<>>^>v^><vv>^><>>v<^^>^^<^>^^>^><^^>^vv>>>vv>vvv>>v><^<<<<>^>^><>^v<v>v><<^v>>^^<>>>vv>^^v^^>>>vvv<>v<<>^v><^<<^<^v diff --git a/aoc2024/src/day15.rs b/aoc2024/src/day15.rs new file mode 100644 index 0000000..62734a5 --- /dev/null +++ b/aoc2024/src/day15.rs @@ -0,0 +1,241 @@ +use aoc_runner_derive::aoc; + +#[aoc(day15, part1)] +#[must_use] +pub fn part1(input: &str) -> usize { + let mut warehouse = Warehouse::parse(input); + let steps = + Warehouse::translate_instructions(warehouse.instructions, warehouse.map.ncols() as isize); + for step in steps { + let current_position = warehouse.robot_pos; + let next_position = current_position.wrapping_add_signed(step); + match warehouse.map[next_position] { + b'.' => { + warehouse.robot_pos = next_position; + } + b'O' => { + if let Some(free_position) = + Warehouse::find_next_free_position(warehouse.map.as_ref(), next_position, step) + { + warehouse.robot_pos = next_position; + warehouse.map[next_position] = b'.'; + warehouse.map[free_position] = b'O'; + } + } + _ => {} + } + } + Warehouse::score(warehouse.map.as_ref(), warehouse.map.ncols(), b'O') +} + +#[aoc(day15, part2)] +#[must_use] +pub fn part2(input: &str) -> usize { + let warehouse = Warehouse::parse(input); + let mut map = warehouse + .map + .iter() + .flat_map(|&b| match b { + b'#' => [b'#', b'#'], + b'.' => [b'.', b'.'], + b'O' => [b'[', b']'], + _ => unreachable!(), + }) + .collect::>(); + let mut robot_pos = 2 * (warehouse.map.ncols() * (warehouse.robot_pos / warehouse.map.ncols())) + + 2 * (warehouse.robot_pos % warehouse.map.ncols()); + let steps = Warehouse::translate_instructions( + warehouse.instructions, + 2 * warehouse.map.ncols() as isize, + ); + for step in steps { + let next_position = robot_pos.wrapping_add_signed(step); + match map[next_position] { + b'.' => { + robot_pos = next_position; + } + b'[' | b']' => { + if Warehouse::check_obstacle(&map, next_position, step) { + Warehouse::move_box(&mut map, next_position, step); + robot_pos = next_position; + } + } + _ => {} + } + } + Warehouse::score(&map, 2 * warehouse.map.ncols(), b'[') +} + +struct Warehouse<'a> { + map: nalgebra::DMatrix, + instructions: &'a str, + robot_pos: usize, +} + +impl<'a> Warehouse<'a> { + fn parse(input: &'a str) -> Self { + let (input_map, instructions) = input.split_once("\n\n").unwrap(); + let input_map = input_map.as_bytes(); + let width = input_map.iter().position(|&c| c == b'\n').unwrap(); + let mut map = nalgebra::DMatrix::from_element(width, (input_map.len() - 1) / width, b'.'); + let mut robot_pos = 0; + input_map + .chunks(width + 1) + .enumerate() + .for_each(|(y, line)| { + line.iter() + .enumerate() + .take(width) + .for_each(|(x, &c)| match c { + b'@' => { + robot_pos = x + y * width; + } + _ => map[(x, y)] = c, + }); + }); + Self { + map, + instructions, + robot_pos, + } + } + + #[inline] + fn translate_instructions( + instructions: &str, + width: isize, + ) -> impl Iterator + '_ { + instructions + .bytes() + .filter(|b| !b.is_ascii_whitespace()) + .map(move |c| match c { + b'^' => -width, + b'v' => width, + b'<' => -1, + b'>' => 1, + _ => { + unreachable!() + } + }) + } + + fn find_next_free_position(map: &[u8], start: usize, step: isize) -> Option { + let mut position = start.wrapping_add_signed(step); + while map[position] == b'O' { + position = position.wrapping_add_signed(step); + } + if map[position] == b'.' { + Some(position) + } else { + None + } + } + + #[inline] + fn score(map: &[u8], cols: usize, box_byte: u8) -> usize { + map.iter() + .enumerate() + .map(|(i, &b)| { + if b == box_byte { + 100 * (i / cols) + (i % cols) + } else { + 0 + } + }) + .sum() + } + + fn check_obstacle(map: &[u8], pos: usize, step: isize) -> bool { + let (left, right) = match map[pos] { + b'[' => (pos, pos + 1), + b']' => (pos - 1, pos), + b'.' => return true, + b'#' => return false, + _ => unreachable!(), + }; + + if step.abs() == 1 { + Self::check_obstacle(map, pos.wrapping_add_signed(step), step) + } else if map[left.wrapping_add_signed(step)] == b'[' { + Self::check_obstacle(map, left.wrapping_add_signed(step), step) + } else { + Self::check_obstacle(map, left.wrapping_add_signed(step), step) + && Self::check_obstacle(map, right.wrapping_add_signed(step), step) + } + } + + fn move_box(map: &mut [u8], pos: usize, step: isize) { + let (left, right) = match map[pos] { + b'[' => (pos, pos + 1), + b']' => (pos - 1, pos), + b'.' => return, + _ => unreachable!(), + }; + + if step.abs() == 1 { + Self::move_box(map, pos.wrapping_add_signed(step * 2), step); + } else { + Self::move_box(map, left.wrapping_add_signed(step), step); + Self::move_box(map, right.wrapping_add_signed(step), step); + } + + map[left] = b'.'; + map[right] = b'.'; + map[left.wrapping_add_signed(step)] = b'['; + map[right.wrapping_add_signed(step)] = b']'; + } +} + +#[cfg(test)] +mod tests { + use super::*; + use indoc::indoc; + + const SAMPLE1: &str = indoc! {" + ########## + #..O..O.O# + #......O.# + #.OO..O.O# + #..O@..O.# + #O#..O...# + #O..O..O.# + #.OO.O.OO# + #....O...# + ########## + + ^v>^vv^v>v<>v^v<<><>>v^v^>^<<<><^ + vvv<<^>^v^^><<>>><>^<<><^vv^^<>vvv<>><^^v>^>vv<>v<<<^<^^>>>^<>vv>v^v^<>><>>>><^^>vv>v<^^^>>v^v^<^^>v^^>v^<^v>v<>>v^v^v^^<^^vv< + <>^^^^>>>v^<>vvv^>^^^vv^^>v<^^^^v<>^>vvvv><>>v^<<^^^^^ + ^><^><>>><>^^<<^^v>>><^^>v>>>^v><>^v><<<>vvvv>^<><<>^>< + ^>><>^v<><^vvv<^^<><^v<<<><<<^^<^>>^<<<^>>^v^>>^v>vv>^<<^v<>><<><<>v<^vv<<<>^^v^>^^>>><<^v>>v^v><^^>>^<>vv^ + <><^^>^^^<>^vv<<^><<><<><<<^^<<<^<<>><<><^^^>^^<>^>v<> + ^^>vv<^v^v^<>^^^>>>^^vvv^>vvv<>>>^<^>>>>>^<<^v>^vvv<>^<>< + v^^>>><<^^<>>^v^v^<<>^<^v^v><^<<<><<^vv>>v>v^<<^ + "}; + + const SAMPLE2: &str = indoc! {" + ######## + #..O.O.# + ##@.O..# + #...O..# + #.#.O..# + #...O..# + #......# + ######## + + <^^>>>vv>v<< + "}; + + #[test] + pub fn part1_example() { + assert_eq!(part1(SAMPLE1), 10092); + assert_eq!(part1(SAMPLE2), 2028); + } + + #[test] + pub fn part2_example() { + assert_eq!(part2(SAMPLE1), 9021); + } +} diff --git a/aoc2024/src/lib.rs b/aoc2024/src/lib.rs index 5ae63e2..2aeb175 100644 --- a/aoc2024/src/lib.rs +++ b/aoc2024/src/lib.rs @@ -13,7 +13,7 @@ pub mod day11; pub mod day12; pub mod day13; pub mod day14; -// pub mod day15; +pub mod day15; // pub mod day16; // pub mod day17; // pub mod day18;