Implement day 15, in Haskell.

This commit is contained in:
2017-12-16 23:38:17 +01:00
parent 56fb300d3b
commit 924c76c3b3
3 changed files with 27 additions and 2 deletions

View File

@@ -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

3
2017/day-15/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
solution
solution.hi
solution.o

22
2017/day-15/solution.hs Normal file
View File

@@ -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)