2018-03-26 01:12:43 +00:00
|
|
|
# Find the maximum path from the top (root)
|
|
|
|
# of the triangle to the leaves of the triangle.
|
|
|
|
|
2018-03-25 22:51:31 +00:00
|
|
|
(defn myfold [xs ys]
|
|
|
|
(def xs1 (tuple-prepend xs 0))
|
|
|
|
(def xs2 (tuple-append xs 0))
|
2018-04-01 19:08:51 +00:00
|
|
|
(def m1 (map + xs1 ys))
|
|
|
|
(def m2 (map + xs2 ys))
|
|
|
|
(map max m1 m2))
|
2018-03-25 22:51:31 +00:00
|
|
|
|
|
|
|
(defn maxpath [t]
|
2018-04-01 19:08:51 +00:00
|
|
|
(extreme > (reduce myfold () t)))
|
|
|
|
|
|
|
|
# Test it
|
|
|
|
# Maximum path is 3 -> 10 -> 3 -> 9 for a total of 25
|
2018-03-25 22:51:31 +00:00
|
|
|
|
2018-05-05 18:05:56 +00:00
|
|
|
(def triangle '[
|
|
|
|
[3]
|
|
|
|
[7 10]
|
|
|
|
[4 3 7]
|
|
|
|
[8 9 1 3]
|
2018-03-25 22:51:31 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
(print (maxpath triangle))
|