Parallel version of day 14
[advent-of-code-17.git] / src / advent14 / advent14.hs
index 5ca281766e91e03b4f8d75644b8504f7bbf43ccc..d091ee1dde5287eb0d4b5bf822084577c5a9c764 100644 (file)
@@ -4,7 +4,7 @@ import Text.Printf (printf)
 import Data.Bits (xor)
 import qualified Data.Map.Strict as M
 import qualified Data.Graph as G
-
+import Control.Parallel.Strategies (parMap, rpar)
 type CellMap = M.Map (Int, Int) Bool
 
 puzzleKey = "xlqgujun"
@@ -17,14 +17,16 @@ main = do
 
 part1 :: String -> Int
 part1 key = sum rowCounts
-    where binHashes = map binHash $ rowSpecs key
-          rowCounts = map countSetBits binHashes
+    where rowCounts = parMap rpar countSetBits $ binHashes key
 
 
 part2 :: String -> Int
 part2 key = length $ cellEdges cells
-    where binHashes = map binHash $ rowSpecs key
-          cells = presentCells binHashes
+    where cells = presentCells $ binHashes key
+
+binHashes :: String -> [String]
+binHashes key = parMap rpar binHash $ rowSpecs key
+
 
 binHash :: String -> String
 binHash = binify . knotHash
@@ -34,7 +36,7 @@ numKey (r, c) = 128 * r + c
 
 
 presentCells :: [String] -> CellMap
-presentCells binHashes = M.fromList [((r, c), True) | r <- [0..127], c <- [0..127], (binHashes!!r)!!c == '1']
+presentCells bhs = M.fromList [((r, c), True) | r <- [0..127], c <- [0..127], (bhs!!r)!!c == '1']
 
 adjacentCells :: CellMap -> (Int, Int) -> [(Int, Int)]
 adjacentCells cells (r, c) = filter (\k -> M.member k cells) possibles