Kaynağa Gözat

[Haskell][17] Adding Solution

Vinicius Teshima 4 ay önce
ebeveyn
işleme
dfee3c82bd
2 değiştirilmiş dosya ile 77 ekleme ve 1 silme
  1. 70 0
      haskell/src/0017.hs
  2. 7 1
      haskell/src/Utils.hs

+ 70 - 0
haskell/src/0017.hs

@@ -0,0 +1,70 @@
+{-
+If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
+If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
+NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
+-}
+
+import Data.Char
+import Data.Function
+
+(|>) = (&)
+
+-- Round Down to Nearest Ten
+rd2nt :: Int -> Int
+rd2nt x
+  | x < 10  = 0
+  | x < 20  = 10
+  | x < 30  = 20
+  | x < 40  = 30
+  | x < 50  = 40
+  | x < 60  = 50
+  | x < 70  = 60
+  | x < 80  = 70
+  | x < 90  = 80
+  | x < 100 = 90
+  | x >= 100 = rd2nt (mod x 100)
+
+digit_to_name :: Int -> String
+digit_to_name 0  = ""
+digit_to_name 1  = "one"
+digit_to_name 2  = "two"
+digit_to_name 3  = "three"
+digit_to_name 4  = "four"
+digit_to_name 5  = "five"
+digit_to_name 6  = "six"
+digit_to_name 7  = "seven"
+digit_to_name 8  = "eight"
+digit_to_name 9  = "nine"
+digit_to_name 10 = "ten"
+digit_to_name 11 = "eleven"
+digit_to_name 12 = "twelve"
+digit_to_name 13 = "thirteen"
+digit_to_name 14 = "fourteen"
+digit_to_name 15 = "fifteen"
+digit_to_name 16 = "sixteen"
+digit_to_name 17 = "seventeen"
+digit_to_name 18 = "eighteen"
+digit_to_name 19 = "nineteen"
+digit_to_name 20 = "twenty"
+digit_to_name 30 = "thirty"
+digit_to_name 40 = "forty"
+digit_to_name 50 = "fifty"
+digit_to_name 60 = "sixty"
+digit_to_name 70 = "seventy"
+digit_to_name 80 = "eighty"
+digit_to_name 90 = "ninety"
+digit_to_name 1000 = "one thousand"
+digit_to_name x
+  | (mod x 100) == 0 = digit_to_name (div x 100) ++ " hundred"
+  | x < 100          = digit_to_name (rd2nt x) ++ " " ++ digit_to_name (mod x 10)
+  | x < 1000         = digit_to_name (div x 100) ++ " hundred and " ++ digit_to_name (mod x 100)
+
+
+isNotSpace :: Char -> Bool
+isNotSpace x = isSpace x |> not
+
+solution :: Int
+solution = map digit_to_name [1..1000] |> map (filter isNotSpace) |> map length |> sum
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)

+ 7 - 1
haskell/src/Utils.hs

@@ -5,7 +5,6 @@ module Utils where
     | i == 0          = car l
     | otherwise       = cdr l >>= \x -> x !? (i-1)
 
-
   car :: [a] -> Maybe a
   car []    = Nothing
   car [x]   = Just x
@@ -88,3 +87,10 @@ module Utils where
         | otherwise = case matrix_get m (x p) (y p) of
                         Nothing -> Nothing
                         Just z -> go' m a d (point_next_dir p d) (c+1) >>= \w -> Just ([z] ++ w)
+
+  coalesce2 :: Maybe a -> Maybe a -> a -> a
+  coalesce2 x y d = coalesce1 y (coalesce1 x d)
+
+  coalesce1 :: Maybe a -> a -> a
+  coalesce1 (Just x)  _ = x
+  coalesce1 (Nothing) d = d