summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-12-15 10:22:41 +0100
committertomsmeding <tom.smeding@gmail.com>2017-12-15 10:22:41 +0100
commitc413221102749997e04370ebc5ff0994df45938f (patch)
tree3dfc95859f95c182990474a3f9b435d54b0ed8cb
parentc88ae38f6da0fa9ce97c9bffa5c3470145467bf7 (diff)
Day 15
Generators are cool. Please compile this, otherwise it will burn your CPU.
-rw-r--r--2017/15.hs29
-rw-r--r--2017/15.in2
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