1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-01 04:19:55 +00:00
janet/examples/maxtriangle.janet

22 lines
409 B
Plaintext
Raw Normal View History

# 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]
2019-03-21 18:32:08 +00:00
(let [m1 (map + [;xs 0] ys)
m2 (map + [0 ;xs] ys)]
(map max m1 m2)))
2018-03-25 22:51:31 +00:00
(defn maxpath [t]
(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
(def triangle
'[[3]
[7 10]
[4 3 7]
[8 9 1 3]])
2018-03-25 22:51:31 +00:00
(print (maxpath triangle))