From b12aa2915a6ed38aafcc65eee4468b184742b957 Mon Sep 17 00:00:00 2001 From: Jonathan Chan Date: Fri, 15 Dec 2017 09:28:02 -0800 Subject: [PATCH] Day 15 - refactored strict version with some fancy ~~custom operators~~ --- 15_strict.hs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/15_strict.hs b/15_strict.hs index 821c959..f36c8c4 100644 --- a/15_strict.hs +++ b/15_strict.hs @@ -1,18 +1,23 @@ {-# LANGUAGE BangPatterns #-} import Data.Function (on) import Data.Bits ((.&.)) + +divisor = 2147483647 +factors = (16807, 48271) +seed = (634, 301) -count :: Int -> (Int, Int) -> (Int, Int) -> (Int, Int) -> Int -> Int -> Int -count divisor (factorA, factorB) (seedA, seedB) (divA, divB) acc times = +count :: (Int, Int) -> (Int, Int) -> Int -> Int -> Int +count pair masks acc times = if times == 0 then acc else - let !nextA = nextMaskBy factorA divA seedA - !nextB = nextMaskBy factorB divB seedB - !eq = fromEnum $ ((==) `on` (.&. 0xffff)) nextA nextB - in count divisor (factorA, factorB) (nextA, nextB) (divA, divB) (acc + eq) (times - 1) - where - nextMaskBy f d s = let t = (f * s) `mod` divisor in if (t .&. d) == 0 then t else nextMaskBy f d t + let !next = nextMaskBy <$$> factors <**> masks <**> pair + !eq = fromEnum . uncurry ((==) `on` (.&. 0xffff)) $ next + in count next masks (acc + eq) (times - 1) + where + h <$$> (x, y) = (h x, h y) + (f, g) <**> (x, y) = (f x, g y) + nextMaskBy f d s = let t = (f * s) `mod` divisor in if (t .&. d) == 0 then t else nextMaskBy f d t main :: IO () main = do - print $ count 2147483647 (16807, 48271) (634, 301) (0, 0) 0 40000000 - print $ count 2147483647 (16807, 48271) (634, 301) (3, 7) 0 5000000 \ No newline at end of file + print $ count seed (0, 0) 0 40000000 + print $ count seed (3, 7) 0 5000000 \ No newline at end of file