summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-12-24 10:44:40 +0100
committertomsmeding <tom.smeding@gmail.com>2017-12-24 10:44:40 +0100
commite5e38b48f9bade9223a894fc212e26dbdee49783 (patch)
tree4aa5fa2cb362583afa50f0286f2d4ca533944f0f
parent695ebdc358096146b7d2594aa252c02027c9dc3f (diff)
Day 24
-rw-r--r--2017/24.hs34
-rw-r--r--2017/24.in57
2 files changed, 91 insertions, 0 deletions
diff --git a/2017/24.hs b/2017/24.hs
new file mode 100644
index 0000000..fe71f27
--- /dev/null
+++ b/2017/24.hs
@@ -0,0 +1,34 @@
+import Control.Monad
+import Data.List
+import qualified Data.Map.Strict as Map
+
+
+type Inventory = Map.Map Int [Int]
+
+findbridges :: Int -> Inventory -> [[(Int, Int)]]
+findbridges start inven = case Map.lookup start inven of
+ Nothing -> [[]]
+ Just nexts ->
+ concatMap (\n -> map ((start, n) :) $ findbridges n (delpair start n $ delpair n start inven)) $
+ nub nexts
+ where
+ delpair :: Int -> Int -> Inventory -> Inventory
+ delpair a b = Map.update (\l -> let l' = l \\ [b] in if null l' then Nothing else Just l') a .
+ Map.update (\l -> let l' = l \\ [a] in if null l' then Nothing else Just l') b
+
+strength :: [(Int, Int)] -> Int
+strength = sum . map (uncurry (+))
+
+parse :: String -> (Int, Int)
+parse str = let Just idx = findIndex (== '/') str in (read $ take idx str, read $ drop (succ idx) str)
+
+main :: IO ()
+main = do
+ input <- liftM (map parse . lines) (readFile "24.in")
+ let mp = foldl (\m (a,b) -> Map.insertWith (++) b [a] $ Map.insertWith (++) a [b] m) Map.empty input
+ br = findbridges 0 mp
+
+ print $ maximum (map strength br)
+
+ let maxlen = maximum (map length br)
+ print $ maximum $ map strength $ filter ((== maxlen) . length) br
diff --git a/2017/24.in b/2017/24.in
new file mode 100644
index 0000000..1fbfe25
--- /dev/null
+++ b/2017/24.in
@@ -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