AoC2019/day16.hs

40 lines
1.3 KiB
Haskell

import Data.Char
main = do
cont <- getContents
let content = (drop 5977377 ( concat $ replicate 10000 (map digitToInt $ init cont) ))
let test1 = doStepsP2 content 1
putStrLn (show $ length content)
--mapM putStrLn (map show patter)
putStrLn (show $ test1)
getPatternForIndex :: [Int] -> Int -> Int -> [Int]
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
get :: [Int] -> Int
get xs = mod (abs $ (sum xs)) 10
step :: [Int] -> [[Int]] -> [Int]
step xs patterns = map (\(a,b) -> get( zipWith (*) a b)) $ zip inputs patterns
where inputs = map (getInputForIndex xs) [0..(length patterns)]
doSteps :: [Int] -> [[Int]] -> Int -> [Int]
doSteps xs patterns cnt
| cnt == 0 = xs
| otherwise = doSteps (step xs patterns) patterns (cnt -1)
stepP2 :: [Int] -> Int -> [Int] -> [Int]
stepP2 xs sumIn acc
| sumIn == 0 = acc
| sumIn > 0 = stepP2 (drop 1 xs) (sumIn - (head xs)) (acc ++ [mod sumIn 10])
doStepsP2 :: [Int] -> Int ->[Int]
doStepsP2 xs cnt
| cnt == 0 = xs
| otherwise = doStepsP2 (stepP2 xs (sum(xs)) []) (cnt -1)