From 7c8cb639cb827fa60e32da31722a60235c27b181 Mon Sep 17 00:00:00 2001 From: Martin Potier Date: Fri, 18 Dec 2020 14:36:58 +0200 Subject: [PATCH] Still too slow --- day13/main.hs | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/day13/main.hs b/day13/main.hs index 58a2ebc..871b01b 100755 --- a/day13/main.hs +++ b/day13/main.hs @@ -1,4 +1,4 @@ -#! /usr/bin/env -S"ANSWER=42" nix-shell +#! /usr/bin/env -S"GHCRTS=-N4" nix-shell #! nix-shell -p ghcid #! nix-shell -p "haskellPackages.ghcWithPackages (p: with p; [pretty-simple attoparsec arithmoi containers])" #! nix-shell -i "ghcid -c 'ghci' -T main" @@ -92,40 +92,27 @@ modList = fmap f findAllEq :: [[Int]] -> Maybe [Int] findAllEq = find (\xs -> notElem (head xs) xs) -isT :: Integer -> Int -> [(Int,Int)] -> Integer +-- Can you feel how much fun I'm not having anymore? +isT :: Int -> Int -> [(Int,Int)] -> Int isT tpx x xs = head $ catMaybes $ map go allTs where - allTs = [158478605388*tpx,158478605388*2*tpx..] - go :: Integer -> Maybe Integer + allTs = [158478605388*tpx,158478605389*tpx..] + go :: Int -> Maybe Int go tpx0 = case and (map (go' tpx0) xs) of - True -> Just (tpx0 - (fromIntegral x)) + True -> Just (tpx0 - x) _ -> Nothing go' tpx0' (shift,fact) = -- trace ("tpx:"<>show tpx0'<>",shift:"<>show shift<>",x:"<>show x<>",fact:"<>show fact) $ - -- traceShowId $ - (tpx0' - (fromIntegral x) + (fromIntegral shift)) `mod` (fromIntegral fact) == 0 + (tpx0' - x + shift) `mod` fact == 0 --- Solving part 2 --- --- Simple example with [(0,3),(1,5)], we're looking for a t, such that --- t + 0 = 3 * i, t+0 is divisible by 3 and --- t + 1 = 5 * j, t+1 is divisible by 5 --- --- Surely there must be some relation between i and j? There should be a number --- such that 3 * i_0 = 5 * j_0 = n_0. n is here the least common multiple (lcm) --- of 3 and 5, in our case lcm(3,5) = 15 (because both numbers are prime) --- --- 3 * i_0 = 15 & 5 * j_0 = 15 -> i_0 = 5 & j_0 = 3 --- --- Now what? -solvePart2 :: String -> Either String Integer +solvePart2 :: String -> Either String Int solvePart2 str = do (_,xs) <- parseInput2 str let startAndIds = catMaybes $ sequence <$> zip [0..] xs - let sortedStartAndIds = reverse $ sortOn snd $ startAndIds + let sortedStartAndIds = traceShowId $ reverse $ sortOn snd $ startAndIds -- pure $ sortedStartAndIds let (x,t) = head sortedStartAndIds - pure $ isT (fromIntegral t) x sortedStartAndIds + pure $ isT t x sortedStartAndIds main :: IO () main = do