Conway Cubes
2020-12-17
Day 17: Conway Cubes
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?
--- 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?
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?
--- 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?
Input:
.#.####.
.#...##.
..###.##
#..#.#.#
#..#....
#.####..
##.##..#
#.#.#..#
.#...##.
..###.##
#..#.#.#
#..#....
#.####..
##.##..#
#.#.#..#
use crate::common::AdventOfCodeDay;
use std::collections::HashMap;
pub struct Day17 {
width: i32,
height: i32,
input: Vec<Vec<bool>>,
}
impl Day17 {
pub fn new() -> Self {
let input_bytes = include_bytes!("../res/17_input.txt");
let input_str = String::from_utf8_lossy(input_bytes);
let lines = input_str.lines().map(String::from).collect::<Vec<String>>();
Self {
width: lines[0].len() as i32,
height: lines.len() as i32,
input: lines.iter().map(|l| l.chars().map(|c| c=='#').collect()).collect(),
}
}
}
trait DimensionalCoord {
fn zero() -> Self;
fn componentwise_min(&self, other: Self) -> Self;
fn componentwise_max(&self, other: Self) -> Self;
fn inc_all(&mut self, delta: i32);
fn iter_neighbours<F>(&self, fun: F) where F: FnMut(Self), Self: Sized;
fn iter_all_coords_inclusive<F>(min: Self, max: Self, fun: F) where F: FnMut(Self), Self: Sized;
}
#[derive(PartialEq, Eq, std::hash::Hash, Clone, Copy)]
struct Coord3D {
x: i32,
y: i32,
z: i32,
}
impl DimensionalCoord for Coord3D {
fn zero() -> Self {
Self {
x: 0,
y: 0,
z: 0,
}
}
fn componentwise_min(&self, other: Self) -> Self {
Self {
x: if self.x < other.x { self.x } else { other.x },
y: if self.y < other.y { self.y } else { other.y },
z: if self.z < other.z { self.z } else { other.z },
}
}
fn componentwise_max(&self, other: Self) -> Self {
Self {
x: if self.x > other.x { self.x } else { other.x },
y: if self.y > other.y { self.y } else { other.y },
z: if self.z > other.z { self.z } else { other.z },
}
}
fn inc_all(&mut self, delta: i32) {
self.x += delta;
self.y += delta;
self.z += delta;
}
fn iter_neighbours<F>(&self, mut fun: F) where F: FnMut(Self) {
for dx in -1..=1 {
for dy in -1..=1 {
for dz in -1..=1 {
if dx==0 && dy==0 && dz==0 { continue; }
fun(Coord3D::new(self.x+dx, self.y+dy, self.z+dz));
}
}
}
}
fn iter_all_coords_inclusive<F>(min: Self, max: Self, mut fun: F) where F: FnMut(Self) {
for x in min.x..=max.x {
for y in min.y..=max.y {
for z in min.z..=max.z {
fun(Coord3D::new(x, y, z));
}
}
}
}
}
impl Coord3D {
fn new(x:i32, y:i32, z:i32) -> Self {
Self {
x,
y,
z,
}
}
}
#[derive(PartialEq, Eq, std::hash::Hash, Clone, Copy)]
struct Coord4D {
x: i32,
y: i32,
z: i32,
w: i32,
}
impl DimensionalCoord for Coord4D {
fn zero() -> Self {
Self {
x: 0,
y: 0,
z: 0,
w: 0,
}
}
fn componentwise_min(&self, other: Self) -> Self {
Self {
x: if self.x < other.x { self.x } else { other.x },
y: if self.y < other.y { self.y } else { other.y },
z: if self.z < other.z { self.z } else { other.z },
w: if self.w < other.w { self.w } else { other.w },
}
}
fn componentwise_max(&self, other: Self) -> Self {
Self {
x: if self.x > other.x { self.x } else { other.x },
y: if self.y > other.y { self.y } else { other.y },
z: if self.z > other.z { self.z } else { other.z },
w: if self.w > other.w { self.w } else { other.w },
}
}
fn inc_all(&mut self, delta: i32) {
self.x += delta;
self.y += delta;
self.z += delta;
self.w += delta;
}
fn iter_neighbours<F>(&self, mut fun: F) where F: FnMut(Self) {
for dx in -1..=1 {
for dy in -1..=1 {
for dz in -1..=1 {
for dw in -1..=1 {
if dx==0 && dy==0 && dz==0 && dw==0 { continue; }
fun(Coord4D::new(self.x+dx, self.y+dy, self.z+dz, self.w+dw));
}
}
}
}
}
fn iter_all_coords_inclusive<F>(min: Self, max: Self, mut fun: F) where F: FnMut(Self) {
for x in min.x..=max.x {
for y in min.y..=max.y {
for z in min.z..=max.z {
for w in min.w..=max.w {
fun(Coord4D::new(x, y, z, w));
}
}
}
}
}
}
impl Coord4D {
fn new(x:i32, y:i32, z:i32, w:i32) -> Self {
Self {
x,
y,
z,
w,
}
}
}
struct PocketUniverse<TCoord: DimensionalCoord + Eq + std::hash::Hash + Clone + Copy> {
state: HashMap<TCoord, (bool, bool)>
}
impl<TCoord: DimensionalCoord + Eq + std::hash::Hash + Clone + Copy> PocketUniverse<TCoord> {
pub fn new() -> Self {
Self {
state: HashMap::with_capacity(8192),
}
}
fn step(&mut self) {
let mut min_coord = TCoord::zero();
let mut max_coord = TCoord::zero();
for (c, (v_old, v_curr)) in self.state.iter_mut() {
*v_old = *v_curr;
min_coord = min_coord.componentwise_min(*c);
max_coord = max_coord.componentwise_max(*c);
}
min_coord.inc_all(-1);
max_coord.inc_all(1);
TCoord::iter_all_coords_inclusive(min_coord.clone(), max_coord.clone(), |c| self.step_single(c));
}
fn step_single(&mut self, c: TCoord) {
let nbc = self.get_neighbours_old(c);
if self.get_old(c) {
if nbc == 2 || nbc == 3 {
// stay active
} else {
self.set_new(c, false);
}
} else {
if nbc == 3 {
self.set_new(c, true);
} else {
// remain inactive
}
}
}
fn get_old(&self, c: TCoord) -> bool {
if let Some((v_old, _)) = self.state.get(&c) {
return *v_old;
}
return false;
}
fn get_neighbours_old(&self, c: TCoord) -> i32 {
let mut count = 0;
c.iter_neighbours(|c| {
if self.get_old(c) {
count+=1;
}
});
return count;
}
fn set_new(&mut self, c: TCoord, v: bool) {
if let Some((_, v_new)) = self.state.get_mut(&c) {
*v_new = v;
} else {
self.state.insert(c, (false, v));
}
}
fn count_active_new(&self) -> i32 {
return self.state.iter().filter(|(_,v)| v.1).count() as i32;
}
}
impl AdventOfCodeDay for Day17 {
fn task_1(&self) -> String {
let mut pock_uni = PocketUniverse::<Coord3D>::new();
for x in 0..self.width {
for y in 0..self.height {
pock_uni.state.insert(Coord3D::new(x,y,0), (false, self.input[y as usize][x as usize]));
}
}
verboseln!("After 0 cycles:");
verboseln!("{}", pock_uni.count_active_new());
verboseln!();
for i in 0..6 {
pock_uni.step();
verboseln!("After {} cycles:", i+1);
verboseln!("{}", pock_uni.count_active_new());
verboseln!();
}
return pock_uni.count_active_new().to_string();
}
fn task_2(&self) -> String {
let mut pock_uni = PocketUniverse::<Coord4D>::new();
for x in 0..self.width {
for y in 0..self.height {
pock_uni.state.insert(Coord4D::new(x,y,0,0), (false, self.input[y as usize][x as usize]));
}
}
for _ in 0..6 { pock_uni.step(); }
return pock_uni.count_active_new().to_string();
}
}
Result Part 1: 276
Result Part 2: 2136