Added Solution for 2020 day 17
This commit is contained in:
parent
e71b1b62fb
commit
758b6354a6
5 changed files with 609 additions and 0 deletions
8
2020/day17_conway_cubes/Cargo.toml
Normal file
8
2020/day17_conway_cubes/Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "day17_conway_cubes"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
409
2020/day17_conway_cubes/challenge.txt
Normal file
409
2020/day17_conway_cubes/challenge.txt
Normal file
|
@ -0,0 +1,409 @@
|
|||
As your flight slowly drifts through the sky, the Elves at the Mythical Information Bureau at the North Pole contact you. They'd like some help debugging a malfunctioning experimental energy source aboard one of their super-secret imaging satellites.
|
||||
|
||||
The experimental energy source is based on cutting-edge technology: a set of Conway Cubes contained in a pocket dimension! When you hear it's having problems, you can't help but agree to take a look.
|
||||
|
||||
The pocket dimension contains an infinite 3-dimensional grid. At every integer 3-dimensional coordinate (`x,y,z`), there exists a single cube which is either *active* or *inactive*.
|
||||
|
||||
In the initial state of the pocket dimension, almost all cubes start *inactive*. The only exception to this is a small flat region of cubes (your puzzle input); the cubes in this region start in the specified *active* (`#`) or *inactive* (`.`) state.
|
||||
|
||||
The energy source then proceeds to boot up by executing six *cycles*.
|
||||
|
||||
Each cube only ever considers its *neighbors*: any of the 26 other cubes where any of their coordinates differ by at most `1`. For example, given the cube at `x=1,y=2,z=3`, its neighbors include the cube at `x=2,y=2,z=2`, the cube at `x=0,y=2,z=3`, and so on.
|
||||
|
||||
During a cycle, *all* cubes *simultaneously* change their state according to the following rules:
|
||||
|
||||
* If a cube is *active* and *exactly `2` or `3`* of its neighbors are also active, the cube remains *active*. Otherwise, the cube becomes *inactive*.
|
||||
* If a cube is *inactive* but *exactly `3`* of its neighbors are active, the cube becomes *active*. Otherwise, the cube remains *inactive*.
|
||||
|
||||
The engineers responsible for this experimental energy source would like you to simulate the pocket dimension and determine what the configuration of cubes should be at the end of the six-cycle boot process.
|
||||
|
||||
For example, consider the following initial state:
|
||||
|
||||
```
|
||||
.#.
|
||||
..#
|
||||
###
|
||||
|
||||
```
|
||||
|
||||
Even though the pocket dimension is 3-dimensional, this initial state represents a small 2-dimensional slice of it. (In particular, this initial state defines a 3x3x1 region of the 3-dimensional space.)
|
||||
|
||||
Simulating a few cycles from this initial state produces the following configurations, where the result of each cycle is shown layer-by-layer at each given `z` coordinate (and the frame of view follows the active cells in each cycle):
|
||||
|
||||
```
|
||||
Before any cycles:
|
||||
|
||||
z=0
|
||||
.#.
|
||||
..#
|
||||
###
|
||||
|
||||
After 1 cycle:
|
||||
|
||||
z=-1
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
z=0
|
||||
#.#
|
||||
.##
|
||||
.#.
|
||||
|
||||
z=1
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
After 2 cycles:
|
||||
|
||||
z=-2
|
||||
.....
|
||||
.....
|
||||
..#..
|
||||
.....
|
||||
.....
|
||||
|
||||
z=-1
|
||||
..#..
|
||||
.#..#
|
||||
....#
|
||||
.#...
|
||||
.....
|
||||
|
||||
z=0
|
||||
##...
|
||||
##...
|
||||
#....
|
||||
....#
|
||||
.###.
|
||||
|
||||
z=1
|
||||
..#..
|
||||
.#..#
|
||||
....#
|
||||
.#...
|
||||
.....
|
||||
|
||||
z=2
|
||||
.....
|
||||
.....
|
||||
..#..
|
||||
.....
|
||||
.....
|
||||
|
||||
After 3 cycles:
|
||||
|
||||
z=-2
|
||||
.......
|
||||
.......
|
||||
..##...
|
||||
..###..
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
|
||||
z=-1
|
||||
..#....
|
||||
...#...
|
||||
#......
|
||||
.....##
|
||||
.#...#.
|
||||
..#.#..
|
||||
...#...
|
||||
|
||||
z=0
|
||||
...#...
|
||||
.......
|
||||
#......
|
||||
.......
|
||||
.....##
|
||||
.##.#..
|
||||
...#...
|
||||
|
||||
z=1
|
||||
..#....
|
||||
...#...
|
||||
#......
|
||||
.....##
|
||||
.#...#.
|
||||
..#.#..
|
||||
...#...
|
||||
|
||||
z=2
|
||||
.......
|
||||
.......
|
||||
..##...
|
||||
..###..
|
||||
.......
|
||||
.......
|
||||
.......
|
||||
|
||||
```
|
||||
|
||||
After the full six-cycle boot process completes, *`112`* cubes are left in the *active* state.
|
||||
|
||||
Starting with your given initial configuration, simulate six cycles. *How many cubes are left in the active state after the sixth cycle?*
|
||||
|
||||
Your puzzle answer was `304`.
|
||||
|
||||
\--- Part Two ---
|
||||
----------
|
||||
|
||||
For some reason, your simulated results don't match what the experimental energy source engineers expected. Apparently, the pocket dimension actually has *four spatial dimensions*, not three.
|
||||
|
||||
The pocket dimension contains an infinite 4-dimensional grid. At every integer 4-dimensional coordinate (`x,y,z,w`), there exists a single cube (really, a *hypercube*) which is still either *active* or *inactive*.
|
||||
|
||||
Each cube only ever considers its *neighbors*: any of the 80 other cubes where any of their coordinates differ by at most `1`. For example, given the cube at `x=1,y=2,z=3,w=4`, its neighbors include the cube at `x=2,y=2,z=3,w=3`, the cube at `x=0,y=2,z=3,w=4`, and so on.
|
||||
|
||||
The initial state of the pocket dimension still consists of a small flat region of cubes. Furthermore, the same rules for cycle updating still apply: during each cycle, consider the *number of active neighbors* of each cube.
|
||||
|
||||
For example, consider the same initial state as in the example above. Even though the pocket dimension is 4-dimensional, this initial state represents a small 2-dimensional slice of it. (In particular, this initial state defines a 3x3x1x1 region of the 4-dimensional space.)
|
||||
|
||||
Simulating a few cycles from this initial state produces the following configurations, where the result of each cycle is shown layer-by-layer at each given `z` and `w` coordinate:
|
||||
|
||||
```
|
||||
Before any cycles:
|
||||
|
||||
z=0, w=0
|
||||
.#.
|
||||
..#
|
||||
###
|
||||
|
||||
After 1 cycle:
|
||||
|
||||
z=-1, w=-1
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
z=0, w=-1
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
z=1, w=-1
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
z=-1, w=0
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
z=0, w=0
|
||||
#.#
|
||||
.##
|
||||
.#.
|
||||
|
||||
z=1, w=0
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
z=-1, w=1
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
z=0, w=1
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
z=1, w=1
|
||||
#..
|
||||
..#
|
||||
.#.
|
||||
|
||||
After 2 cycles:
|
||||
|
||||
z=-2, w=-2
|
||||
.....
|
||||
.....
|
||||
..#..
|
||||
.....
|
||||
.....
|
||||
|
||||
z=-1, w=-2
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=0, w=-2
|
||||
###..
|
||||
##.##
|
||||
#...#
|
||||
.#..#
|
||||
.###.
|
||||
|
||||
z=1, w=-2
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=2, w=-2
|
||||
.....
|
||||
.....
|
||||
..#..
|
||||
.....
|
||||
.....
|
||||
|
||||
z=-2, w=-1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=-1, w=-1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=0, w=-1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=1, w=-1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=2, w=-1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=-2, w=0
|
||||
###..
|
||||
##.##
|
||||
#...#
|
||||
.#..#
|
||||
.###.
|
||||
|
||||
z=-1, w=0
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=0, w=0
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=1, w=0
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=2, w=0
|
||||
###..
|
||||
##.##
|
||||
#...#
|
||||
.#..#
|
||||
.###.
|
||||
|
||||
z=-2, w=1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=-1, w=1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=0, w=1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=1, w=1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=2, w=1
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=-2, w=2
|
||||
.....
|
||||
.....
|
||||
..#..
|
||||
.....
|
||||
.....
|
||||
|
||||
z=-1, w=2
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=0, w=2
|
||||
###..
|
||||
##.##
|
||||
#...#
|
||||
.#..#
|
||||
.###.
|
||||
|
||||
z=1, w=2
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
.....
|
||||
|
||||
z=2, w=2
|
||||
.....
|
||||
.....
|
||||
..#..
|
||||
.....
|
||||
.....
|
||||
|
||||
```
|
||||
|
||||
After the full six-cycle boot process completes, *`848`* cubes are left in the *active* state.
|
||||
|
||||
Starting with your given initial configuration, simulate six cycles in a 4-dimensional space. *How many cubes are left in the active state after the sixth cycle?*
|
||||
|
||||
Your puzzle answer was `1868`.
|
||||
|
||||
Both parts of this puzzle are complete! They provide two gold stars: \*\*
|
||||
|
||||
At this point, you should [return to your Advent calendar](/2020) and try another puzzle.
|
||||
|
||||
If you still want to see it, you can [get your puzzle input](17/input).
|
181
2020/day17_conway_cubes/src/lib.rs
Normal file
181
2020/day17_conway_cubes/src/lib.rs
Normal file
|
@ -0,0 +1,181 @@
|
|||
use core::fmt::Display;
|
||||
use std::num::ParseIntError;
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum ParseError {
|
||||
ParseIntError(std::num::ParseIntError),
|
||||
LineMalformed(String),
|
||||
}
|
||||
|
||||
impl From<ParseIntError> for ParseError {
|
||||
fn from(value: ParseIntError) -> Self {
|
||||
Self::ParseIntError(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for ParseError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::ParseIntError(e) => write!(f, "Unable to parse into integer: {e}"),
|
||||
Self::LineMalformed(v) => write!(f, "Line is malformed: {v}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Grid3D {
|
||||
cubes_active: Vec<Vec<Vec<bool>>>,
|
||||
max: (usize, usize, usize),
|
||||
}
|
||||
|
||||
impl TryFrom<&str> for Grid3D {
|
||||
type Error = ParseError;
|
||||
fn try_from(value: &str) -> Result<Self, Self::Error> {
|
||||
let y_count = value.lines().count() + 14;
|
||||
let x_count = value.lines().next().unwrap().len() + 14;
|
||||
let z_count = 15;
|
||||
let empty_plane = vec![vec![false; x_count]; y_count];
|
||||
|
||||
let mut init_plane = vec![vec![false; x_count]; 7];
|
||||
for line in value.lines() {
|
||||
let mut row = vec![false; 7];
|
||||
for c in line.chars() {
|
||||
match c {
|
||||
'#' => row.push(true),
|
||||
'.' => row.push(false),
|
||||
_ => return Err(Self::Error::LineMalformed(line.to_string())),
|
||||
}
|
||||
}
|
||||
row.append(&mut vec![false; 7]);
|
||||
init_plane.push(row);
|
||||
}
|
||||
init_plane.append(&mut vec![vec![false; x_count]; 7]);
|
||||
let mut cubes_active = vec![empty_plane.to_vec(); 7];
|
||||
cubes_active.push(init_plane);
|
||||
cubes_active.append(&mut vec![empty_plane; 7]);
|
||||
|
||||
Ok(Self { cubes_active, max: (x_count-1, y_count-1, z_count-1) })
|
||||
}
|
||||
}
|
||||
|
||||
impl Grid3D {
|
||||
fn active_neighbours(&self, (x, y, z): (usize, usize, usize)) -> usize {
|
||||
if x == 0 || y == 0 || z == 0 || x == self.max.0 || y == self.max.1 || z == self.max.2 {
|
||||
0
|
||||
} else {
|
||||
(0..3).map(|dx| (0..3).map(|dy| (0..3).map(|dz|
|
||||
match (dx, dy, dz, self.cubes_active[z+dz-1][y+dy-1][x+dx-1]) {
|
||||
(1, 1, 1, _) => 0,
|
||||
(_, _, _, false) => 0,
|
||||
_ => 1,
|
||||
}).sum::<usize>()).sum::<usize>()).sum()
|
||||
}
|
||||
}
|
||||
|
||||
fn step(&mut self) {
|
||||
let mut next = self.cubes_active.clone();
|
||||
next.iter_mut().enumerate().for_each(|(z, plane)| {
|
||||
plane.iter_mut().enumerate().for_each(|(y, row)| {
|
||||
row.iter_mut().enumerate().for_each(|(x, cube)| {
|
||||
match (self.cubes_active[z][y][x], self.active_neighbours((x, y, z))) {
|
||||
(true, 2) | (_, 3) => *cube = true,
|
||||
_ => *cube = false,
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
std::mem::swap(&mut next, &mut self.cubes_active);
|
||||
}
|
||||
|
||||
fn active_cubes(&self) -> usize {
|
||||
self.cubes_active.iter().map(|plane| plane.iter().map(|row| row.iter().filter(|cube| **cube).count()).sum::<usize>()).sum()
|
||||
}
|
||||
}
|
||||
|
||||
struct Grid4D {
|
||||
cubes_active: Vec<Vec<Vec<Vec<bool>>>>,
|
||||
max: (usize, usize, usize, usize),
|
||||
}
|
||||
|
||||
impl From<&Grid3D> for Grid4D {
|
||||
fn from(value: &Grid3D) -> Self {
|
||||
let empty_cube = vec![vec![vec![false; value.max.0+1]; value.max.1+1]; value.max.2+1];
|
||||
let mut cubes_active = vec![empty_cube.clone(); 7];
|
||||
cubes_active.push(value.cubes_active.clone());
|
||||
cubes_active.append(&mut vec![empty_cube.clone(); 7]);
|
||||
|
||||
Self {
|
||||
cubes_active,
|
||||
max: (value.max.0, value.max.1, value.max.2, 14),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Grid4D {
|
||||
fn active_neighbours(&self, (x, y, z, w): (usize, usize, usize, usize)) -> usize {
|
||||
if x == 0 || y == 0 || z == 0 || w == 0 || x == self.max.0 || y == self.max.1 || z == self.max.2 || w == self.max.3 {
|
||||
0
|
||||
} else {
|
||||
(0..3).map(|dx| (0..3).map(|dy| (0..3).map(|dz| (0..3).map(|dw|
|
||||
match (dx, dy, dz, dw, self.cubes_active[w+dw-1][z+dz-1][y+dy-1][x+dx-1]) {
|
||||
(1, 1, 1, 1, _) => 0,
|
||||
(_, _, _, _, false) => 0,
|
||||
_ => 1,
|
||||
}).sum::<usize>()).sum::<usize>()).sum::<usize>()).sum()
|
||||
}
|
||||
}
|
||||
|
||||
fn step(&mut self) {
|
||||
let mut next = self.cubes_active.clone();
|
||||
next.iter_mut().enumerate().for_each(|(w, dim)| {
|
||||
dim.iter_mut().enumerate().for_each(|(z, plane)| {
|
||||
plane.iter_mut().enumerate().for_each(|(y, row)| {
|
||||
row.iter_mut().enumerate().for_each(|(x, cube)| {
|
||||
match (self.cubes_active[w][z][y][x], self.active_neighbours((x, y, z, w))) {
|
||||
(true, 2) | (_, 3) => *cube = true,
|
||||
_ => *cube = false,
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
std::mem::swap(&mut next, &mut self.cubes_active);
|
||||
}
|
||||
|
||||
fn active_cubes(&self) -> usize {
|
||||
self.cubes_active.iter().map(|dim| dim.iter().map(|plane| plane.iter().map(|row| row.iter().filter(|cube| **cube).count()).sum::<usize>()).sum::<usize>()).sum()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn run(input: &str) -> Result<(usize, usize), ParseError> {
|
||||
let mut grid_3d = Grid3D::try_from(input)?;
|
||||
let mut grid_4d = Grid4D::from(&grid_3d);
|
||||
for _round in 0..6 {
|
||||
grid_3d.step();
|
||||
grid_4d.step();
|
||||
}
|
||||
let first = grid_3d.active_cubes();
|
||||
let second = grid_4d.active_cubes();
|
||||
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((112, 848)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_challenge() {
|
||||
let challenge_input = read_file("tests/challenge_input");
|
||||
assert_eq!(run(&challenge_input), Ok((304, 1868)));
|
||||
}
|
||||
}
|
8
2020/day17_conway_cubes/tests/challenge_input
Normal file
8
2020/day17_conway_cubes/tests/challenge_input
Normal file
|
@ -0,0 +1,8 @@
|
|||
.###..#.
|
||||
##.##...
|
||||
....#.#.
|
||||
#..#.###
|
||||
...#...#
|
||||
##.#...#
|
||||
#..##.##
|
||||
#.......
|
3
2020/day17_conway_cubes/tests/sample_input
Normal file
3
2020/day17_conway_cubes/tests/sample_input
Normal file
|
@ -0,0 +1,3 @@
|
|||
.#.
|
||||
..#
|
||||
###
|
Loading…
Add table
Add a link
Reference in a new issue