Solution to problem 22 in Julia
This commit is contained in:
parent
8b11f977bb
commit
90e6ce9490
40
src/Julia/Problem022.jl
Normal file
40
src/Julia/Problem022.jl
Normal file
@ -0,0 +1,40 @@
|
||||
#=
|
||||
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 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 = "/datos/Scripts/Gitea/Project_Euler/src/files/Problem22.txt"
|
||||
names = readdlm(file, ',', String)
|
||||
|
||||
result = 0
|
||||
for (idx, name) in enumerate(sort(names[1:end]))
|
||||
result += sum(Int(c) - 64 for c in name) * idx
|
||||
end
|
||||
return result
|
||||
end
|
||||
|
||||
|
||||
println("Time to evaluate Problem 22:")
|
||||
@time Problem22()
|
||||
println("")
|
||||
println("Result for Problem 22: ", Problem22())
|
Loading…
x
Reference in New Issue
Block a user