Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 42 lines (35 sloc) 1.237 kb
6af2fa51 »
2011-10-31 Solution for problem #51.
1 module Main
2 where
3
4 import Data.Function
5 import Data.List
6 import Common (digitsToNumber, numberToDigits, isPrime, primes)
7
8 seqLength, numWildCards :: Int
9 seqLength = 8
10 numWildCards = 3
11
12 substitutions :: Int -> [Int]
13 substitutions p = [ digitsToNumber
14 . map (\d -> if d == wild then x else d) $ digits
15 | x <- [st..9] ]
16 where
17 st = if head digits == wild then 1 else 0
18 digits = numberToDigits p
19 wild = head . maximumBy (compare `on` length)
20 . groupBy (==) . sort $ digits
21
22 check :: [Int] -> Int -> Bool
23 check ps param = go ps 0 0
24 where
25 go [] count _ = count == param
26 go (n:ns) count total = if (total - count) > (10 - param)
27 then False
28 else if isPrime n
29 then go ns (count + 1) (total + 1)
30 else go ns count (total + 1)
31
32 solution :: [Int]
33 solution = [p | p <- primesN numWildCards, check (substitutions p) seqLength]
34
35 primesN :: Int -> [Int]
36 primesN n = filter (hasNdig) primes
37 where
38 hasNdig = any (== n) . map length
39 . groupBy (==) . sort . numberToDigits
40
41 main :: IO ()
42 main = print . head $ solution
Something went wrong with that request. Please try again.