From 11ba4341db1d4ae5d0c0fcc966b1e03759ea36fe Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sun, 24 Dec 2017 14:34:38 +0000 Subject: [PATCH] Day 24 --- advent-of-code.cabal | 13 +- data/advent24.txt | 57 ++++++ problems/day24.html | 162 +++++++++++++++ src/advent23/advent23.hs | 2 +- src/advent24/advent24.hs | 114 +++++++++++ src/advent24/advent24.ipynb | 393 ++++++++++++++++++++++++++++++++++++ 6 files changed, 739 insertions(+), 2 deletions(-) create mode 100644 data/advent24.txt create mode 100644 problems/day24.html create mode 100644 src/advent24/advent24.hs create mode 100644 src/advent24/advent24.ipynb diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 60df43b..65cbc37 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -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 index 0000000..845654d --- /dev/null +++ b/data/advent24.txt @@ -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 index 0000000..4e5604d --- /dev/null +++ b/problems/day24.html @@ -0,0 +1,162 @@ + + + + +Day 24 - Advent of Code 2017 + + + + + + + +

Advent of Code

Neil Smith (AoC++) 48*

   0xffff&2017

+ + + +
+

--- Day 24: Electromagnetic Moat ---

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.

+

No way, of course, other than building a bridge out of the magnetic components strewn about nearby.

+

Each component has two ports, 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 pins it uses; more pins mean a stronger connection for your bridge. A 3/7 component, for example, has a type-3 port on one side, and a type-7 port on the other.

+

Your side of the pit is metallic; a perfect surface to connect a magnetic, zero-pin port. Because of this, the first port you use must be of type 0. It doesn't matter what type of port you end with; your goal is just to make the bridge as strong as possible.

+

The strength of a bridge is the sum of the port types in each component. For example, if your bridge is made of components 0/3, 3/7, and 7/4, your bridge has a strength of 0+3 + 3+7 + 7+4 = 24.

+

For example, suppose you had the following components:

+
0/2
+2/2
+2/3
+3/4
+3/5
+0/1
+10/1
+9/10
+
+

With them, you could make the following valid bridges:

+
    +
  • 0/1
  • +
  • 0/1--10/1
  • +
  • 0/1--10/1--9/10
  • +
  • 0/2
  • +
  • 0/2--2/3
  • +
  • 0/2--2/3--3/4
  • +
  • 0/2--2/3--3/5
  • +
  • 0/2--2/2
  • +
  • 0/2--2/2--2/3
  • +
  • 0/2--2/2--2/3--3/4
  • +
  • 0/2--2/2--2/3--3/5
  • +
+

(Note how, as shown by 10/1, order of ports within a component doesn't matter. However, you may only use each port on a component once.)

+

Of these bridges, the strongest one is 0/1--10/1--9/10; it has a strength of 0+1 + 1+10 + 10+9 = 31.

+

What is the strength of the strongest bridge you can make with the components you have available?

+
+

Your puzzle answer was 1940.

--- Part Two ---

The bridge you've built isn't long enough; you can't jump the rest of the way.

+

In the example above, there are two longest bridges:

+
    +
  • 0/2--2/2--2/3--3/4
  • +
  • 0/2--2/2--2/3--3/5
  • +
+

Of them, the one which uses the 3/5 component is stronger; its strength is 0+2 + 2+2 + 2+3 + 3+5 = 19.

+

What is the strength of the longest bridge you can make? If you can make multiple bridges of the longest length, pick the strongest one.

+
+

Your puzzle answer was 1928.

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 diff --git a/src/advent23/advent23.hs b/src/advent23/advent23.hs index 516819f..642343c 100644 --- a/src/advent23/advent23.hs +++ b/src/advent23/advent23.hs @@ -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 index 0000000..df3136b --- /dev/null +++ b/src/advent24/advent24.hs @@ -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 index 0000000..b53087f --- /dev/null +++ b/src/advent24/advent24.ipynb @@ -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 +} -- 2.34.1