Day 14 now using a set
[advent-of-code-17.git] / src / advent14 / advent14.hs
1 import Data.List.Split (chunksOf)
2 import Data.Char (ord)
3 import Text.Printf (printf)
4 import Data.Bits (xor)
5 -- import qualified Data.Map.Strict as M
6 import qualified Data.Set as S
7 import qualified Data.Graph as G
8 import Control.Parallel.Strategies (parMap, rpar)
9
10
11 type CellSet = S.Set (Int, Int)
12
13 puzzleKey = "xlqgujun"
14
15 main :: IO ()
16 main = do
17 print $ part1 puzzleKey
18 print $ part2 puzzleKey
19
20
21 part1 :: String -> Int
22 part1 key = sum rowCounts
23 where rowCounts = parMap rpar countSetBits $ binHashes key
24
25
26 part2 :: String -> Int
27 part2 key = length $ cellEdges cells
28 where cells = presentCells $ binHashes key
29
30 binHashes :: String -> [String]
31 binHashes key = parMap rpar binHash $ rowSpecs key
32
33
34 binHash :: String -> String
35 binHash = binify . knotHash
36
37 numKey :: (Int, Int) -> Int
38 numKey (r, c) = 128 * r + c
39
40
41 presentCells :: [String] -> CellSet
42 presentCells bhs = S.fromList [(r, c) | r <- [0..127], c <- [0..127], (bhs!!r)!!c == '1']
43
44 adjacentCells :: CellSet -> (Int, Int) -> [(Int, Int)]
45 adjacentCells cells (r, c) = filter (\k -> S.member k cells) possibles
46 where possibles = [(r, c - 1), (r, c + 1), (r - 1, c), (r + 1, c)]
47
48
49 cellEdges :: CellSet -> [G.SCC (Int, Int)]
50 cellEdges cells = G.stronglyConnComp [(k, numKey k, map numKey $ adjacentCells cells k) | k <- S.elems cells]
51
52 rowSpecs :: String -> [String]
53 rowSpecs key = map (((key ++ "-") ++) . show) ([0..127] :: [Integer])
54
55 countSetBits :: String -> Int
56 countSetBits = length . filter (== '1')
57
58
59
60 knotHash :: String -> [Int]
61 knotHash input = densify tied
62 where (tied, _, _) = foldl step ([0..255], 0, 0) hashTerms
63 hashTerms = mkHashTerms input
64
65 step :: ([Int], Int, Int) -> Int -> ([Int], Int, Int)
66 step (original, start, skip) len = (replaced, start', skip + 1)
67 where replaced = tie original start len
68 start' = (start + len + skip) `mod` (length original)
69
70 tie :: [a] -> Int -> Int -> [a]
71 tie original start len = replace original replacement start
72 where replacement = reverse $ extract original start len
73
74 extract :: [a] -> Int -> Int -> [a]
75 extract items from len = take len $ drop from $ items ++ items
76
77 replace :: [a] -> [a] -> Int -> [a]
78 replace original replacement from = take (length original) (start ++ replacement ++ remainder)
79 where excess = drop (length original - from) replacement
80 stub = drop (length excess) original
81 start = take from (excess ++ stub)
82 remainder = drop (length $ start ++ replacement) original
83
84
85 mkHashTerms :: String -> [Int]
86 mkHashTerms text = take (length chunk * 64) $ cycle chunk
87 where chunk = map ord text ++ [17, 31, 73, 47, 23]
88
89 -- hexify :: [Int] -> String
90 -- hexify = concatMap (printf "%02x")
91
92 binify :: [Int] -> String
93 binify = concatMap (printf "%08b")
94
95 densify :: [Int] -> [Int]
96 densify ns = codes
97 where chunks = chunksOf 16 ns
98 compress = foldl1 xor
99 codes = map compress chunks