2022-08-18 10:47:29 +02:00

40 lines
1.1 KiB
Julia
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#=
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())