Tidied imports and type annotations
[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.Graph as G
7
8 type CellMap = M.Map (Int, Int) Bool
9
10 puzzleKey = "xlqgujun"
11
12 main :: IO ()
13 main = do
14 print $ part1 puzzleKey
15 print $ part2 puzzleKey
16
17
18 part1 :: String -> Int
19 part1 key = sum rowCounts
20 where binHashes = map binHash $ rowSpecs key
21 rowCounts = map countSetBits binHashes
22
23
24 part2 :: String -> Int
25 part2 key = length $ cellEdges cells
26 where binHashes = map binHash $ rowSpecs key
27 cells = presentCells binHashes
28
29 binHash :: String -> String
30 binHash = binify . knotHash
31
32 numKey :: (Int, Int) -> Int
33 numKey (r, c) = 128 * r + c
34
35
36 presentCells :: [String] -> CellMap
37 presentCells binHashes = M.fromList [((r, c), True) | r <- [0..127], c <- [0..127], (binHashes!!r)!!c == '1']
38
39 adjacentCells :: CellMap -> (Int, Int) -> [(Int, Int)]
40 adjacentCells cells (r, c) = filter (\k -> M.member k cells) possibles
41 where possibles = [(r, c - 1), (r, c + 1), (r - 1, c), (r + 1, c)]
42
43
44 cellEdges :: CellMap -> [G.SCC (Int, Int)]
45 cellEdges cells = G.stronglyConnComp [(k, numKey k, map numKey $ adjacentCells cells k) | k <- M.keys cells]
46
47 rowSpecs :: String -> [String]
48 rowSpecs key = map (((key ++ "-") ++) . show) ([0..127] :: [Integer])
49
50 countSetBits :: String -> Int
51 countSetBits = length . filter (== '1')
52
53
54
55 knotHash :: String -> [Int]
56 knotHash input = densify tied
57 where (tied, _, _) = foldl step ([0..255], 0, 0) hashTerms
58 hashTerms = mkHashTerms input
59
60 step :: ([Int], Int, Int) -> Int -> ([Int], Int, Int)
61 step (original, start, skip) len = (replaced, start', skip + 1)
62 where replaced = tie original start len
63 start' = (start + len + skip) `mod` (length original)
64
65 tie :: [a] -> Int -> Int -> [a]
66 tie original start len = replace original replacement start
67 where replacement = reverse $ extract original start len
68
69 extract :: [a] -> Int -> Int -> [a]
70 extract items from len = take len $ drop from $ items ++ items
71
72 replace :: [a] -> [a] -> Int -> [a]
73 replace original replacement from = take (length original) (start ++ replacement ++ remainder)
74 where excess = drop (length original - from) replacement
75 stub = drop (length excess) original
76 start = take from (excess ++ stub)
77 remainder = drop (length $ start ++ replacement) original
78
79
80 mkHashTerms :: String -> [Int]
81 mkHashTerms text = take (length chunk * 64) $ cycle chunk
82 where chunk = map ord text ++ [17, 31, 73, 47, 23]
83
84 -- hexify :: [Int] -> String
85 -- hexify = concatMap (printf "%02x")
86
87 binify :: [Int] -> String
88 binify = concatMap (printf "%08b")
89
90 densify :: [Int] -> [Int]
91 densify ns = codes
92 where chunks = chunksOf 16 ns
93 compress = foldl1 xor
94 codes = map compress chunks