-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem18.fsx
29 lines (22 loc) · 980 Bytes
/
problem18.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
(* Project Euler Problem 18
* By Weisi Dai <weisi@x-research.com>
*)
let numbers = System.IO.File.ReadLines "problem18.data"
|> Seq.map (fun str -> str.Split(' '))
|> Seq.map (Seq.map (int))
|> Seq.map List.ofSeq
|> List.ofSeq
let update (currentTotal: list<int>) (newLine: list<int>) =
List.init newLine.Length
(fun n -> if n = 0 then newLine.[0]
+ currentTotal.[0] else
if n = newLine.Length - 1 then newLine.[n]
+ currentTotal.[n - 1] else
newLine.[n] + max currentTotal.[n - 1]
currentTotal.[n])
let total = List.fold update
[ for i in 1 .. numbers.Length -> 0 ]
numbers
let problem18 = Seq.max total
let main = printfn "The answer is %d." (problem18)
main