From c413221102749997e04370ebc5ff0994df45938f Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Fri, 15 Dec 2017 10:22:41 +0100 Subject: Day 15 Generators are cool. Please compile this, otherwise it will burn your CPU. --- 2017/15.hs | 29 +++++++++++++++++++++++++++++ 2017/15.in | 2 ++ 2 files changed, 31 insertions(+) create mode 100644 2017/15.hs create mode 100644 2017/15.in 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 -- cgit v1.2.3-70-g09d2