projects
/
advent-of-code-17.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Tidying
[advent-of-code-17.git]
/
src
/
advent14
/
advent14.hs
diff --git
a/src/advent14/advent14.hs
b/src/advent14/advent14.hs
index 45c9dd2c45a67ea85f7714e93b2cb27899b37006..698853c7bcd2c60b0a891598e0ee9b0bbcc9f41f 100644
(file)
--- a/
src/advent14/advent14.hs
+++ b/
src/advent14/advent14.hs
@@
-2,11
+2,11
@@
import Data.List.Split (chunksOf)
import Data.Char (ord)
import Text.Printf (printf)
import Data.Bits (xor)
import Data.Char (ord)
import Text.Printf (printf)
import Data.Bits (xor)
-import qualified Data.Map.Strict as M
-import Data.Map.Strict ((!))
+import qualified Data.Set as S
import qualified Data.Graph as G
import qualified Data.Graph as G
+import Control.Parallel.Strategies (parMap, rpar)
-type Cell
Map = M.Map (Int, Int) Bool
+type Cell
Set = S.Set (Int, Int)
puzzleKey = "xlqgujun"
puzzleKey = "xlqgujun"
@@
-15,21
+15,19
@@
main = do
print $ part1 puzzleKey
print $ part2 puzzleKey
print $ part1 puzzleKey
print $ part2 puzzleKey
--- part1 :: String -> Int
--- part1 key = sum rowCounts
--- where hashes = map knotHash $ rowSpecs key
--- rowCounts = map (countSetBits . binify) hashes
part1 :: String -> Int
part1 key = sum rowCounts
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 :: String -> Int
part2 key = length $ cellEdges cells
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
binHash :: String -> String
binHash = binify . knotHash
@@
-38,20
+36,19
@@
numKey :: (Int, Int) -> Int
numKey (r, c) = 128 * r + c
numKey (r, c) = 128 * r + c
-presentCells :: [String] -> Cell
Map
-presentCells b
inHashes = M.fromList [((r, c), True) | r <- [0..127], c <- [0..127], (binHashe
s!!r)!!c == '1']
+presentCells :: [String] -> Cell
Set
+presentCells b
hs = S.fromList [(r, c) | r <- [0..127], c <- [0..127], (bh
s!!r)!!c == '1']
-adjacentCells :: Cell
Map
-> (Int, Int) -> [(Int, Int)]
-adjacentCells cells (r, c) = filter (\k ->
M
.member k cells) possibles
+adjacentCells :: Cell
Set
-> (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)]
where possibles = [(r, c - 1), (r, c + 1), (r - 1, c), (r + 1, c)]
- -- isPresent rc = length $ rc `member` cells
--- cellEdges :: CellMap -> Int
-cellEdges cells = G.stronglyConnComp [(k, numKey k, map numKey $ adjacentCells cells k) | k <-
M.key
s cells]
+cellEdges :: CellSet -> [G.SCC (Int, Int)]
+cellEdges cells = G.stronglyConnComp [(k, numKey k, map numKey $ adjacentCells cells k) | k <-
S.elem
s cells]
rowSpecs :: String -> [String]
rowSpecs :: String -> [String]
-rowSpecs key = map (((key ++ "-") ++) . show)
[0..127]
+rowSpecs key = map (((key ++ "-") ++) . show)
([0..127] :: [Integer])
countSetBits :: String -> Int
countSetBits = length . filter (== '1')
countSetBits :: String -> Int
countSetBits = length . filter (== '1')
@@
-87,8
+84,8
@@
mkHashTerms :: String -> [Int]
mkHashTerms text = take (length chunk * 64) $ cycle chunk
where chunk = map ord text ++ [17, 31, 73, 47, 23]
mkHashTerms text = take (length chunk * 64) $ cycle chunk
where chunk = map ord text ++ [17, 31, 73, 47, 23]
-hexify :: [Int] -> String
-hexify = concatMap (printf "%02x")
+
--
hexify :: [Int] -> String
+
--
hexify = concatMap (printf "%02x")
binify :: [Int] -> String
binify = concatMap (printf "%08b")
binify :: [Int] -> String
binify = concatMap (printf "%08b")