Skip to content

Instantly share code, notes, and snippets.

@igrep
Created March 24, 2013 11:09
Show Gist options
  • Select an option

  • Save igrep/5231439 to your computer and use it in GitHub Desktop.

Select an option

Save igrep/5231439 to your computer and use it in GitHub Desktop.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import System.Environment (getArgs)
import Data.String.Utils (split)
import Data.Char (digitToInt, intToDigit, isDigit)
import Data.List (sort, permutations)
import Data.Maybe (listToMaybe)
-- |
-- >>> parseInput "13:01167"
-- (13, [0, 1, 1, 6, 7] )
-- >>> parseInput ""
-- *** Exception: Invalid Input: ""
-- >>> parseInput ":"
-- *** Exception: Invalid Input: ":"
-- >>> parseInput ":1"
-- *** Exception: Invalid Input: ":1"
-- >>> parseInput "1:"
-- *** Exception: Invalid Input: "1:"
-- >>> parseInput "1:123"
-- *** Exception: Invalid Input: "1:123"
parseInput :: String -> (Int, [Int])
parseInput s = parseInputList $ split ":" s
where
parseInputList (s1:s2:[])
| isDigits s1 && isDigits s2 && length s2 == 4 = ( read s1 :: Int, map digitToInt s2 )
| otherwise = invalidInput
parseInputList _ = invalidInput
invalidInput = error $ "Invalid Input: " ++ show s
solve :: Int -> [Int] -> Maybe [Int]
solve i is = i `thElemOf` fourDigits
where
is' = sort is
fourDigits = filter ( (4 ==) . length ) $ permutationsWithoutZeroHead is'
permutationsWithoutZeroHead is'' = do
overZero <- overZeros
let without_a_OverZero = undefined
map (without_a_OverZero:) $ permutations without_a_OverZero
where
overZeros = filter (0 /=) is''
-- |
-- >>> 2 `thElemOf` [1..2]
-- Just 2
-- >>> 1 `thElemOf` [1]
-- Just 1
-- >>> 0 `thElemOf` [1]
-- Nothing
thElemOf :: Int -> [a] -> Maybe a
thElemOf i = listToMaybe . drop ( i - 1 )
-- |
-- >>> solveFromInput "13:01167"
-- "1109"
-- >>> solveFromInput "1:1234"
-- "1234"
-- >>> solveFromInput "2:1234"
-- "1243"
-- >>> solveFromInput "7:1234"
-- "2134"
-- >>> solveFromInput "24:1234"
-- "4321"
-- >>> solveFromInput "25:1234"
-- "-"
-- >>> solveFromInput "2:1116"
-- "1119"
-- >>> solveFromInput "2:0116"
-- "1019"
-- >>> solveFromInput "3:6666"
-- "6696"
-- >>> solveFromInput "16:6666"
-- "9999"
-- >>> solveFromInput "10:01267"
-- "1097"
-- >>> solveFromInput "14:111167"
-- "1671"
-- >>> solveFromInput "1:1111"
-- "1111"
-- >>> solveFromInput "2:1111"
-- "-"
-- >>> solveFromInput "1:666633"
-- "3366"
-- >>> solveFromInput "72:666611"
-- "9999"
-- >>> solveFromInput "73:666622"
-- "-"
-- >>> solveFromInput "1:666600"
-- "6006"
-- >>> solveFromInput "52:666600"
-- "9999"
-- >>> solveFromInput "53:666600"
-- "-"
-- >>> solveFromInput "25:06688"
-- "8086"
-- >>> solveFromInput "93:02566"
-- "6502"
-- >>> solveFromInput "11:06688"
-- "6809"
-- >>> solveFromInput "169:01268"
-- "9801"
-- >>> solveFromInput "174:01268"
-- "9821"
-- >>> solveFromInput "66:012288"
-- "8201"
solveFromInput :: String -> String
solveFromInput s = maybe "-" (map intToDigit) $ uncurry solve $ parseInput s
isDigits :: String -> Bool
isDigits = all isDigit
main :: IO ()
main = do
(input:_) <- getArgs
putStrLn $ solveFromInput input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment