Day 14, part 2 - partial progress

This commit is contained in:
Jonathan Chan 2017-12-13 23:30:42 -08:00
parent 3ca34256b1
commit ac2c786b6c
1 changed files with 12 additions and 0 deletions

12
14.hs
View File

@ -2,6 +2,8 @@ import Data.List.Split (chunksOf)
import Data.Char (ord) import Data.Char (ord)
import Data.Bits (xor) import Data.Bits (xor)
import Text.Printf (printf) import Text.Printf (printf)
import Data.Matrix (Matrix, fromLists, safeGet)
import Data.HashSet (Set, member, insert)
type Length = Int type Length = Int
type State = ([Int], Int, Int) type State = ([Int], Int, Int)
@ -23,9 +25,19 @@ sparseHash lengths =
let (hashed, _, _) = iterate (hash lengths) ([0..255], 0, 0) !! 64 let (hashed, _, _) = iterate (hash lengths) ([0..255], 0, 0) !! 64
in concat . map (printf "%08b" . foldr xor 0) . chunksOf 16 $ hashed in concat . map (printf "%08b" . foldr xor 0) . chunksOf 16 $ hashed
visit :: Matrix Int -> (Int, Int) -> Set (Int, Int) -> Set (Int, Int)
visit matrix (row, col) seen = if member (row, col) seen then seen else
let newSeen = insert (row, col) seen
in
case safeGet row col matrix of
Nothing -> seen
Just 0 -> newSeen
Just 1 -> foldr (visit matrix) newSeen [(row + 1, col), (row - 1, col), (row, col + 1), (row, col - 1)]
main :: IO () main :: IO ()
main = do main = do
let hashes = map (sparseHash . (++ [17, 31, 73, 47, 23]) . map ord . ("ffayrhll-" ++) . show) [0..127] let hashes = map (sparseHash . (++ [17, 31, 73, 47, 23]) . map ord . ("ffayrhll-" ++) . show) [0..127]
used = length . filter (== '1') . concat $ hashes used = length . filter (== '1') . concat $ hashes
matrix = fromLists hashes
print $ used print $ used
mapM_ print hashes mapM_ print hashes