From b49dd731ff412a5f60fa70f0d288fe532c3b55e6 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sat, 26 Dec 2020 11:43:37 +0000 Subject: [PATCH] Done part 2 --- advent17/package.yaml | 9 +- advent17/src/advent17.hs | 74 ++++-- advent17/src/advent17a.hs | 52 ++++ problems/day17.html | 494 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 606 insertions(+), 23 deletions(-) create mode 100644 advent17/src/advent17a.hs create mode 100644 problems/day17.html diff --git a/advent17/package.yaml b/advent17/package.yaml index 4d7792b..ec276e9 100644 --- a/advent17/package.yaml +++ b/advent17/package.yaml @@ -59,4 +59,11 @@ executables: - base >= 2 && < 6 - containers - linear - - vector + + advent17a: + main: advent17a.hs + source-dirs: src + dependencies: + - base >= 2 && < 6 + - containers + - linear diff --git a/advent17/src/advent17.hs b/advent17/src/advent17.hs index 535f971..27b10f4 100644 --- a/advent17/src/advent17.hs +++ b/advent17/src/advent17.hs @@ -1,51 +1,81 @@ -- import Debug.Trace import qualified Data.Set as S -import Linear (V3(..), V4(..), (^+^), (^-^)) -import qualified Data.Vector as V +import Linear (V3(..), V4(..), (^+^)) -type Coord = V3 Int -- x, y, z -type Grid = S.Set Coord +class (Num a, Ord a) => Coord a where + (^+^^) :: a -> a -> a + neighbourCells :: S.Set a +instance Coord (V3 Int) where + x ^+^^ y = x ^+^ y + neighbourCells = S.fromList [ V3 dx dy dz + | dx <- [-1, 0, 1] + , dy <- [-1, 0, 1] + , dz <- [-1, 0, 1] + , (dx, dy, dz) /= (0, 0, 0) + ] +instance Coord (V4 Int) where + x ^+^^ y = x ^+^ y + neighbourCells = S.fromList [ V4 dx dy dz dw + | dx <- [-1, 0, 1] + , dy <- [-1, 0, 1] + , dz <- [-1, 0, 1] + , dw <- [-1, 0, 1] + , (dx, dy, dz, dw) /= (0, 0, 0, 0) + ] + +type Grid a = S.Set a main :: IO () main = do grid0 <- readGrid "data/advent17.txt" - print grid0 - let finalGrid = head $ drop 6 $ iterate update grid0 - print $ S.size finalGrid + print $ part1 grid0 + print $ part2 grid0 + +part1 grid0 = S.size finalGrid + where finalGrid = head $ drop 6 $ iterate update grid0 -readGrid :: String -> IO Grid +part2 grid0 = S.size finalGrid + where grid4 = conv34 grid0 + finalGrid = head $ drop 6 $ iterate update grid4 + + +readGrid :: String -> IO (Grid (V3 Int)) readGrid filename = do gs <- readFile filename let grid = lines gs let isActive x y = (grid!!y)!!x == '#' let maxX = length (head grid) - 1 let maxY = length grid - 1 - return $ S.fromList [ V3 x y 0 | x <- [0..maxX], y <- [0..maxY], isActive x y] + return $ S.fromList [ V3 x y 0 + | x <- [0..maxX], y <- [0..maxY], isActive x y] -neighbourSpaces :: Coord -> Grid -neighbourSpaces here = S.map (here ^+^) nbrs - where nbrs = S.fromList [ V3 dx dy dz - | dx <- [-1, 0, 1] - , dy <- [-1, 0, 1] - , dz <- [-1, 0, 1] - , (dx, dy, dz) /= (0, 0, 0)] +conv34 :: Grid (V3 Int) -> Grid (V4 Int) +conv34 grid = S.map conv34Cell grid + +conv34Cell (V3 x y z) = V4 x y z 0 + + +neighbourSpaces :: Coord a => a -> Grid a +neighbourSpaces here = S.map (here ^+^^) neighbourCells -countOccupiedNeighbours :: Coord -> Grid -> Int -countOccupiedNeighbours cell grid = S.size $ S.intersection grid $ neighbourSpaces cell +countOccupiedNeighbours :: Coord a => a -> Grid a -> Int +countOccupiedNeighbours cell grid = + S.size $ S.intersection grid $ neighbourSpaces cell -cubeSurvives :: Grid -> Coord -> Bool +cubeSurvives :: Coord a => Grid a -> a -> Bool cubeSurvives grid cell = alive && (nNbrs == 2 || nNbrs == 3) where alive = cell `S.member` grid nNbrs = countOccupiedNeighbours cell grid -cubeBorn :: Grid -> Coord -> Bool +cubeBorn :: Coord a => Grid a -> a -> Bool cubeBorn grid cell = dead && (nNbrs == 3) where dead = cell `S.notMember` grid nNbrs = countOccupiedNeighbours cell grid -update :: Grid -> Grid -update grid = S.union (S.filter (cubeSurvives grid) grid) (S.filter (cubeBorn grid) empties) +update :: Coord a => Grid a -> Grid a +update grid = S.union (S.filter (cubeSurvives grid) grid) + (S.filter (cubeBorn grid) empties) where empties = (S.foldr mergeEmpties S.empty grid) `S.difference` grid mergeEmpties cell acc = S.union acc $ neighbourSpaces cell diff --git a/advent17/src/advent17a.hs b/advent17/src/advent17a.hs new file mode 100644 index 0000000..19c7e47 --- /dev/null +++ b/advent17/src/advent17a.hs @@ -0,0 +1,52 @@ +-- import Debug.Trace + +import qualified Data.Set as S +import Linear (V3(..), (^+^)) + +type Coord = V3 Int -- x, y, z +type Grid = S.Set Coord + +main :: IO () +main = + do grid0 <- readGrid "data/advent17.txt" + let finalGrid = head $ drop 6 $ iterate update grid0 + print $ S.size finalGrid + + +readGrid :: String -> IO Grid +readGrid filename = + do gs <- readFile filename + let grid = lines gs + let isActive x y = (grid!!y)!!x == '#' + let maxX = length (head grid) - 1 + let maxY = length grid - 1 + return $ S.fromList [ V3 x y 0 + | x <- [0..maxX], y <- [0..maxY], isActive x y] + +neighbourSpaces :: Coord -> Grid +neighbourSpaces here = S.map (here ^+^) nbrs + where nbrs = S.fromList [ V3 dx dy dz + | dx <- [-1, 0, 1] + , dy <- [-1, 0, 1] + , dz <- [-1, 0, 1] + , (dx, dy, dz) /= (0, 0, 0)] + +countOccupiedNeighbours :: Coord -> Grid -> Int +countOccupiedNeighbours cell grid = + S.size $ S.intersection grid $ neighbourSpaces cell + +cubeSurvives :: Grid -> Coord -> Bool +cubeSurvives grid cell = alive && (nNbrs == 2 || nNbrs == 3) + where alive = cell `S.member` grid + nNbrs = countOccupiedNeighbours cell grid + +cubeBorn :: Grid -> Coord -> Bool +cubeBorn grid cell = dead && (nNbrs == 3) + where dead = cell `S.notMember` grid + nNbrs = countOccupiedNeighbours cell grid + +update :: Grid -> Grid +update grid = S.union (S.filter (cubeSurvives grid) grid) + (S.filter (cubeBorn grid) empties) + where empties = (S.foldr mergeEmpties S.empty grid) `S.difference` grid + mergeEmpties cell acc = S.union acc $ neighbourSpaces cell diff --git a/problems/day17.html b/problems/day17.html new file mode 100644 index 0000000..8bf0f00 --- /dev/null +++ b/problems/day17.html @@ -0,0 +1,494 @@ + + + + +Day 17 - Advent of Code 2020 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 34*

  0.0.0.0: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?

+
+

Your puzzle answer was 388.

--- 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 2280.

Both parts of this puzzle are complete! They provide two gold stars: **

+

At this point, you should return to your Advent calendar and try another puzzle.

+

If you still want to see it, you can get your puzzle input.

+

You can also this puzzle.

+
+ + + + + + \ No newline at end of file -- 2.34.1