From 417c4ede04df4bdd1bda1ba8e43842401b66c4fa Mon Sep 17 00:00:00 2001 From: Arranun Date: Mon, 16 Dec 2019 16:45:29 +0100 Subject: [PATCH] Day16: This should be faster ... --- day16.hs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/day16.hs b/day16.hs index a449179..50d8429 100644 --- a/day16.hs +++ b/day16.hs @@ -1,17 +1,12 @@ import Data.Char -import Debug.Trace as T main = do cont <- getContents - let content = (map digitToInt $ init cont) - let base = [0,1,0,-1] - let patter = drop 500 (map (getPatternForIndex base (length content)) [0..(length content)]) - let input = drop 500 content - - let test1 = doSteps input patter 1 - --putStrLn (show input) + 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) + putStrLn (show $ test1) getPatternForIndex :: [Int] -> Int -> Int -> [Int] getPatternForIndex patter length index = drop (1 + index) $ take (length +1) (cycle base) @@ -31,3 +26,14 @@ 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)