From: Neil Smith Date: Thu, 14 Dec 2017 12:33:11 +0000 (+0000) Subject: Tried day 14 X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=3608c12b36e8fa0a4846663f42da942911dda263;p=advent-of-code-17.git Tried day 14 --- diff --git a/advent-of-code.cabal b/advent-of-code.cabal index 0461d0b..a5666f4 100644 --- a/advent-of-code.cabal +++ b/advent-of-code.cabal @@ -126,3 +126,10 @@ executable advent13 , text , megaparsec +executable advent14 + hs-source-dirs: src/advent14 + main-is: advent14.hs + default-language: Haskell2010 + build-depends: base >= 4.7 && < 5 + , split + , containers diff --git a/src/advent14/advent14.hs b/src/advent14/advent14.hs index 940fe7b..206d43e 100644 --- a/src/advent14/advent14.hs +++ b/src/advent14/advent14.hs @@ -2,14 +2,52 @@ 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 Data.Map.Strict ((!)) +import qualified Data.Graph as G + +type CellMap = M.Map (Int, Int) Bool puzzleKey = "xlqgujun" +main :: IO () +main = do + print $ part1 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 - where hashes = map knotHash $ rowSpecs key - rowCounts = map (countSetBits . binify) hashes + where binHashes = map binHash $ rowSpecs key + rowCounts = map countSetBits binHashes + + +-- part2 :: String -> Int +part2 key = cells + where binHashes = map binHash $ rowSpecs key + cells = presentCells binHashes + +binHash :: String -> String +binHash = binify . knotHash + +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'] + +adjacentCells :: CellMap -> (Int, Int) -> [(Int, Int)] +adjacentCells cells (r, c) = filter (\k -> M.member k cells) possibles + 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 = length $ G.stronglyConnComp [(k, numKey k, map numKey $ adjacentCells cells k) | k <- M.keys cells] rowSpecs :: String -> [String] rowSpecs key = map (((key ++ "-") ++) . show) [0..127] @@ -18,6 +56,7 @@ countSetBits :: String -> Int countSetBits = length . filter (== '1') + knotHash :: String -> [Int] knotHash input = densify tied where (tied, _, _) = foldl step ([0..255], 0, 0) hashTerms