LoginSignup
0
0

More than 5 years have passed since last update.

1日1個 @nabetani さんの作った問題を解くAdventCalendarの13日目です。

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

module Doukaku.XYSort (solve) where
import Data.List (transpose, sort)
import Data.Char (ord)

table :: [[Int]]
table = [
  [4, 1, 4, 2, 1, 3]
  , [7, 3, 2, 0, 5, 0]
  , [2, 3, 6, 0, 6, 7]
  , [6, 4, 5, 7, 5, 1]
  , [3, 1, 6, 6, 2, 4]
  , [6, 0, 5, 5, 5, 1]
  ]

solve :: String -> String
solve = concatMap show . head . foldl (flip change) table

num :: Char -> Int
num c
  | c `elem` "ABCDEF" = ord c - ord 'A'
  | otherwise         = ord c - ord 'u'

change :: Char -> [[Int]] -> [[Int]]
change c
  | c `elem` "ABCDEF" = transpose . change' (num c) . transpose
  | otherwise         = change' (num c)

change' :: Int -> [[Int]] -> [[Int]]
change' n tb = map ((tb !!) . snd) . sort . zip base $ [0..]
  where
    base = transpose tb !! n

!!使いまくっているので行儀も効率も悪いですが、素直な感じになりました。

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

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