Add 2024 Day 06

This commit is contained in:
Burnus 2024-12-06 18:54:24 +01:00
parent d555db24e0
commit 81c7a42af2
5 changed files with 534 additions and 0 deletions

View file

@ -0,0 +1,13 @@
[package]
name = "day06_guard_gallivant"
version = "0.1.0"
edition = "2021"
[dependencies]
[dev-dependencies]
criterion = "0.5.1"
[[bench]]
name = "test_benchmark"
harness = false

View file

@ -0,0 +1,235 @@
The Historians use their fancy [device](4) again, this time to whisk you all away to the North Pole prototype suit manufacturing lab... in the year [1518](/2018/day/5)! It turns out that having direct access to history is very convenient for a group of historians.
You still have to be careful of time paradoxes, and so it will be important to avoid anyone from 1518 while The Historians search for the Chief. Unfortunately, a single *guard* is patrolling this part of the lab.
Maybe you can work out where the guard will go ahead of time so that The Historians can search safely?
You start by making a map (your puzzle input) of the situation. For example:
```
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...
```
The map shows the current position of the guard with `^` (to indicate the guard is currently facing *up* from the perspective of the map). Any *obstructions* - crates, desks, alchemical reactors, etc. - are shown as `#`.
Lab guards in 1518 follow a very strict patrol protocol which involves repeatedly following these steps:
* If there is something directly in front of you, turn right 90 degrees.
* Otherwise, take a step forward.
Following the above protocol, the guard moves up several times until she reaches an obstacle (in this case, a pile of failed suit prototypes):
```
....#.....
....^....#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#...
```
Because there is now an obstacle in front of the guard, she turns right before continuing straight in her new facing direction:
```
....#.....
........>#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#...
```
Reaching another obstacle (a spool of several *very* long polymers), she turns right again and continues downward:
```
....#.....
.........#
..........
..#.......
.......#..
..........
.#......v.
........#.
#.........
......#...
```
This process continues for a while, but the guard eventually leaves the mapped area (after walking past a tank of universal solvent):
```
....#.....
.........#
..........
..#.......
.......#..
..........
.#........
........#.
#.........
......#v..
```
By predicting the guard's route, you can determine which specific positions in the lab will be in the patrol path. *Including the guard's starting position*, the positions visited by the guard before leaving the area are marked with an `X`:
```
....#.....
....XXXXX#
....X...X.
..#.X...X.
..XXXXX#X.
..X.X.X.X.
.#XXXXXXX.
.XXXXXXX#.
#XXXXXXX..
......#X..
```
In this example, the guard will visit `*41*` distinct positions on your map.
Predict the path of the guard. *How many distinct positions will the guard visit before leaving the mapped area?*
Your puzzle answer was `5177`.
\--- Part Two ---
----------
While The Historians begin working around the guard's patrol route, you borrow their fancy device and step outside the lab. From the safety of a supply closet, you time travel through the last few months and [record](/2018/day/4) the nightly status of the lab's guard post on the walls of the closet.
Returning after what seems like only a few seconds to The Historians, they explain that the guard's patrol area is simply too large for them to safely search the lab without getting caught.
Fortunately, they are *pretty sure* that adding a single new obstruction *won't* cause a time paradox. They'd like to place the new obstruction in such a way that the guard will get *stuck in a loop*, making the rest of the lab safe to search.
To have the lowest chance of creating a time paradox, The Historians would like to know *all* of the possible positions for such an obstruction. The new obstruction can't be placed at the guard's starting position - the guard is there right now and would notice.
In the above example, there are only `*6*` different positions where a new obstruction would cause the guard to get stuck in a loop. The diagrams of these six situations use `O` to mark the new obstruction, `|` to show a position where the guard moves up/down, `-` to show a position where the guard moves left/right, and `+` to show a position where the guard moves both up/down and left/right.
Option one, put a printing press next to the guard's starting position:
```
....#.....
....+---+#
....|...|.
..#.|...|.
....|..#|.
....|...|.
.#.O^---+.
........#.
#.........
......#...
```
Option two, put a stack of failed suit prototypes in the bottom right quadrant of the mapped area:
```
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
......O.#.
#.........
......#...
```
Option three, put a crate of chimney-squeeze prototype fabric next to the standing desk in the bottom right quadrant:
```
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
.+----+O#.
#+----+...
......#...
```
Option four, put an alchemical retroencabulator near the bottom left corner:
```
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
..|...|.#.
#O+---+...
......#...
```
Option five, put the alchemical retroencabulator a bit to the right instead:
```
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
....|.|.#.
#..O+-+...
......#...
```
Option six, put a tank of sovereign glue right next to the tank of universal solvent:
```
....#.....
....+---+#
....|...|.
..#.|...|.
..+-+-+#|.
..|.|.|.|.
.#+-^-+-+.
.+----++#.
#+----++..
......#O..
```
It doesn't really matter what you choose to use as an obstacle so long as you and The Historians can put it into position without the guard noticing. The important thing is having enough options that you can find one that minimizes time paradoxes, and in this example, there are `*6*` different positions you could choose.
You need to get the guard stuck in a loop by adding a single new obstruction. *How many different positions could you choose for this obstruction?*
Your puzzle answer was `1686`.
Both parts of this puzzle are complete! They provide two gold stars: \*\*
At this point, you should [return to your Advent calendar](/2024) and try another puzzle.
If you still want to see it, you can [get your puzzle input](6/input).

