Skip to content

Instantly share code, notes, and snippets.

@bananaumai
Last active February 7, 2017 03:56
Show Gist options
  • Save bananaumai/324ce5d6f77933e3064dca174ac9b716 to your computer and use it in GitHub Desktop.
Save bananaumai/324ce5d6f77933e3064dca174ac9b716 to your computer and use it in GitHub Desktop.
プログラマ脳を鍛える数学パズル 問題14
import Data.List
main :: IO ()
main = print $ solve [ "def", "fdc", "abcd", "char", "hoge"]
solve :: [String] -> [String]
solve wrds = snd $ last $ sortOn fst $ map (\ws -> (calc ws, ws)) $ permutations wrds
calc :: [String] -> Int
calc [] = 0
calc wrds = let wrd = head wrds
in _calc (tail wrds) (last wrd) 1
_calc :: [String] -> Char -> Int -> Int
_calc [] _ acc = acc
_calc wrds c acc = let wrd = head wrds
h = head wrd
l = last wrd
in if h == c
then _calc (tail wrds) l (acc+1)
else _calc [] l acc
@bananaumai
Copy link
Author

output: ["abcd","def","fdc","char","hoge"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment