{-# Language QuasiQuotes, NumDecimals #-}
module Main where
import Advent (countBy, format)
import Data.Bits ((.&.))
main :: IO ()
IO ()
main =
do (startA, startB) <- [format|2017 15 Generator A starts with %u%nGenerator B starts with %u%n|]
print $ countBy (uncurry match) $ take 40e6
$ zip (iterate nextA startA)
(iterate nextB startB)
print $ countBy (uncurry match) $ take 5e6
$ zip (filter (isDivisibleBy 4) (iterate nextA startA))
(filter (isDivisibleBy 8) (iterate nextB startB))
match :: Int -> Int -> Bool
match :: Int -> Int -> Bool
match Int
x Int
y = Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0xffff Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
y Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
0xffff
nextA, nextB :: Int -> Int
nextA :: Int -> Int
nextA Int
x = Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
16807 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
0x7fffffff
nextB :: Int -> Int
nextB Int
x = Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
48271 Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
0x7fffffff
isDivisibleBy ::
Int ->
Int ->
Bool
isDivisibleBy :: Int -> Int -> Bool
isDivisibleBy Int
x Int
y = Int
y Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0