2019-12-16 12:01:17 +01:00
import Data.Char
2019-12-16 13:04:34 +01:00
import Debug.Trace as T
2019-12-16 12:01:17 +01:00
main = do
2019-12-16 13:04:34 +01:00
cont <- getContents
let content = ( map digitToInt $ init cont )
2019-12-16 12:01:17 +01:00
let base = [ 0 , 1 , 0 , - 1 ]
2019-12-16 13:04:34 +01:00
let patter = drop 500 ( map ( getPatternForIndex base ( length content ) ) [ 0 .. ( length content ) ] )
let input = drop 500 content
let test1 = doSteps input patter 1
2019-12-16 12:29:06 +01:00
--putStrLn (show input)
2019-12-16 12:01:17 +01:00
--mapM putStrLn (map show patter)
putStrLn ( show test1 )
getPatternForIndex :: [ Int ] -> Int -> Int -> [ Int ]
2019-12-16 12:29:06 +01:00
getPatternForIndex patter length index = drop ( 1 + index ) $ take ( length + 1 ) ( cycle base )
where base = concat $ map ( replicate ( index + 1 ) ) patter
getInputForIndex :: [ Int ] -> Int -> [ Int ]
getInputForIndex xs index = drop index xs
2019-12-16 12:01:17 +01:00
get :: [ Int ] -> Int
get xs = mod ( abs $ ( sum xs ) ) 10
step :: [ Int ] -> [ [ Int ] ] -> [ Int ]
2019-12-16 12:29:06 +01:00
step xs patterns = map ( \ ( a , b ) -> get ( zipWith ( * ) a b ) ) $ zip inputs patterns
where inputs = map ( getInputForIndex xs ) [ 0 .. ( length patterns ) ]
2019-12-16 12:01:17 +01:00
doSteps :: [ Int ] -> [ [ Int ] ] -> Int -> [ Int ]
doSteps xs patterns cnt
| cnt == 0 = xs
| otherwise = doSteps ( step xs patterns ) patterns ( cnt - 1 )