Done part 2
authorNeil Smith <neil.git@njae.me.uk>
Sat, 26 Dec 2020 11:43:37 +0000 (11:43 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Sat, 26 Dec 2020 12:59:24 +0000 (12:59 +0000)
advent17/package.yaml
advent17/src/advent17.hs
advent17/src/advent17a.hs [new file with mode: 0644]
problems/day17.html [new file with mode: 0644]

index 4d7792be5692e76b1c7566fe529be35b09b63cec..ec276e98131a609ce6d1bd2b177b96fd3737971c 100644 (file)
@@ -59,4 +59,11 @@ executables:
     - base >= 2 && < 6
     - containers
     - linear
     - base >= 2 && < 6
     - containers
     - linear
-    - vector
+
+  advent17a:
+    main: advent17a.hs
+    source-dirs: src
+    dependencies:
+    - base >= 2 && < 6
+    - containers
+    - linear
index 535f971ab508216a7938512310788a53a660bc4b..27b10f4008b3e3d946d18f0f8da871a374f2ba35 100644 (file)
@@ -1,51 +1,81 @@
 -- import Debug.Trace
 
 import qualified Data.Set as S
 -- 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"
 
 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
 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
 
 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
 
 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
   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 (file)
index 0000000..19c7e47
--- /dev/null
@@ -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 (file)
index 0000000..8bf0f00
--- /dev/null
@@ -0,0 +1,494 @@
+<!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
+###..
+##.##
+#...#
+.#..#
+.###.
+
+z=1, w=2
+.....
+.....
+.....
+.....
+.....
+
+z=2, w=2
+.....
+.....
+..#..
+.....
+.....
+</code></pre>
+<p>After the full six-cycle boot process completes, <em><code>848</code></em> cubes are left in the <em>active</em> state.</p>
+<p>Starting with your given initial configuration, simulate six cycles in a 4-dimensional space. <em>How many cubes are left in the active state after the sixth cycle?</em></p>
+</article>
+<p>Your puzzle answer was <code>2280</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
+<p>At this point, you should <a href="/2020">return to your Advent calendar</a> and try another puzzle.</p>
+<p>If you still want to see it, you can <a href="17/input" target="_blank">get your puzzle input</a>.</p>
+<p>You can also <span class="share">[Share<span class="share-content">on
+  <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22Conway+Cubes%22+%2D+Day+17+%2D+Advent+of+Code+2020&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F17&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
+  <a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' && mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=I%27ve+completed+%22Conway+Cubes%22+%2D+Day+17+%2D+Advent+of+Code+2020+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2020%2Fday%2F17'}else{return false;}" target="_blank">Mastodon</a
+></span>]</span> this puzzle.</p>
+</main>
+
+<!-- ga -->
+<script>
+(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+ga('create', 'UA-69522494-1', 'auto');
+ga('set', 'anonymizeIp', true);
+ga('send', 'pageview');
+</script>
+<!-- /ga -->
+</body>
+</html>
\ No newline at end of file