Skip to content

Commit

Permalink
Year 2018 Day 20
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Aug 26, 2024
1 parent ebae413 commit da67d4b
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ Performance is reasonable even on older hardware, for example a 2011 MacBook Pro
| 17 | [Reservoir Research ](https://adventofcode.com/2018/day/17) | [Source](src/year2018/day17.rs) | 145 |
| 18 | [Settlers of The North Pole](https://adventofcode.com/2018/day/18) | [Source](src/year2018/day18.rs) | 387 |
| 19 | [Go With The Flow](https://adventofcode.com/2018/day/19) | [Source](src/year2018/day19.rs) | 1 |
| 20 | [A Regular Map](https://adventofcode.com/2018/day/20) | [Source](src/year2018/day20.rs) | 41 |

## 2017

Expand Down
1 change: 1 addition & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ mod year2018 {
benchmark!(year2018, day17);
benchmark!(year2018, day18);
benchmark!(year2018, day19);
benchmark!(year2018, day20);
}

mod year2019 {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub mod year2018 {
pub mod day17;
pub mod day18;
pub mod day19;
pub mod day20;
}

/// # Rescue Santa from deep space with a solar system voyage.
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ fn year2018() -> Vec<Solution> {
solution!(year2018, day17),
solution!(year2018, day18),
solution!(year2018, day19),
solution!(year2018, day20),
]
}

Expand Down
55 changes: 55 additions & 0 deletions src/year2018/day20.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! # A Regular Map
//!
//! Simple solution taking advantage of a controversial property of the input. After taking any
//! branch it's assumed that we can return to the pre-branch position. This does *not* hold for
//! general inputs, as it's easy to construct paths which violate this constraint.
//!
//! We use a stack to save the position before a branch, pushing whenever an opening `(` is
//! encountered then popping whenever the closing `)` is found. Additionally we assume that
//! the location will never move more than 55 rooms from the starting location in order to use
//! a fixed size array to hold the minimum distance to any room.
type Input = (u32, usize);

pub fn parse(input: &str) -> Input {
explore(input)
}

pub fn part1(input: &Input) -> u32 {
input.0
}

pub fn part2(input: &Input) -> usize {
input.1
}

fn explore(input: &str) -> Input {
// Start in the center.
let mut index = 6105;
// 55 in each direction, gives a width and height of 110, for a total size of 12,100.
let mut grid = vec![u32::MAX; 12_100];
let mut stack = Vec::with_capacity(500);
let mut part_one = 0;

grid[index] = 0;

for b in input.bytes() {
let distance = grid[index];

match b {
b'(' => stack.push(index),
b'|' => index = stack[stack.len() - 1],
b')' => index = stack.pop().unwrap(),
b'N' => index -= 110,
b'S' => index += 110,
b'W' => index -= 1,
b'E' => index += 1,
_ => (),
}

grid[index] = grid[index].min(distance + 1);
part_one = part_one.max(grid[index]);
}

let part_two = grid.iter().filter(|d| (1000..u32::MAX).contains(d)).count();
(part_one, part_two)
}
1 change: 1 addition & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ mod year2018 {
mod day17_test;
mod day18_test;
mod day19_test;
mod day20_test;
}

mod year2019 {
Expand Down
15 changes: 15 additions & 0 deletions tests/year2018/day20_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use aoc::year2018::day20::*;

const EXAMPLE: &str = "^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$";

#[test]
fn part1_test() {
let input = parse(EXAMPLE);
assert_eq!(part1(&input), 31);
}

#[test]
fn part2_test() {
let input = parse(EXAMPLE);
assert_eq!(part2(&input), 0);
}

0 comments on commit da67d4b

Please sign in to comment.