View file

@ -0,0 +1,146 @@
use core::fmt::Display;
use std::collections::HashSet;
#[derive(Debug, PartialEq, Eq)]
pub enum ParseError {
EmptyMap,
InvalidChar(char),
NoGuard,
NonRectangular,
}
impl Display for ParseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::EmptyMap => write!(f, "Input can't be empty"),
Self::InvalidChar(e) => write!(f, "Unable to parse {e} into a map item. Vaid items are '.', '#', and '^'."),
Self::NoGuard => write!(f, "No guard found. Expected exactly one '^'."),
Self::NonRectangular => write!(f, "All input lines must be of equal length"),
}
}
}
type Coordinates = (isize, isize);
#[derive(Clone)]
struct Map {
obstacles: HashSet<Coordinates>,
width: isize,
height: isize,
guard_position: Coordinates,
guard_facing: Coordinates,
}
impl TryFrom<&str> for Map {
type Error = ParseError;
fn try_from(value: &str) -> Result<Self, Self::Error> {
let mut obstacles = HashSet::new();
let mut guard_position = None;
let mut guard_facing = None;
let height = value.lines().count();
if height == 0 {
return Err(Self::Error::EmptyMap);
}
let width = value.lines().next().unwrap().len();
for (y, line) in value.lines().enumerate() {
for (x, c) in line.chars().enumerate() {
if x > width {
return Err(Self::Error::NonRectangular);
}
match c {
'.' => (),
'#' => _ = obstacles.insert((x as isize, y as isize)),
'^' => {
guard_position = Some((x as isize, y as isize));
guard_facing = Some((0, -1));
},
e => return Err(Self::Error::InvalidChar(e)),
}
}
}
if guard_position.is_none() {
return Err(Self::Error::NoGuard);
}
Ok(Self {
obstacles,
height: height as isize,
width: width as isize,
guard_position: guard_position.unwrap(),
guard_facing: guard_facing.unwrap(),
})
}
}
impl Map {
/// Return the next `facing` by turning right. Panics if called with an invalid facing.
fn turn_right(facing: Coordinates) -> Coordinates {
match facing {
(0, -1) => (1, 0),
(1, 0) => (0, 1),
(0, 1) => (-1, 0),
(-1, 0) => (0, -1),
_ => unreachable!(),
}
}
fn guard_way(&self) ->Option<HashSet<Coordinates>> {
let mut curr = self.guard_position;
let mut facing = self.guard_facing;
let mut route = HashSet::from([(curr, facing)]);
loop {
let next = (curr.0+facing.0, curr.1+facing.1);
if next.0 < 0 || next.1 < 0 || next.0 == self.width || next.1 == self.height {
break;
}
let right = Self::turn_right(facing);
if self.obstacles.contains(&next) {
facing = right;
} else {
curr = next;
}
if route.contains(&(curr, facing)) {
return None;
}
route.insert((curr, facing));
}
let route_positions = route.iter().map(|(position, _facing)| *position).collect();
Some(route_positions)
}
}
pub fn run(input: &str) -> Result<(usize, usize), ParseError> {
let map = Map::try_from(input)?;
let guard_way = map.guard_way().unwrap();
let first = guard_way.len();
let second = guard_way.iter().map(|pos| {
let mut new_map = map.clone();
new_map.obstacles.insert(*pos);
new_map
}).filter(|map| map.guard_way().is_none())
.count();
Ok((first, second))
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::read_to_string;
fn read_file(name: &str) -> String {
read_to_string(name).expect(&format!("Unable to read file: {name}")[..]).trim().to_string()
}
#[test]
fn test_sample() {
let sample_input = read_file("tests/sample_input");
assert_eq!(run(&sample_input), Ok((41, 6)));
}
#[test]
fn test_challenge() {
let challenge_input = read_file("tests/challenge_input");
assert_eq!(run(&challenge_input), Ok((5177, 1686)));
}
}

