-
Notifications
You must be signed in to change notification settings - Fork 0
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
Solve Day 17 #30
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
parsePocketDimension3D :: String -> Either ParseError (PocketDimension Point3D) | ||
parsePocketDimension3D = parsePocketDimension | ||
|
||
parsePocketDimension4D :: String -> Either ParseError (PocketDimension Point4D) | ||
parsePocketDimension4D = parsePocketDimension |
There was a problem hiding this comment.
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 🤯 !
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 = |
There was a problem hiding this comment.
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.
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) | ||
] |
There was a problem hiding this comment.
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.
* 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)
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 atx=1,y=2,z=3
, its neighbors include the cube atx=2,y=2,z=2
, the cube atx=0,y=2,z=3
, and so on.During a cycle, all cubes simultaneously change their state according to the following rules:
2
or3
of its neighbors are also active, the cube remains active . Otherwise, the cube becomes inactive .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):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 atx=1,y=2,z=3,w=4
, its neighbors include the cube atx=2,y=2,z=3,w=3
, the cube atx=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
andw
coordinate: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