Skip to content

Project Euler Problem 22

November 16, 2011

Project Euler Problem 22 solution is straightforward. The only auxiliary function needed is alphavalue that for any string calculates its alphabetic value. Names are stripped from whitespace, sorted, products of alphabetic value by position are calculated and finally added together:

open System.IO

let alphavalue (s: string) =
    s.ToCharArray() |> Array.map (fun x -> int x - int 'A' + 1) |> Array.sum

let readData (path: string) =
    use sr = new StreamReader(path)
    sr.ReadLine().Split(',')

let problem022 () =
    readData @"..\..\..\Datafiles\Problem022.data"
    |> Array.map (fun x -> x.Replace("\"",""))
    |> Array.sort
    |> Array.mapi (fun i x -> int64((alphavalue x) * (i + 1)))
    |> Array.sum
Advertisements

From → Project Euler

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: