Created
June 15, 2013 03:16
-
-
Save hiratara/5786734 to your computer and use it in GitHub Desktop.
An answer for http://nabetani.sakura.ne.jp/hena/ord11arithseq/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Main (main) where | |
import Data.Char | |
import Data.List | |
import Data.List.Split | |
import Test.HUnit | |
charToInt :: Char -> Int | |
charToInt c | isDigit c = fromEnum c - fromEnum '0' | |
| otherwise = fromEnum c - fromEnum 'a' + 10 | |
goWithDiff :: [Int] -> Int -> Int | |
goWithDiff [] _ = 0 | |
goWithDiff _ 0 = 1 | |
goWithDiff (x:xs) n = length withCommonDiff + 1 | |
where | |
withCommonDiff = takeWhile (`elem` xs) (map (\i -> x + n * i) [1 ..]) | |
solve :: String -> String | |
solve input = show . maximum $ [goWithDiff l n | l <- tails list, | |
n <- [0 .. maximum list]] | |
where | |
list = map charToInt input | |
doAssert :: [String] -> Assertion | |
doAssert (name:input:expected:_) = assertEqual name expected (solve input) | |
doAssert _ = error "Specify a list which contains more than 3 items." | |
main :: IO () | |
main = | |
readFile "patterns.tsv" | |
>>= runTestTT . test . map (doAssert . splitOn "\t") . lines | |
>> return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment