Problem 22
module Problem_0022
open System.IO
let read () =
let path = "names.txt"
seq {
use sr = new StreamReader(path)
while not sr.EndOfStream do
yield sr.Read()
yield int ','
}
//871198282
//390ms
let calc () =
read()
|> Seq.fold (fun (l,s,a) n ->
if int '"' = n then (l,s,a)
elif int ',' = n then (l@[(s,a)],"",0)
else
let c = string (char n)
let s = s + c
let a = a + (n-64)
(l,s,a)
) ([],"",0)
|> (fun (l,_,_) -> l)
|> Seq.sortBy (fst)
|> Seq.mapi (fun i (s,a) -> (s,a*(i+1)))
|> Seq.sumBy (snd)
let run() =
calc ()