summaryrefslogtreecommitdiff
path: root/2017/15.hs
blob: 855b179f542a6f9245d75316c4f60d5e68beb2c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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)