
Project Euler 22: Names Scores

Peter Prevos |
377 words | 2 minutes
Share this content
Project Euler 22 is another trivial task that takes us to the realm of ASCII codes. ASCII is a method to convert symbols into numbers, initially invented for telegraphs.
Back in the 8-bit days, ASCII art was a method to create images without using lots of memory. Each image consists of a collection of text characters that give the illusion of an image.
,-""""""-. /\j__/\ ( \`--. hjw \`@_@'/ _) >--.`. _{.:Y:_}_{{_,' ) ) {_}`-^{_} ``` (_/ .
Euler problem 22 is, unfortunately, a bit less poetic. This problem is similar to Euler problem 17 that asks to count the length of words expressing numbers.
Project Euler 22 Definition
Using names.txt
, a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetic 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?
Proposed Solution
This code reads and cleans the file and sorts the names alphabetically. The charToRaw function determines the numerical value of each character in each name. This output of this function is the hex ASCII code for each character. The letter A is number 65, so we subtract 64 from each value to get the sum total.
## Project Euler 22: Names Scores
names <- readLines("https://projecteuler.net/project/resources/p022_names.txt",
warn =FALSE)
names <- unlist(strsplit(names, ","))
names <- gsub("[[:punct:]]", "", names)
names <- sort(names)
# Total Name scores
answer <- 0
for (i in names) {
value <- sum(sapply(unlist(strsplit(i, "")),
function(x) as.numeric(charToRaw(x)) - 64))
value <- value * which(names==i)
answer <- answer + value
}
print(answer)
Modern computers in a globalised world don't use ASCII because it cannot encode the tens of thousands of characters that are needed for the world's languages. The video below explains the Unicode system to encode the world's symbols.
Share this content