Commit b49dd731 authored by Neil Smith's avatar Neil Smith
Browse files

Done part 2

parent 51389d5c
......@@ -59,4 +59,11 @@ executables:
- base >= 2 && < 6
- containers
- linear
- vector
advent17a:
main: advent17a.hs
source-dirs: src
dependencies:
- base >= 2 && < 6
- containers
- linear
-- 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
-- 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
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8"/>
<title>Day 17 - Advent of Code 2020</title>
<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'/>
<link rel="stylesheet" type="text/css" href="/static/style.css?25"/>
<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
<link rel="shortcut icon" href="/favicon.png"/>
</head><!--
Oh, hello! Funny seeing you here.
I appreciate your enthusiasm, but you aren't going to find much down here.
There certainly aren't clues to any of the puzzles. The best surprises don't
even appear in the source until you unlock them for real.
Please be careful with automated requests; I'm not a massive company, and I can
only take so much traffic. Please be considerate so that everyone gets to play.
If you're curious about how Advent of Code works, it's running on some custom
Perl code. Other than a few integrations (auth, analytics, social media), I
built the whole thing myself, including the design, animations, prose, and all
of the puzzles.
The puzzles are most of the work; preparing a new calendar and a new set of
puzzles each year takes all of my free time for 4-5 months. A lot of effort
went into building this thing - I hope you're enjoying playing it as much as I
enjoyed making it for you!
If you'd like to hang out, I'm @ericwastl on Twitter.
- Eric Wastl
-->
<body>
<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2020/about">[About]</a></li><li><a href="/2020/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2020/settings">[Settings]</a></li><li><a href="/2020/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2020/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">34*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;<span class="title-event-wrap">0.0.0.0:</span><a href="/2020">2020</a><span class="title-event-wrap"></span></h1><nav><ul><li><a href="/2020">[Calendar]</a></li><li><a href="/2020/support">[AoC++]</a></li><li><a href="/2020/sponsors">[Sponsors]</a></li><li><a href="/2020/leaderboard">[Leaderboard]</a></li><li><a href="/2020/stats">[Stats]</a></li></ul></nav></div></header>
<div id="sidebar">
<div id="sponsor"><div class="quiet">Our <a href="/2020/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://careers.bolt.eu/positions?team=engineering&amp;utm_source=adventofcode&amp;utm_medium=banner&amp;utm_campaign=adventofcode_traffic" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">Bolt</a> - Fastest growing mobility startup in Europe: distributed systems, microservices, ML, complex algorithms for demand prediction, and much more!</div></div>
</div><!--/sidebar-->
<main>
<script>window.addEventListener('click', function(e,s,r){if(e.target.nodeName==='CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(e.target);s.addRange(r);}});</script>
<article class="day-desc"><h2>--- Day 17: Conway Cubes ---</h2><p>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.</p>
<p>The experimental energy source is based on cutting-edge technology: a set of <span title="Rest in peace, Conway.">Conway</span> Cubes contained in a pocket dimension! When you hear it's having problems, you can't help but agree to take a look.</p>
<p>The pocket dimension contains an infinite 3-dimensional grid. At every integer 3-dimensional coordinate (<code>x,y,z</code>), there exists a single cube which is either <em>active</em> or <em>inactive</em>.</p>
<p>In the initial state of the pocket dimension, almost all cubes start <em>inactive</em>. The only exception to this is a small flat region of cubes (your puzzle input); the cubes in this region start in the specified <em>active</em> (<code>#</code>) or <em>inactive</em> (<code>.</code>) state.</p>
<p>The energy source then proceeds to boot up by executing six <em>cycles</em>.</p>
<p>Each cube only ever considers its <em>neighbors</em>: any of the 26 other cubes where any of their coordinates differ by at most <code>1</code>. For example, given the cube at <code>x=1,y=2,z=3</code>, its neighbors include the cube at <code>x=2,y=2,z=2</code>, the cube at <code>x=0,y=2,z=3</code>, and so on.</p>
<p>During a cycle, <em>all</em> cubes <em>simultaneously</em> change their state according to the following rules:</p>
<ul>
<li>If a cube is <em>active</em> and <em>exactly <code>2</code> or <code>3</code></em> of its neighbors are also active, the cube remains <em>active</em>. Otherwise, the cube becomes <em>inactive</em>.</li>
<li>If a cube is <em>inactive</em> but <em>exactly <code>3</code></em> of its neighbors are active, the cube becomes <em>active</em>. Otherwise, the cube remains <em>inactive</em>.</li>
</ul>
<p>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.</p>
<p>For example, consider the following initial state:</p>
<pre><code>.#.
..#
###
</code></pre>
<p>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.)</p>
<p>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 <code>z</code> coordinate (and the frame of view follows the active cells in each cycle):</p>
<pre><code>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
.......
.......
..##...
..###..
.......
.......
.......
</code></pre>
<p>After the full six-cycle boot process completes, <em><code>112</code></em> cubes are left in the <em>active</em> state.</p>
<p>Starting with your given initial configuration, simulate six cycles. <em>How many cubes are left in the active state after the sixth cycle?</em></p>
</article>
<p>Your puzzle answer was <code>388</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>For some reason, your simulated results don't match what the experimental energy source engineers expected. Apparently, the pocket dimension actually has <em>four spatial dimensions</em>, not three.</p>
<p>The pocket dimension contains an infinite 4-dimensional grid. At every integer 4-dimensional coordinate (<code>x,y,z,w</code>), there exists a single cube (really, a <em>hypercube</em>) which is still either <em>active</em> or <em>inactive</em>.</p>
<p>Each cube only ever considers its <em>neighbors</em>: any of the 80 other cubes where any of their coordinates differ by at most <code>1</code>. For example, given the cube at <code>x=1,y=2,z=3,w=4</code>, its neighbors include the cube at <code>x=2,y=2,z=3,w=3</code>, the cube at <code>x=0,y=2,z=3,w=4</code>, and so on.</p>
<p>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 <em>number of active neighbors</em> of each cube.</p>
<p>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.)</p>
<p>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 <code>z</code> and <code>w</code> coordinate:</p>
<pre><code>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
###..
##.##
#...#
.#..#
.###.