Skip to content

Instantly share code, notes, and snippets.

@nobsun
Created April 23, 2017 12:19
Show Gist options
  • Save nobsun/fd84283e5af31f2cf3ac1702b5405af9 to your computer and use it in GitHub Desktop.
Save nobsun/fd84283e5af31f2cf3ac1702b5405af9 to your computer and use it in GitHub Desktop.
「正八角形の分割」問題 ref: http://qiita.com/nobsun/items/2c227bab1156eddeb3df
module F04 where
import Data.Bool (bool)
import Data.List (sort,unfoldr)
import Text.Printf (printf)
readDiag :: String -> Word
readDiag = read
toBin :: Word -> String
toBin = printf "%08b"
angles :: String -> [Int]
angles = unfoldr phi
where
phi "" = Nothing
phi xs = case break ('1'==) xs of
(xs,_:ys) -> Just (length xs,ys)
type Problem = String
type Answer = String
f04 :: Problem -> Answer
f04 = sort
. concatMap (show . count)
. angles
. take 8
. tail
. dropWhile ('0'==)
. cycle
. toBin
. readDiag
count :: Int -> Int
count n = bool (n+3) 5 (n == 3)
type Test = (Problem, Answer)
{- |
>>> test ( "165", "3445" )
True
>>> test ( "80", "48" )
True
>>> test ( "255", "33333333" )
True
>>> test ( "68", "55" )
True
>>> test ( "200", "355" )
True
>>> test ( "82", "455" )
True
>>> test ( "164", "455" )
True
>>> test ( "73", "455" )
True
>>> test ( "146", "455" )
True
>>> test ( "37", "455" )
True
>>> test ( "74", "455" )
True
>>> test ( "148", "455" )
True
>>> test ( "41", "455" )
True
>>> test ( "38", "355" )
True
>>> test ( "76", "355" )
True
>>> test ( "152", "355" )
True
>>> test ( "49", "355" )
True
>>> test ( "98", "355" )
True
>>> test ( "196", "355" )
True
>>> test ( "137", "355" )
True
>>> test ( "19", "355" )
True
>>> test ( "20", "48" )
True
>>> test ( "9", "57" )
True
>>> test ( "209", "3345" )
True
>>> test ( "121", "33345" )
True
>>> test ( "239", "3333334" )
True
>>> test ( "26", "347" )
True
>>> test ( "111", "333344" )
True
>>> test ( "95", "333344" )
True
>>> test ( "85", "4444" )
True
>>> test ( "24", "39" )
True
>>> test ( "97", "347" )
True
>>> test ( "234", "33444" )
True
>>> test ( "59", "33345" )
True
>>> test ( "187", "333344" )
True
>>> test ( "34", "55" )
True
>>> test ( "249", "333335" )
True
>>> test ( "43", "3445" )
True
>>> test ( "143", "33335" )
True
>>> test ( "28", "338" )
True
>>> test ( "79", "33345" )
True
>>> test ( "173", "33444" )
True
>>> test ( "55", "33345" )
True
>>> test ( "77", "3445" )
True
>>> test ( "35", "355" )
True
>>> test ( "153", "3355" )
True
>>> test ( "30", "3337" )
True
>>> test ( "228", "3355" )
True
>>> test ( "177", "3345" )
True
>>> test ( "162", "445" )
True
>>> test ( "184", "3345" )
True
-}
test :: Test -> Bool
test (p,a) = f04 p == a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment