diff options
-rw-r--r-- | 2017/15.hs | 29 | ||||
-rw-r--r-- | 2017/15.in | 2 |
2 files changed, 31 insertions, 0 deletions
diff --git a/2017/15.hs b/2017/15.hs new file mode 100644 index 0000000..855b179 --- /dev/null +++ b/2017/15.hs @@ -0,0 +1,29 @@ +import Control.Monad +import Data.Bits + + +modv :: Int +modv = 2147483647 + +gen :: Int -> Int -> [Int] +gen mulv startv = lowbits r : gen mulv r + where r = mulv * startv `rem` modv + +gen2 :: Int -> Int -> Int -> [Int] +gen2 checkv mulv startv = if l .&. checkv == 0 then l : rest else rest + where r = mulv * startv `rem` modv + l = lowbits r + rest = gen2 checkv mulv r + +lowbits :: Int -> Int +lowbits n = n .&. 0xffff + +main :: IO () +main = do + [sA, sB] <- liftM (map (read . last . words) . lines) (readFile "15.in") + + print $ sum $ map (fromEnum . uncurry (==)) $ + zip (take 40000000 $ gen 16807 sA) (take 40000000 $ gen 48271 sB) + + print $ sum $ map (fromEnum . uncurry (==)) $ + zip (take 5000000 $ gen2 3 16807 sA) (take 5000000 $ gen2 7 48271 sB) diff --git a/2017/15.in b/2017/15.in new file mode 100644 index 0000000..99a8029 --- /dev/null +++ b/2017/15.in @@ -0,0 +1,2 @@ +Generator A starts with 116 +Generator B starts with 299 |