-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblem22.fsx
31 lines (23 loc) · 875 Bytes
/
problem22.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
29
30
31
(* Project Euler Problem 22
* By Weisi Dai <[email protected]>
*)
let NAMES_FILENAME = "./problem22.res"
let NAMES_URL = "http://projecteuler.net/project/names.txt"
if not <| System.IO.File.Exists NAMES_FILENAME then
(new System.Net.WebClient()).DownloadFile(NAMES_URL, NAMES_FILENAME)
let names =
let fullLine = System.IO.File.ReadAllText NAMES_FILENAME
fullLine.[1 .. fullLine.Length - 2]
.Replace("\",\"", (string << char) 30)
.Split(char 30)
|> Array.append [| "" |] // first name to position 1 after sorting
|> Array.sort
let score number name =
let worth name =
Seq.map (fun ch -> (int ch) - (int 'A') + 1)
name
|> Seq.sum
number * (worth name)
let problem22 = Seq.mapi score names |> Seq.sum
let main = printfn "The answer is %d." (problem22)
main