language: Haskell (ghc-6.8.2)
date: 108 days 8 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import Data.List 
import Data.Char
import Data.Maybe ( fromJust ) 
import qualified Data.IntMap as M 
import qualified Data.ByteString.Lazy.Char8 as BS
 
byteSum :: BS.ByteString -> Integer 
byteSum s = BS.foldr ( \x y -> y + ( fromIntegral . digitToInt $ x )  ) 0 s 
 
 
{--
testSeven :: BS.ByteString -> Integer 
testSeven s = sum. BS.zipWith (\x y -> fromIntegral $ digitToInt x * digitToInt y )  s . BS.pack. concatMap show .  concat. repeat 
                           $ [ 1 , 3 , 2 , 6 , 4 , 5 ]
 
--}
 
numDiv :: Int -> BS.ByteString -> Bool 
numDiv a s
  | a == 0 = False
  | a == 1 = True 
  | a == 2 = if even . digitToInt . BS.last $ s then True else False
  | a == 3 = if mod ( byteSum s ) 3 == 0 then True else False
  | a == 4 = if mod ( fst . fromJust . BS.readInt . BS.drop k $ s ) 4 == 0 then True else False
  | a == 5 = if mod ( digitToInt . BS.last $ s ) 5 == 0 then True else False
  | a == 6 =  numDiv 2 s && numDiv 3 s 
  | a == 7 =  if BS.foldr ( \x y -> mod ( 10 * y + digitToInt x ) 7 ) 0 s == 0 then True else False 
  | a == 8 = if mod ( fst . fromJust . BS.readInt . BS.drop m $ s ) 8 == 0 then True else False
  | a == 9 = if mod ( byteSum  s ) 9 == 0 then True else False where 
         len = BS.length s 
         k = len - 2 
         m = len - 3 
 
 
 
solve :: BS.ByteString -> BS.ByteString
solve s = BS.pack.show $  ret where 
     mp = BS.foldr ( \x y -> M.insertWith (+) ( digitToInt x ) 1 y ) M.empty s 
 
     key = M.keys mp 
     ret = foldr ( \x y -> if numDiv x s then y + ( fromJust . M.lookup  x $ mp )  else  y    ) 0 key
 
 
main = BS.interact $ BS.unlines . map solve . BS.lines