AoC2019/day16.hs

40 lines
1.3 KiB
Haskell
Raw Normal View History

2019-12-16 12:01:17 +01:00
import Data.Char
main = do
cont <- getContents
2019-12-16 21:16:22 +01:00
let content = drop 5977377 (concat (replicate 10000 (map digitToInt $ init cont) ))
let test1 = doStepsP2 content 100
2019-12-16 16:45:29 +01:00
putStrLn (show $ length content)
2019-12-16 12:01:17 +01:00
--mapM putStrLn (map show patter)
2019-12-16 21:16:22 +01:00
putStrLn (show $ take 8 test1)
2019-12-16 12:01:17 +01:00
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
2019-12-16 12:01:17 +01:00
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)]
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)
2019-12-16 16:45:29 +01:00
stepP2 :: [Int] -> Int -> [Int] -> [Int]
stepP2 xs sumIn acc
2019-12-16 21:16:22 +01:00
| sumIn == 0 = reverse acc
| sumIn > 0 = stepP2 (drop 1 xs) (sumIn - (head xs)) ((mod sumIn 10):acc)
2019-12-16 16:45:29 +01:00
doStepsP2 :: [Int] -> Int ->[Int]
doStepsP2 xs cnt
| cnt == 0 = xs
| otherwise = doStepsP2 (stepP2 xs (sum(xs)) []) (cnt -1)