From 924c76c3b302c9f7018018dca7429c5202796075 Mon Sep 17 00:00:00 2001 From: Bert Peters Date: Sat, 16 Dec 2017 23:38:17 +0100 Subject: [PATCH] Implement day 15, in Haskell. --- 2017/README.md | 4 ++-- 2017/day-15/.gitignore | 3 +++ 2017/day-15/solution.hs | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 2017/day-15/.gitignore create mode 100644 2017/day-15/solution.hs diff --git a/2017/README.md b/2017/README.md index a6ec398..87e6583 100644 --- a/2017/README.md +++ b/2017/README.md @@ -14,7 +14,7 @@ The current plan, in no particular order: - [x] Clojure - [Day 03](./day-03/solution.clj) - [ ] Coffeescript - [x] Go - [Day 13](./day-13/solution.go) -- [ ] Haskell +- [x] Haskell - [Day 15](./day-15/solution.hs) - [ ] Java - [x] Kotlin - [Day 10](./day-10/solution.kt) - [x] Lex - [Day 09](./day-09/solution.l) @@ -26,7 +26,7 @@ The current plan, in no particular order: - [ ] Prolog - [ ] Python - [x] Ruby - [Day 08](./day-08/solution.rb) -- [ ] Rust +- [x] Rust - [Day 14](./day-14/solution.rs) - [x] Scala - [Day 11](./day-11/solution.scala) - [ ] Scheme - [ ] SQL diff --git a/2017/day-15/.gitignore b/2017/day-15/.gitignore new file mode 100644 index 0000000..103996e --- /dev/null +++ b/2017/day-15/.gitignore @@ -0,0 +1,3 @@ +solution +solution.hi +solution.o diff --git a/2017/day-15/solution.hs b/2017/day-15/solution.hs new file mode 100644 index 0000000..bcc8cde --- /dev/null +++ b/2017/day-15/solution.hs @@ -0,0 +1,22 @@ +import Data.Bits + +genA n = n : genA ((n * 16807) `rem` 2147483647) + +genB n = n : genB ((n * 48271) `rem` 2147483647) + +judge :: (Integer, Integer) -> Bool +judge (a, b) = (a .&. 0xffff) == (b .&. 0xffff) + +divisible :: Integer -> Integer -> Bool +divisible b = (== 0) . (`rem` b) + +genA' n = filter (divisible 4) $ genA n +genB' n = filter (divisible 8) $ genB n + +main :: IO () +main = do + let aStart = 618 + let bStart = 814 + + print $ length $ filter judge $ take 40000000 $ drop 1 $ zip (genA aStart) (genB bStart) + print $ length $ filter judge $ take 5000000 $ zip (genA' aStart) (genB' bStart)