View file

@ -0,0 +1,130 @@
....#....#.............#...........#...........#................................#..................#..............................
#........................................................................................#.........................#..............
.........................#.......................................#.....#..................#.....................#...##............
..............#.........................................#...#..................................#.......#.....#....................
..#.........#.......................#...........................................#................................#................
.............#.......................#.........#..#...#....#...........#..#.......#..#......................#........#.......#....
................#.................#........#.........#..#........#..............................#.........................#.......
..............................................................#......#....#.....#.................................................
#...........#....#.............#.................#.................#..............................................#...............
#....#...........................................................................#............................................#...
....#.#...#................................#......#........................#....................................................#.
......................##.......#....#.............................................#......#......#.......#.........................
.......#...#....................................................................#.................................................
.....................................................#............#..............#...........#................#.......#...#.......
...........................#............................................................................................#......#..
..................#.........................................#.#............................#.........#.........#..................
................#..........#...........................#...................................#............................#.........
..................##.........................................................#...............................................#....
........#.##.................................#..................#......#............#................#...#.....#.............#....
#....................................#...................................#............................#......................#....
.....................................##....................................#..#.........#............#......#..............#......
..........#...........................................#...#..........#.............#......................#...........#...........
...#................#...#...............................................................#.........................................
..........................#...#.............................................#......##...........##....................#...........
#..........#..#......................................................................................................#............
................#.....#............#...#...............................#.#...#....#.#.....##.....................................#
...............................................................................................................#.....#.....#.....#
.......................................#.........#.................................#....................#.#.................#.....
...........................................................................................................#.........#....#.......
..........#...........#..........#....................................#.........#...#.....#....#........#...#................#...#
..................#......#............................##...............#......................#..........#........................
.............#...#...........#..........................#.......#...........#.............#................#......................
............................#..........................................................##...................#...........#....#..##
.....#...........................#.........#..#.............................................#..................................#.#
.........................................#..................................#..........#..........................#...............
.........................................................#...........##......#.....##...#..........#.....#................#.......
.....#..................................................................................................#......#.....#.........#..
.................#..............................#......................####.......#..#...#........#..........................#...#
......................#.................#..............##........................................#....#...........................
.#.........#........................................................................................#.................#...........
#.....................#............................#.........................#...........................#.........#..............
#....#....................#..........................................................................#..........#.......#......#..
....................................#..........................#.#....................#.........#...............#.................
..#.............................................................................................#.................................
.................................................#...................................................#............................
...................................................#..........................#.................#.................................
..........#....#...................#..........#.................................................................#................#
..#..................................................................#..............#..#......#.........#..................#......
...........#...................#.......................#......#......................#............................#...............
........#.........................................................................................................................
......................#..................................................................#.................#.......#..............
.........#...#..........#........................#.......#.........................#.....#..........#.......#.....................
.....#..........................................................................^.....................................#...........
......................#..........#................#...#....#......#..................................#.......#................##..
...............#...............................................#.......................#........................#.....#...........
.....#..................#.................#..............................................................#......#..............#..
....................#..........#..................................................................................................
......#.............................................................................#.#..........#...#...#...#........#...........
...#..............#..............................................#................................#.#................#............
..................#...............................................................................................................
..............#................................#...................................................................#..............
...#....##.#..........#...#...........#..#..............................................#..................#....###...............
........#....#.#......#.....................................................................................................##....
...........................#........................................................................................#.............
...#..............#..............#................................#...........................................#...................
.....#........#......................................................................................#...#........................
...................................................#.....#...............#........................................................
.......................#.....#....................#............#.................#.........................#......................
#..............#..........................#.............................................................#.........................
#....#.....#....................................................#...........#..........................#.................#........
#............................#...............................................................................##..................#
..........................................................#............#...#............................................#..#......
..................#...............#....................................................#.#......................#....#............
.................#.......#.....................................##...................##.........#..................................
.............#..............#.............#.................................#....#.........................#..............#.......
........#.........................................#....#..........................................................................
...#...............................................................#.....................#..#..........................#..........
..............................................#........#.#.........#.......#......................................................
........#...............................................................................................#.........................
......................................#...........................................................................................
................#....................#........................#..............................#.#..................................
......................................#...............#......................................#.............#....#.................
..#.....................#................#................................#....#............#.#..............#....................
.............#....................#...................#.............#........................................................#....
......................................#.#.................................................#.....................#..#..............
..#............##..................................................#..............#.............................................#.
...................................#.......#............#...#...........................................#.......................#.
........#..#.............................................................#........................................#...............
...........#...##..........................##..#......#............#.................#...#..#...#.......#......................#..
.#.....................#......................#..........................#...............#...#.........................#..........
............................................#.........#.....#................#..............................#.......#...#....#....
...................................#.................................................................#.....#..............#.......
......#..............................#........#................................#....................#.............#..........#....
......#.......................#.....................#....................................##....#.........#.#......................
..#...#..#..........................................#..........................#...#.#.#.....#....................................
..............#.......#.........#.#...........................#................#...............................#...............#..
.#.............#......#..##....#.....#.............................................................................#..............
.............#......................................................................##.................#..........................
................#...........................................#..........#..........................................................
..............................................#.............#...#....................................................##......##.#.
............#.....#..................................................#............................................................
.............................#..#..................#....#.........#...#........#............#....#.#......................#.......
...........#...........................#.#...........................#................................#........................###
.........#.....................#..#..#.......................................#....#....#.......................#.................#
.............#..............#........................................#.......#....#........#...................#...........#......
.#..........#..........#....................#.......#....#............................................................#...........
..#...................#...........................................#.......................................#.......................
.........#...........#.................#..................#..............#.....#.......##........#........#.#.....................
..#...........#..............................................#.....#..........................................#...................
.................................................#.............................#..........#.........#.............................
..#...#.#........#.#................................................................................................#.............
.......###.........#..............................................................#...#...................#........#..............
.................................................#.................#..#................#...........................#..............
..........#.#...........................................................................#..............#..........................
.......#...............................#....................#.................................#.....#.......................#..#..
...............#.....................##..........................#.##....#.....................#............#.........#.......#...
#.............#..........................................................#..................................................#.....
...................................................................................................#........#.....................
..............................#..........................#............................#...........................................
...#............................................#.................................................................................
....#........#..##.....................................................#.#.........#.....#.............##.......#.............#...
..........#....#...............#....#..#.......#.........#.......#..................................................#.............
..............................#.#.........................................................#.....................#.............#...
#..........................................#..............#.........#...........#......#............#.#...........................
.............#.................................................................#........#.......#......#..#.......................
......#..#.....................#.............#.....#.........................#.....................##......#......................
...................#..............................................................................#......#........................
...........#.#..#...................#....#................#.....#.....#...#....................#...............................#..
............#...........................##..........................##......#...................#........................#....#...
.....#..#.#.....##..........#.................................................................#..............#..............#.....

View file

@ -0,0 +1,10 @@
....#.....
.........#
..........
..#.......
.......#..
..........
.#..^.....
........#.
#.........
......#...