Ver Fonte

[Haskell][18] Adding Solution

Vinicius Teshima há 4 meses atrás
pai
commit
fd5818cc6f
2 ficheiros alterados com 108 adições e 0 exclusões
  1. 58 0
      haskell/src/0018.hs
  2. 50 0
      haskell/src/Utils.hs

+ 58 - 0
haskell/src/0018.hs

@@ -0,0 +1,58 @@
+{-
+By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23.
+   *3*
+  *7* 4
+ 2 *4* 6
+8 5 *9* 3
+That is, 3 + 7 + 4 + 9 = 23.
+Find the maximum total from top to bottom of the triangle below:
+75
+95 64
+17 47 82
+18 35 87 10
+20 04 82 47 65
+19 01 23 75 03 34
+88 02 77 73 07 63 67
+99 65 04 28 06 16 70 92
+41 41 26 56 83 40 80 70 33
+41 48 72 33 47 32 37 16 94 29
+53 71 44 65 25 43 91 52 97 51 14
+70 11 33 28 77 73 17 78 39 68 17 57
+91 71 52 38 17 14 91 43 58 50 27 29 48
+63 66 04 68 89 53 67 30 73 16 69 87 40 31
+04 62 98 27 23 09 70 98 73 93 38 53 60 04 23
+NOTE: As there are only 16384 routes, it is possible to solve this problem by trying every route. However, Problem 67, is the same challenge with a triangle containing one-hundred rows; it cannot be solved by brute force, and requires a clever method! ;o)
+-}
+
+import Utils
+
+triangle :: String
+triangle = "75\n95 64\n17 47 82\n18 35 87 10\n20 04 82 47 65\n19 01 23 75 03 34\n88 02 77 73 07 63 67\n99 65 04 28 06 16 70 92\n41 41 26 56 83 40 80 70 33\n41 48 72 33 47 32 37 16 94 29\n53 71 44 65 25 43 91 52 97 51 14\n70 11 33 28 77 73 17 78 39 68 17 57\n91 71 52 38 17 14 91 43 58 50 27 29 48\n63 66 04 68 89 53 67 30 73 16 69 87 40 31\n04 62 98 27 23 09 70 98 73 93 38 53 60 04 23"
+
+lookAhead :: Int -> Tri Int -> Int
+lookAhead _  (EmptyTri) = 0
+lookAhead 0  _          = 0
+lookAhead td t          = go' 0 t
+  where
+    go' :: Int -> Tri Int -> Int
+    go' d EmptyTri     = 0
+    go' d (Node x l r)
+      | d >= td   = x
+      | otherwise = x + (go' (d+1) n)
+                      where n :: Tri Int
+                            --n = bigger (triValDef 0) [l, r]
+                            n = bigger (lookAhead (td-1)) [l, r]
+
+solution :: Int
+solution = go' (triParseString triangle)
+  where
+    go' :: Tri Int -> Int
+    go' EmptyTri    = 0
+    go' (Node x l r) = x + (go' n)
+                         where
+                           n :: Tri Int
+                           n = bigger (lookAhead 4) [l, r]
+
+
+main :: IO ()
+main = putStrLn ("Solution: " ++ show solution)

+ 50 - 0
haskell/src/Utils.hs

@@ -1,4 +1,10 @@
 module Utils where
+  import Data.Function
+  import Data.Maybe
+  import Data.List
+
+  (|>) = (&)
+
   (!?) :: [a] -> Int -> Maybe a
   l !? i
     | i >= (length l) = Nothing
@@ -94,3 +100,47 @@ module Utils where
   coalesce1 :: Maybe a -> a -> a
   coalesce1 (Just x)  _ = x
   coalesce1 (Nothing) d = d
+
+  bigger :: (a -> Int) -> [a] -> a
+  bigger f xs = xs !! i where li = map f xs
+                              i = fromJust $ elemIndex (maximum li) li
+
+  data Tri a = EmptyTri | Node a (Tri a) (Tri a)
+    deriving Show
+
+  triLeft :: Tri a -> Tri a
+  triLeft EmptyTri = EmptyTri
+  triLeft (Node _ l _) = l
+
+  triRight :: Tri a -> Tri a
+  triRight EmptyTri = EmptyTri
+  triRight (Node _ _ r) = r
+
+  triVal :: Tri a -> Maybe a
+  triVal EmptyTri     = Nothing
+  triVal (Node x _ _) = Just x
+
+  triValDef :: a -> Tri a -> a
+  triValDef d EmptyTri     = d
+  triValDef _ (Node x _ _) = x
+
+  triVal' :: Tri a -> a
+  triVal' (Node x _ _) = x
+
+  triParseString :: String -> Tri Int
+  triParseString s = lines s |> map words |> map (map read) |> go' 0
+    where
+      go' :: Int -> [[Int]] -> Tri Int
+      go' _ [] = EmptyTri
+      go' i [x] = Node (x !! i) EmptyTri EmptyTri
+      go' i (x:xs) = Node (x !! i) (go' (i+0) xs) (go' (i+1) xs)
+
+  tri2UnsortedList :: Int -> Tri a -> [a]
+  tri2UnsortedList _  (EmptyTri) = []
+  tri2UnsortedList td t          = go' t 0
+    where
+      go' (EmptyTri)   _ = []
+      go' (Node x l r) d
+        | d == td   = []
+        | otherwise = [x] ++ go' l (d+1) ++ go' r (d+1)
+