LoginSignup
0
0

More than 5 years have passed since last update.

1日1個 @nabetani さんの作った問題を解くAdventCalendarの22日目です。明後日にイブを控えて、今年作られた我が家のクリスマスケーキも数えきれないほどの数になりました。もうまもなくですね!

今日の問題は http://nabetani.sakura.ne.jp/hena/ord9nummake/ にあります。

module Doukaku.Numbers (solve) where
import Data.List (sort)
solve :: String -> String
solve input = let ns' = drop (read order - 1) . uniq . sort $ ds
              in if null ns' then "-" else head ns'
  where
    (order, _:is) = break (== ':') input
    is' = concatMap inflate . zip is $ [(0 :: Int)..]
      where
        inflate (d, n)
          | d `elem` "69" = [('6', n), ('9', n)]
          | otherwise = [(d, n)]
    ds = [[d1, d2, d3, d4] | (d1, n1) <- is', d1 /= '0',
                             (d2, n2) <- is', n2 /= n1,
                             (d3, n3) <- is', n3 `notElem` [n1, n2],
                             (d4, n4) <- is', n4 `notElem` [n1, n2, n3]]
    uniq (x:y:ys) = (if x == y then id else (x :)) . uniq $ (y:ys)
    uniq xs = xs

内包表記で順列を作り、uniqsortして指定された順のカードを返します。69の扱いはカードにID値をふったので特に苦労もなく。

http://qiita.com/items/7c46e8409c456e8fabd1 に他の方の回答もありますので、見ると参考になるでしょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0