1
0
Fork 0
This commit is contained in:
Jonathan Chan 2017-12-09 23:00:44 -08:00
parent 732092abe3
commit a96ab19024
2 changed files with 55 additions and 0 deletions

54
10.hs Normal file
View File

@ -0,0 +1,54 @@
import Data.List.Split (splitOn)
import Data.Sequence (Seq, update, fromList)
import qualified Data.Sequence as S (lookup)
import Data.Char (ord)
import Data.Bits (xor)
import Data.Foldable (toList)
import Numeric (showHex)
type Ring = Seq Int
type Position = Int
type Skip = Int
type Length = Int
type State = (Ring, Position, Skip)
(%) = mod
unsafeLookup :: Int -> Seq Int -> Int
unsafeLookup index seq =
case S.lookup index seq of
Just i -> i
twist :: State -> Length -> State
twist (ring, position, skip) len =
let end = position + len - 1
positions = map (% 256) [position..end]
posRev = zip positions $ reverse positions
newRing = foldl (\currRing (pos, rev) -> update pos (unsafeLookup rev ring) currRing) ring posRev
in (newRing, (end + skip + 1) % 256, skip + 1)
hash :: [Length] -> State -> State
hash lengths state = foldl twist state lengths
condense :: [Int] -> [Int]
condense [] = []
condense ns =
let block = foldr xor 0 $ take 16 ns
in block : (condense $ drop 16 ns)
showHexPrepended :: Int -> (String -> String)
showHexPrepended n
| n < 15 = showHex 0 . showHex n
| otherwise = showHex n
main :: IO ()
main = do
input <- readFile "10.txt"
let lengths = map read $ splitOn "," input
newLengths = map ord input ++ [17, 31, 73, 47, 23]
(hashed, _, _) = hash lengths (fromList [0..255], 0, 0)
(sparseHash, _, _) = iterate (hash newLengths) (fromList [0..255], 0, 0) !! 64
condensedHash = condense $ toList sparseHash
hexString = foldr (\digit str -> showHexPrepended digit $ str) "" condensedHash
print $ (unsafeLookup 0 hashed) * (unsafeLookup 1 hashed)
print $ hexString

1
10.txt Normal file
View File

@ -0,0 +1 @@
18,1,0,161,255,137,254,252,14,95,165,33,181,168,2,188