From 3ca34256b1815befeba9ad99c4e6e07fef1e6240 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Wed, 13 Dec 2017 23:10:07 -0800 Subject: [PATCH] Day 14, part 1 --- 14.hs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 14.hs diff --git a/14.hs b/14.hs new file mode 100644 index 0000000..fb6b321 --- /dev/null +++ b/14.hs @@ -0,0 +1,31 @@ +import Data.List.Split (chunksOf) +import Data.Char (ord) +import Data.Bits (xor) +import Text.Printf (printf) + +type Length = Int +type State = ([Int], Int, Int) +(%) = mod + +twist :: State -> Length -> State +twist (ring, position, skip) len = + let rotated = slice position 256 $ cycle ring + twisted = (reverse $ take len rotated) ++ drop len rotated + newRing = slice (256 - position) 256 $ cycle twisted + in (newRing, (position + len + skip) % 256, skip + 1) + where slice start amount = take amount . drop start + +hash :: [Length] -> State -> State +hash lengths state = foldl twist state lengths + +sparseHash :: [Int] -> String +sparseHash lengths = + let (hashed, _, _) = iterate (hash lengths) ([0..255], 0, 0) !! 64 + in concat . map (printf "%08b" . foldr xor 0) . chunksOf 16 $ hashed + +main :: IO () +main = do + let hashes = map (sparseHash . (++ [17, 31, 73, 47, 23]) . map ord . ("ffayrhll-" ++) . show) [0..127] + used = length . filter (== '1') . concat $ hashes + print $ used + mapM_ print hashes \ No newline at end of file