X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=src%2Fadvent14%2Fadvent14.hs;h=698853c7bcd2c60b0a891598e0ee9b0bbcc9f41f;hb=fc23edeb0e66a61d640dfeaffd5a274a63acf8c8;hp=5ca281766e91e03b4f8d75644b8504f7bbf43ccc;hpb=8685ec38046f10f7bb38a58dcb791d35d35796e8;p=advent-of-code-17.git diff --git a/src/advent14/advent14.hs b/src/advent14/advent14.hs index 5ca2817..698853c 100644 --- a/src/advent14/advent14.hs +++ b/src/advent14/advent14.hs @@ -2,10 +2,11 @@ import Data.List.Split (chunksOf) import Data.Char (ord) import Text.Printf (printf) import Data.Bits (xor) -import qualified Data.Map.Strict as M +import qualified Data.Set as S import qualified Data.Graph as G +import Control.Parallel.Strategies (parMap, rpar) -type CellMap = M.Map (Int, Int) Bool +type CellSet = S.Set (Int, Int) puzzleKey = "xlqgujun" @@ -17,14 +18,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 @@ -33,16 +36,16 @@ numKey :: (Int, Int) -> Int 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 :: [String] -> CellSet +presentCells bhs = S.fromList [(r, c) | 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 +adjacentCells :: CellSet -> (Int, Int) -> [(Int, Int)] +adjacentCells cells (r, c) = filter (\k -> S.member k cells) possibles where possibles = [(r, c - 1), (r, c + 1), (r - 1, c), (r + 1, c)] -cellEdges :: CellMap -> [G.SCC (Int, Int)] -cellEdges cells = G.stronglyConnComp [(k, numKey k, map numKey $ adjacentCells cells k) | k <- M.keys cells] +cellEdges :: CellSet -> [G.SCC (Int, Int)] +cellEdges cells = G.stronglyConnComp [(k, numKey k, map numKey $ adjacentCells cells k) | k <- S.elems cells] rowSpecs :: String -> [String] rowSpecs key = map (((key ++ "-") ++) . show) ([0..127] :: [Integer])