Day 24
authorNeil Smith <neil.git@njae.me.uk>
Sun, 24 Dec 2017 14:34:38 +0000 (14:34 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Sun, 24 Dec 2017 14:34:38 +0000 (14:34 +0000)
advent-of-code.cabal
data/advent24.txt [new file with mode: 0644]
problems/day24.html [new file with mode: 0644]
src/advent23/advent23.hs
src/advent24/advent24.hs [new file with mode: 0644]
src/advent24/advent24.ipynb [new file with mode: 0644]

index 60df43b990a6ea77dd10ce36144bd7e8960a8d39..65cbc373e14ba617f1cddffa4003bcfd4c897e36 100644 (file)
@@ -269,4 +269,15 @@ executable advent23
                      , mtl
                      , text
                      , megaparsec
-                     , primes
\ No newline at end of file
+                     , primes
+
+executable advent24
+  hs-source-dirs:      src/advent24
+  main-is:             advent24.hs
+  default-language:    Haskell2010
+  build-depends:       base >= 4.7 && < 5
+                     , containers
+                     , mtl
+                     , text
+                     , megaparsec
+                     , multiset
diff --git a/data/advent24.txt b/data/advent24.txt
new file mode 100644 (file)
index 0000000..845654d
--- /dev/null
@@ -0,0 +1,57 @@
+42/37
+28/28
+29/25
+45/8
+35/23
+49/20
+44/4
+15/33
+14/19
+31/44
+39/14
+25/17
+34/34
+38/42
+8/42
+15/28
+0/7
+49/12
+18/36
+45/45
+28/7
+30/43
+23/41
+0/35
+18/9
+3/31
+20/31
+10/40
+0/22
+1/23
+20/47
+38/36
+15/8
+34/32
+30/30
+30/44
+19/28
+46/15
+34/50
+40/20
+27/39
+3/14
+43/45
+50/42
+1/33
+6/39
+46/44
+22/35
+15/20
+43/31
+23/23
+19/27
+47/15
+43/43
+25/36
+26/38
+1/10
\ No newline at end of file
diff --git a/problems/day24.html b/problems/day24.html
new file mode 100644 (file)
index 0000000..4e5604d
--- /dev/null
@@ -0,0 +1,162 @@
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 24 - Advent of Code 2017</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?12"/>
+<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/>
+<link rel="shortcut icon" href="/favicon.ico?2"/>
+</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 Google, 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, ads, social media),
+I built the whole thing myself, including the design, animations, prose, and
+all of the puzzles.
+
+The puzzles probably took the longest; the easiest ones took an hour or two
+each, but the harder ones took 4-5 hours, and a few even longer than that. 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="/2017/about">[About]</a></li><li><a href="/2017/support">[AoC++]</a></li><li><a href="/2017/events">[Events]</a></li><li><a href="/2017/settings">[Settings]</a></li><li><a href="/2017/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <span class="supporter">(AoC++)</span> <span class="star-count">48*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">0xffff&amp;</span><a href="/2017">2017</a><span class="title-event-wrap"></span></h1><nav><ul><li><a href="/2017">[Calendar]</a></li><li><a href="/2017/leaderboard">[Leaderboard]</a></li><li><a href="/2017/stats">[Stats]</a></li><li><a href="/2017/sponsors">[Sponsors]</a></li></ul></nav></div></header>
+
+<div id="sidebar">
+<div id="sponsor"><div class="quiet">Our <a href="/2017/sponsors">sponsors</a> help make Advent of Code possible:</div><p><a href="http://winton.com/" target="_blank" onclick="if(ga)ga('send','event','sponsor','click',this.href);" rel="noopener">Winton</a> - a data science and investment management company</p></div>
+<p class="quiet">By popular demand, there are now AoC-themed objects available (until Jan. 3rd)! Get them shipped <a href="https://teespring.com/advent-of-code" target="_blank">from the US</a> or <a href="https://teespring.com/advent-of-code-eu" target="_blank">from Europe</a>.</p>
+
+</div><!--/sidebar-->
+
+<main>
+<article class="day-desc"><h2>--- Day 24: Electromagnetic Moat ---</h2><p>The CPU itself is a large, black building surrounded by a bottomless pit. Enormous metal tubes extend outward from the side of the building at regular intervals and descend down into the void. There's no way to cross, but you need to get inside.</p>
+<p>No way, of course, other than building a <em>bridge</em> out of the magnetic components strewn about nearby.</p>
+<p>Each component has two <em>ports</em>, one on each end.  The ports come in all different types, and only matching types can be connected.  You take an inventory of the components by their port types (your puzzle input). Each port is identified by the number of <em>pins</em> it uses; more pins mean a stronger connection for your bridge. A <code>3/7</code> component, for example, has a type-<code>3</code> port on one side, and a type-<code>7</code> port on the other.</p>
+<p>Your side of the pit is metallic; a perfect surface to connect a magnetic, <em>zero-pin port</em>. Because of this, the first port you use must be of type <code>0</code>. It doesn't matter what type of port you end with; your goal is just to make the bridge as strong as possible.</p>
+<p>The <em>strength</em> of a bridge is the sum of the port types in each component. For example, if your bridge is made of components <code>0/3</code>, <code>3/7</code>, and <code>7/4</code>, your bridge has a strength of <code>0+3 + 3+7 + 7+4 = 24</code>.</p>
+<p>For example, suppose you had the following components:</p>
+<pre><code>0/2
+2/2
+2/3
+3/4
+3/5
+0/1
+10/1
+9/10
+</code></pre>
+<p>With them, you could make the following valid bridges:</p>
+<ul>
+<li><code>0/1</code></li>
+<li><code>0/1</code>--<code>10/1</code></li>
+<li><code>0/1</code>--<code>10/1</code>--<code>9/10</code></li>
+<li><code>0/2</code></li>
+<li><code>0/2</code>--<code>2/3</code></li>
+<li><code>0/2</code>--<code>2/3</code>--<code>3/4</code></li>
+<li><code>0/2</code>--<code>2/3</code>--<code>3/5</code></li>
+<li><code>0/2</code>--<code>2/2</code></li>
+<li><code>0/2</code>--<code>2/2</code>--<code>2/3</code></li>
+<li><code>0/2</code>--<code>2/2</code>--<code>2/3</code>--<code>3/4</code></li>
+<li><code>0/2</code>--<code>2/2</code>--<code>2/3</code>--<code>3/5</code></li>
+</ul>
+<p>(Note how, as shown by <code>10/1</code>, order of ports within a component doesn't matter. However, you may only use each port on a component once.)</p>
+<p>Of these bridges, the <em>strongest</em> one is <code>0/1</code>--<code>10/1</code>--<code>9/10</code>; it has a strength of <code>0+1 + 1+10 + 10+9 = <em>31</em></code>.</p>
+<p><em>What is the strength of the strongest bridge you can make</em> with the components you have available?</p>
+</article>
+<p>Your puzzle answer was <code>1940</code>.</p><article class="day-desc"><h2>--- Part Two ---</h2><p>The bridge you've built isn't long enough; you can't <span title="Who do you think you are, Mario?">jump the rest of the way</span>.</p>
+<p>In the example above, there are two longest bridges:</p>
+<ul>
+<li><code>0/2</code>--<code>2/2</code>--<code>2/3</code>--<code>3/4</code></li>
+<li><code>0/2</code>--<code>2/2</code>--<code>2/3</code>--<code>3/5</code></li>
+</ul>
+<p>Of them, the one which uses the <code>3/5</code> component is stronger; its strength is <code>0+2 + 2+2 + 2+3 + 3+5 = <em>19</em></code>.</p>
+<p><em>What is the strength of the longest bridge you can make?</em> If you can make multiple bridges of the longest length, pick the <em>strongest</em> one.</p>
+</article>
+<p>Your puzzle answer was <code>1928</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="/2017">return to your advent calendar</a> and try another puzzle.</p>
+<p>If you still want to see it, you can <a href="24/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+%22Electromagnetic+Moat%22+%2D+Day+24+%2D+Advent+of+Code+2017&amp;url=http%3A%2F%2Fadventofcode%2Ecom%2F2017%2Fday%2F24&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
+  <a href="https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2F2017%2Fday%2F24" target="_blank">Google+</a>
+  <a href="http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2F2017%2Fday%2F24&amp;title=I%27ve+completed+%22Electromagnetic+Moat%22+%2D+Day+24+%2D+Advent+of+Code+2017" target="_blank">Reddit</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('send', 'pageview');
+</script>
+<!-- /ga -->
+</body>
+</html>
\ No newline at end of file
index 516819f917464e04f99d89688faa355484ca2fa7..642343c160815b10762154e1cbc7c3c8c15c0e46 100644 (file)
@@ -60,13 +60,13 @@ part1 instructions =
              emptyMachine
 
 
-
 -- Part 2 following results of analysis by Dario Petrillo
 -- https://github.com/dp1/AoC17/blob/master/day23.5.txt
 part2 = length $ filter (not . P.isPrime) [start, start + 17 .. end]
     where start = 84 * 100 + 100000
           end = start + 17000
 
+
 executeInstructions = 
     do  instrs <- ask
         m <- get
diff --git a/src/advent24/advent24.hs b/src/advent24/advent24.hs
new file mode 100644 (file)
index 0000000..df3136b
--- /dev/null
@@ -0,0 +1,114 @@
+{-# LANGUAGE NegativeLiterals #-}
+{-# LANGUAGE FlexibleContexts #-}
+{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE TypeFamilies #-}
+
+import Data.Text (Text)
+import qualified Data.Text as T
+import qualified Data.Text.IO as TIO
+
+import Text.Megaparsec hiding (State)
+import qualified Text.Megaparsec.Lexer as L
+import Text.Megaparsec.Text (Parser)
+import qualified Control.Applicative as CA
+
+import qualified Data.MultiSet as B -- B for bag
+import qualified Data.Set as S
+import Data.Either
+
+type Part = B.MultiSet Integer
+type Parts = B.MultiSet Part
+type Candidates = S.Set Part
+data Bridge = Bridge { bridgeParts :: Parts, requiring :: Integer } deriving (Eq, Show, Ord)
+type Bridges = S.Set Bridge
+
+
+main :: IO ()
+main = do 
+        text <- TIO.readFile "data/advent24.txt"
+        let parts = successfulParse text
+        let bridges = allBridges parts
+        print $ part1 bridges
+        print $ part2 bridges
+
+
+part1 = strongestBridge
+
+part2 = bestBridge
+
+strongestBridge :: Bridges -> Integer
+strongestBridge bridges = S.findMax $ S.map bridgeStrength bridges
+
+bestBridge :: Bridges -> Integer
+bestBridge bridges = strongestBridge longBridges
+    where longest = S.findMax $ S.map bridgeLength bridges
+          longBridges = S.filter (\b -> bridgeLength b == longest) bridges
+
+
+emptyBridge :: Bridge
+emptyBridge = Bridge { bridgeParts = B.empty, requiring = 0}
+
+
+allBridges :: Parts -> Bridges
+allBridges parts = extendBridges parts (S.singleton emptyBridge) S.empty
+
+extendBridges :: Parts -> Bridges -> Bridges -> Bridges
+extendBridges parts bridges completed = 
+    if S.null bridges then completed
+                      else extendBridges parts bridges' completed'
+    where updates = map (extendOneBridge parts) $ S.toList bridges
+          newCompleted = lefts updates
+          completed' = S.union completed $ S.fromList newCompleted
+          bridges' = S.unions $ rights updates
+
+extendOneBridge :: Parts -> Bridge -> Either Bridge Bridges
+extendOneBridge parts bridge = 
+    if S.null $ candidates parts bridge
+    then Left bridge
+    else Right (S.map (grow bridge) $ candidates parts bridge)
+
+grow :: Bridge -> Part -> Bridge
+grow bridge part = bridge {bridgeParts = bp', requiring = req'}
+    where req = requiring bridge
+          req' = B.findMin $ B.delete req part
+          bp' = B.insert part $ bridgeParts bridge
+
+candidates :: Parts -> Bridge -> Candidates
+candidates parts bridge = B.toSet $ B.filter canUse parts
+    where needed = requiring bridge
+          canUse p = hasPort p needed && available parts p bridge
+
+hasPort :: Part -> Integer -> Bool
+hasPort part port = port `B.member` part
+
+available :: Parts -> Part -> Bridge -> Bool
+available parts part bridge = B.occur part parts > B.occur part (bridgeParts bridge)
+
+
+bridgeStrength :: Bridge -> Integer
+bridgeStrength bridge = B.fold (+) 0 $ B.map partStrength $ bridgeParts bridge
+    where partStrength = sum . B.elems 
+
+bridgeLength :: Bridge -> Int
+bridgeLength bridge = B.size $ bridgeParts bridge
+
+
+-- really persuade Megaparsec not to include newlines in how it consume spaces.
+onlySpace = (char ' ') <|> (char '\t')
+
+sc :: Parser ()
+sc = L.space (skipSome onlySpace) CA.empty CA.empty
+
+lexeme = L.lexeme sc
+integer = lexeme L.integer
+symbol = L.symbol sc
+slash = symbol "/"
+
+partsP = partP `sepBy` newline
+partP = B.fromList <$> integer `sepBy` slash
+
+successfulParse :: Text -> Parts
+successfulParse input = 
+        case parse partsP "input" input of
+                Left  _error -> B.empty -- TIO.putStr $ T.pack $ parseErrorPretty err
+                Right partsList -> B.fromList partsList
\ No newline at end of file
diff --git a/src/advent24/advent24.ipynb b/src/advent24/advent24.ipynb
new file mode 100644 (file)
index 0000000..b53087f
--- /dev/null
@@ -0,0 +1,393 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "{-# LANGUAGE NegativeLiterals #-}\n",
+    "{-# LANGUAGE FlexibleContexts #-}\n",
+    "{-# LANGUAGE OverloadedStrings #-}\n",
+    "{-# LANGUAGE TypeFamilies #-}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "-- import Prelude hiding ((++))\n",
+    "import Data.Text (Text)\n",
+    "import qualified Data.Text as T\n",
+    "import qualified Data.Text.IO as TIO\n",
+    "\n",
+    "import Text.Megaparsec hiding (State)\n",
+    "import qualified Text.Megaparsec.Lexer as L\n",
+    "import Text.Megaparsec.Text (Parser)\n",
+    "import qualified Control.Applicative as CA\n",
+    "\n",
+    "import qualified Data.MultiSet as B -- B for bag\n",
+    "import qualified Data.Set as S\n",
+    "\n",
+    "import Data.Either"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "type Part = B.MultiSet Integer\n",
+    "type Parts = B.MultiSet Part\n",
+    "type Candidates = S.Set Part\n",
+    "data Bridge = Bridge { bridgeParts :: Parts, requiring :: Integer } deriving (Eq, Show, Ord)\n",
+    "type Bridges = S.Set Bridge"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "-- really persuade Megaparsec not to include newlines in how it consume spaces.\n",
+    "onlySpace = (char ' ') <|> (char '\\t')\n",
+    "\n",
+    "sc :: Parser ()\n",
+    "sc = L.space (skipSome onlySpace) CA.empty CA.empty\n",
+    "\n",
+    "lexeme = L.lexeme sc\n",
+    "integer = lexeme L.integer\n",
+    "symbol = L.symbol sc\n",
+    "slash = symbol \"/\"\n",
+    "\n",
+    "partsP = partP `sepBy` newline\n",
+    "partP = B.fromList <$> integer `sepBy` slash\n",
+    "\n",
+    "successfulParse :: Text -> Parts\n",
+    "successfulParse input = \n",
+    "        case parse partsP \"input\" input of\n",
+    "                Left  _error -> B.empty -- TIO.putStr $ T.pack $ parseErrorPretty err\n",
+    "                Right partsList -> B.fromList partsList"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "sample = T.pack \"42/37\\n28/28\\n29/25\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[fromOccurList [(37,1),(42,1)],fromOccurList [(25,1),(29,1)]]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "parseTest partsP (T.pack \"42/37\\n29/25\")"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "fromOccurList [(fromOccurList [(25,1),(29,1)],1),(fromOccurList [(28,2)],1),(fromOccurList [(37,1),(42,1)],1)]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "sampleParts = successfulParse sample\n",
+    "sampleParts"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "57"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "text <- TIO.readFile \"../../data/advent24.txt\"\n",
+    "parts = successfulParse text\n",
+    "B.size parts"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "emptyBridge :: Bridge\n",
+    "emptyBridge = Bridge { bridgeParts = B.empty, requiring = 0}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "hasPort :: Part -> Integer -> Bool\n",
+    "hasPort part port = port `B.member` part"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "available :: Parts -> Part -> Bridge -> Bool\n",
+    "available parts part bridge = B.occur part parts > B.occur part (bridgeParts bridge)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 12,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "candidates :: Parts -> Bridge -> Candidates\n",
+    "candidates parts bridge = B.toSet $ B.filter canUse parts\n",
+    "    where needed = requiring bridge\n",
+    "          canUse p = hasPort p needed && available parts p bridge"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "fromList [fromOccurList [(0,1),(7,1)],fromOccurList [(0,1),(22,1)],fromOccurList [(0,1),(35,1)]]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "candidates parts emptyBridge"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 14,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "grow :: Bridge -> Part -> Bridge\n",
+    "grow bridge part = bridge {bridgeParts = bp', requiring = req'}\n",
+    "    where req = requiring bridge\n",
+    "          req' = B.findMin $ B.delete req part\n",
+    "          bp' = B.insert part $ bridgeParts bridge"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 15,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "fromList [Bridge {bridgeParts = fromOccurList [(fromOccurList [(0,1),(7,1)],1)], requiring = 7},Bridge {bridgeParts = fromOccurList [(fromOccurList [(0,1),(22,1)],1)], requiring = 22},Bridge {bridgeParts = fromOccurList [(fromOccurList [(0,1),(35,1)],1)], requiring = 35}]"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "S.map (grow emptyBridge) $ candidates parts emptyBridge"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 16,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "extendOneBridge :: Parts -> Bridge -> Either Bridge Bridges\n",
+    "extendOneBridge parts bridge = \n",
+    "    if S.null $ candidates parts bridge\n",
+    "    then Left bridge\n",
+    "    else Right (S.map (grow bridge) $ candidates parts bridge)\n",
+    "                                "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 17,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "extendBridges :: Parts -> Bridges -> Bridges -> Bridges\n",
+    "extendBridges parts bridges completed = \n",
+    "    if S.null bridges then completed\n",
+    "                      else extendBridges parts bridges' completed'\n",
+    "    where updates = map (extendOneBridge parts) $ S.toList bridges\n",
+    "          newCompleted = lefts updates\n",
+    "          completed' = S.union completed $ S.fromList newCompleted\n",
+    "          bridges' = S.unions $ rights updates\n",
+    "          "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 18,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "allBridges parts = extendBridges parts (S.singleton emptyBridge) S.empty"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 19,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "49096"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "S.size $ allBridges parts"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 22,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "bridgeStrength :: Bridge -> Integer\n",
+    "bridgeStrength bridge = B.fold (+) 0 $ B.map partStrength $ bridgeParts bridge\n",
+    "    where partStrength = sum . B.elems "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 25,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1940"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "S.findMax $ S.map bridgeStrength $ allBridges parts"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 27,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "bridgeLength :: Bridge -> Int\n",
+    "bridgeLength bridge = B.size $ bridgeParts bridge"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 28,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "bestBridge parts = S.findMax $ S.map bridgeStrength longBridges\n",
+    "    where bridges = allBridges parts\n",
+    "          longest = S.findMax $ S.map bridgeLength bridges\n",
+    "          longBridges = S.filter (\\b -> bridgeLength b == longest) bridges\n",
+    "          "
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 29,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "1928"
+      ]
+     },
+     "metadata": {},
+     "output_type": "display_data"
+    }
+   ],
+   "source": [
+    "bestBridge parts"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": null,
+   "metadata": {},
+   "outputs": [],
+   "source": []
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": "Haskell",
+   "language": "haskell",
+   "name": "haskell"
+  },
+  "language_info": {
+   "codemirror_mode": "ihaskell",
+   "file_extension": ".hs",
+   "name": "haskell",
+   "version": "8.0.2"
+  }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}