Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solve Day 17 #30

Merged
merged 8 commits into from
Dec 24, 2020
Merged

Solve Day 17 #30

merged 8 commits into from
Dec 24, 2020

Conversation

manuphatak
Copy link
Owner

@manuphatak manuphatak commented Dec 24, 2020

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?

Link

https://adventofcode.com/2020/day/17

@manuphatak manuphatak self-assigned this Dec 24, 2020
@manuphatak manuphatak added the solution A solution to a problem label Dec 24, 2020
@codecov
Copy link

codecov bot commented Dec 24, 2020

Codecov Report

Merging #30 (fa20001) into main (4724ec4) will increase coverage by 0.01%.
The diff coverage is 95.55%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #30      +/-   ##
==========================================
+ Coverage   95.44%   95.45%   +0.01%     
==========================================
  Files          23       24       +1     
  Lines         329      374      +45     
  Branches       21       28       +7     
==========================================
+ Hits          314      357      +43     
  Misses          9        9              
- Partials        6        8       +2     
Impacted Files Coverage Δ
src/Day17/Solution.hs 95.55% <95.55%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4724ec4...fa20001. Read the comment docs.

Comment on lines +55 to +59
parsePocketDimension3D :: String -> Either ParseError (PocketDimension Point3D)
parsePocketDimension3D = parsePocketDimension

parsePocketDimension4D :: String -> Either ParseError (PocketDimension Point4D)
parsePocketDimension4D = parsePocketDimension
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty cool; you get totally different outcomes based on type annotations 🤯 !

Comment on lines +85 to +90
executeCycles :: forall a. (Ord a, Pocket a) => Int -> PocketDimension a -> PocketDimension a
executeCycles 0 pocketDimension = pocketDimension
executeCycles i pocketDimension = executeCycles (pred i) nextPocketDimension
where
nextPocketDimension :: PocketDimension a
nextPocketDimension =
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to do some learning about ScopedTypeVariables.

Basically a in PocketDimension a references a in executeCycles :: forall a. (Ord a, Pocket a) => Int -> PocketDimension a -> PocketDimension a.

Typing these "where" clause functions isn't necessary. However, I like them. They tell you what a function is/does, and they're useful during your development. Haskell feels like a game where you go from "type a" to "type b," and these where-clause functions are just waypoints along the way.

Comment on lines +31 to +58
class Pocket a where
from2D :: (Int, Int) -> a
offset :: a -> a -> a
neighbors :: a -> Set.Set a

newtype Point3D = Point3D (Int, Int, Int) deriving (Eq, Ord, Show)

instance Pocket Point3D where
from2D (x, y) = Point3D (x, y, 0)
Point3D (a, b, c) `offset` Point3D (x, y, z) = Point3D (a + x, b + y, c + z)
neighbors point =
Set.fromList
[ point `offset` Point3D (x, y, z)
| (x, y, z) <- offsets3D,
(x, y, z) /= (0, 0, 0)
]

newtype Point4D = Point4D (Int, Int, Int, Int) deriving (Eq, Ord, Show)

instance Pocket Point4D where
from2D (x, y) = Point4D (x, y, 0, 0)
Point4D (a, b, c, d) `offset` Point4D (x, y, z, w) = Point4D (a + x, b + y, c + z, d + w)
neighbors point =
Set.fromList
[ point `offset` Point4D (x, y, z, w)
| (x, y, z, w) <- offsets4D,
(x, y, z, w) /= (0, 0, 0, 0)
]
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is my first attempt at writing a class in Haskell. 🎉

I think it makes sense here for a couple of reasons. My first instinct is usually dependency injection -- pass in functions like neighbors as dependencies. However, I couldn't figure out how I would "type" these dependencies to work on both 3-tuples and 4-tuples, which are very different things in Haskell.

I think the classes make sense.

@manuphatak manuphatak merged commit 5384f0a into main Dec 24, 2020
@manuphatak manuphatak deleted the day_17 branch December 24, 2020 19:24
manuphatak added a commit that referenced this pull request Dec 26, 2020
* origin/main:
  Solve Day 18 (#31)
  Solve Day 17 (#30)
  Create Advent.Parser for shared parsers (#29)
  Solve Day 16 (#26)
  Add hspec-discover to PATH (#28)
manuphatak added a commit that referenced this pull request Jan 1, 2021
* origin/main:
  Solve Day 20 (#34)
  Solve Day 10 (#18)
  Harvest parseInts util (#33)
  Solve Day 18 (#31)
  Solve Day 17 (#30)
  Create Advent.Parser for shared parsers (#29)
  Solve Day 16 (#26)
  Add hspec-discover to PATH (#28)
manuphatak added a commit that referenced this pull request Jan 10, 2021
* origin/main:
  Update dependencies (#41)
  Remove hspec skip rules from hlint (#39)
  Add hlint rule (#37)
  Solve Day 21 (#35)
  Refactor: Use LANGUAGE TypeApplications (#36)
  Solve Day 12 (#21)
  Solve Day 20 (#34)
  Solve Day 10 (#18)
  Harvest parseInts util (#33)
  Solve Day 18 (#31)
  Solve Day 17 (#30)
  Create Advent.Parser for shared parsers (#29)
  Solve Day 16 (#26)
  Add hspec-discover to PATH (#28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solution A solution to a problem
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant