Guest User

Untitled

a guest
Aug 2nd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 0.82 KB | None | 0 0
  1. let triangle = "" // removed cause it's fuckin' big (paste in)
  2.  
  3. let largest =
  4.     let temp =
  5.         triangle.Replace("\r", String.Empty).Split('\n')
  6.         |> List.ofArray
  7.         |> List.map(fun line ->
  8.                 line.Split(' ')
  9.                 |> List.ofArray
  10.                 |> List.map(fun str -> Int32.Parse(str))
  11.            )
  12.         |> List.rev
  13.  
  14.     let length = temp.[0].Length
  15.  
  16.     temp
  17.     |> List.fold(fun acc line ->
  18.             acc
  19.             |> List.mapi(fun i e ->
  20.                     match (i < (line.Length)) with
  21.                     | true -> line.[i] + Math.Max(acc.[i], acc.[i+1])
  22.                     | _ -> acc.[i]
  23.                 )
  24.         )
  25.         (List.ofArray (Array.zeroCreate(length + 1))) // number of elements in bottom row + 1
  26.     |> Seq.nth 0
  27.  
  28. printfn "%d" largest
Add Comment
Please, Sign In to add comment