Skip to content

Instantly share code, notes, and snippets.

@hiratara
Created June 15, 2013 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiratara/5786734 to your computer and use it in GitHub Desktop.
Save hiratara/5786734 to your computer and use it in GitHub Desktop.
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