Refactor output

This commit is contained in:
David Doblas Jiménez 2023-08-28 18:37:27 +02:00
parent 768a3278dd
commit 1209566e4a

View File

@ -4,13 +4,15 @@ 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 =#
Solution for Problem 022 of Project Euler
https://projecteuler.net/problem=22
=#
using BenchmarkTools
using DelimitedFiles
using MethodAnalysis
function Problem22()
function Problem022()
#=
Using names.txt, a 46K text file containing over five-thousand first names,
begin by sorting it into alphabetical order. Then working out the
@ -21,19 +23,23 @@ function Problem22()
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"
What is the total of all the name scores in the file?
=#
file = "files/Problem22.txt"
names = sort(readdlm(file, ',', String)[:])
result = 0
ans::Int64 = 0
for (idx, name) in enumerate(names)
result += sum(Int(c) - 64 for c in name) * idx
ans += sum(Int(c) - 64 for c in name) * idx
end
return result
return ans
end
println("Time to evaluate Problem $(lpad(22, 3, "0")):")
@btime Problem22()
println("Took:")
@btime Problem022()
println("")
println("Result for Problem $(lpad(22, 3, "0")): ", Problem22())
println("Result for Problem $(lpad(22, 3, "0")): ", Problem022())
# @code_warntype debuginfo=:source Problem022()