40 lines
1.1 KiB
Julia
40 lines
1.1 KiB
Julia
#=
|
||
Created on 08 Aug 2021
|
||
|
||
@author: David Doblas Jiménez
|
||
@email: daviddoji@pm.me
|
||
|
||
Solution for Problem 22 of Project Euler
|
||
https://projecteuler.net/problem=22 =#
|
||
|
||
using BenchmarkTools
|
||
using DelimitedFiles
|
||
|
||
function Problem22()
|
||
#=
|
||
Using names.txt, a 46K text file containing over five-thousand first names,
|
||
begin by sorting it into alphabetical order. Then working out the
|
||
alphabetical value for each name, multiply this value by its alphabetical
|
||
position in the list to obtain a name score.
|
||
|
||
For example, when the list is sorted into alphabetical order, COLIN, which
|
||
is worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So,
|
||
COLIN would obtain a score of 938 × 53 = 49714.
|
||
|
||
What is the total of all the name scores in the file? =#
|
||
file = "../files/Problem22.txt"
|
||
names = sort(readdlm(file, ',', String)[:])
|
||
|
||
result = 0
|
||
for (idx, name) in enumerate(names)
|
||
result += sum(Int(c) - 64 for c in name) * idx
|
||
end
|
||
return result
|
||
end
|
||
|
||
|
||
println("Time to evaluate Problem $(lpad(22, 3, "0")):")
|
||
@btime Problem22()
|
||
println("")
|
||
println("Result for Problem $(lpad(22, 3, "0")): ", Problem22())
|