-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.hs
30 lines (24 loc) · 1010 Bytes
/
hello.hs
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
import Data.Array (Array, listArray, (!), (//))
import Text.Printf (printf)
(.:) :: (b -> c) -> (a -> d -> b) -> a -> d -> c
(.:) = (.) . (.)
(.:.) :: (b -> c) -> (a -> d -> e -> b) -> a -> d -> e -> c
(.:.) = (.:) . (.)
doIt :: String -> String -> String
doIt = printf "%04d" .: hello
hello :: String -> String -> Int
hello message text = solution ! len `mod` 1000
where
len = length message
indexedMessage = zip [1..] message
base = (listArray (1, len) (repeat 0))
solution = foldl (aux indexedMessage) base text
aux :: [(Int, Char)] -> Array Int Int -> Char -> Array Int Int
aux indexedMessage outer textLetter = foldl (aux2 textLetter) outer indexedMessage
aux2 :: Char -> Array Int Int -> (Int, Char) -> Array Int Int
aux2 textLetter inner (index, messageLetter)
| messageLetter == textLetter =
if index == 1
then inner // [(1, inner!1 + 1)]
else inner // [(index, (inner!index + inner!(index-1)))]
| otherwise = inner