Tried day 14
[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 Data.Map.Strict ((!))
7 import qualified Data.Graph as G
8
9 type CellMap = M.Map (Int, Int) Bool
10
11 puzzleKey = "xlqgujun"
12
13 main :: IO ()
14 main = do
15 print $ part1 puzzleKey
16
17 -- part1 :: String -> Int
18 -- part1 key = sum rowCounts
19 -- where hashes = map knotHash $ rowSpecs key
20 -- rowCounts = map (countSetBits . binify) hashes
21
22 part1 :: String -> Int
23 part1 key = sum rowCounts
24 where binHashes = map binHash $ rowSpecs key
25 rowCounts = map countSetBits binHashes
26
27
28 -- part2 :: String -> Int
29 part2 key = cells
30 where binHashes = map binHash $ rowSpecs key
31 cells = presentCells binHashes
32
33 binHash :: String -> String
34 binHash = binify . knotHash
35
36 numKey :: (Int, Int) -> Int
37 numKey (r, c) = 128 * r + c
38
39
40 presentCells :: [String] -> CellMap
41 presentCells binHashes = M.fromList [((r, c), True) | r <- [0..127], c <- [0..127], (binHashes!!r)!!c == '1']
42
43 adjacentCells :: CellMap -> (Int, Int) -> [(Int, Int)]
44 adjacentCells cells (r, c) = filter (\k -> M.member k cells) possibles
45 where possibles = [(r, c - 1), (r, c + 1), (r - 1, c), (r + 1, c)]
46 -- isPresent rc = length $ rc `member` cells
47
48
49 cellEdges :: CellMap -> Int
50 cellEdges cells = length $ G.stronglyConnComp [(k, numKey k, map numKey $ adjacentCells cells k) | k <- M.keys cells]
51
52 rowSpecs :: String -> [String]
53 rowSpecs key = map (((key ++ "-") ++) . show) [0..127]
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