Skip to content

Instantly share code, notes, and snippets.

@nobsun
Created March 23, 2017 01:12
Show Gist options
  • Select an option

  • Save nobsun/a8ab7e6cd3fd5c4a0de0cd989f796dbb to your computer and use it in GitHub Desktop.

Select an option

Save nobsun/a8ab7e6cd3fd5c4a0de0cd989f796dbb to your computer and use it in GitHub Desktop.
「トリオミノの分類」問題 ref: http://qiita.com/nobsun/items/2b9b15bcaa50b32e4fbb
module F03 where
import Data.Ix (range)
import Data.List (sort)
import Data.Maybe (mapMaybe)
import Data.Tuple (swap)
type Index = (Int,Int)
type Omino = [Index]
type Moves = [Index]
toIndex :: Char -> Maybe Index
toIndex = flip lookup itable
readOmino :: String -> Omino
readOmino = sort . mapMaybe toIndex
itable :: [(Char,Index)]
itable = zip ['a' .. 'y'] $ map swap $ range ((1,1),(5,5))
moves :: Omino -> Moves
moves [_] = []
moves (p:qs@(q:_)) = move p q : moves qs
move :: Index -> Index -> Index
move (x,y) (x',y') = (x'-x,y'-y)
otable :: [(Moves, String)]
otable = [([(1,-1),(0,1)],"J")
,([(0,1),(1,0)],"L")
,([(1,0),(0,1)],"T")
,([(0,1),(1,-1)],"R")
,([(0,1),(0,1)],"I")
,([(1,0),(1,0)],"B")
]
type Problem = String
type Answer = String
f03 :: Problem -> Answer
f03 = maybe "-" id . flip lookup otable . moves . readOmino
type Test = (Problem, Answer)
{- |
>>> test ( "cba", "B" )
True
>>> test ( "yam", "-" )
True
>>> test ( "aaa", "-" )
True
>>> test ( "def", "-" )
True
>>> test ( "gga", "-" )
True
>>> test ( "bbf", "-" )
True
>>> test ( "gmh", "T" )
True
>>> test ( "mhn", "L" )
True
>>> test ( "dea", "-" )
True
>>> test ( "mrn", "R" )
True
>>> test ( "hcm", "I" )
True
>>> test ( "mno", "B" )
True
>>> test ( "snr", "J" )
True
>>> test ( "xnn", "-" )
True
>>> test ( "nnl", "-" )
True
>>> test ( "kop", "-" )
True
>>> test ( "ejd", "T" )
True
>>> test ( "txy", "J" )
True
>>> test ( "pvu", "L" )
True
>>> test ( "baf", "R" )
True
>>> test ( "hhc", "-" )
True
>>> test ( "ono", "-" )
True
>>> test ( "wxv", "B" )
True
>>> test ( "bdc", "B" )
True
>>> test ( "ojt", "I" )
True
>>> test ( "fkp", "I" )
True
-}
test :: Test -> Bool
test (p,a) = f03 p == a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment