Table of Contents
Problem
Using names.txt (right click and 'Save Link/Target As...'), 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 $938$th name in the list. So, COLIN would obtain a score of $938 \times 53 = 49714$.
What is the total of all the name scores in the file?
Parsing
If you copy the contents of the file and prepend it with a $
it is interpreted as a string that looks something like this:
$"MARY","PATRICIA","LINDA","BARBARA", ...
We are not going to be applying a score to the double quotes, so let's remove them.
$"MARY","PATRICIA","LINDA","BARBARA", ...
▽ ⊸≠@"
"MARY,PATRICIA,LINDA,BARBARA,..."
Then, let's split the string by comma, @, with the pbbn
alias to get an array of boxed names.
$"MARY","PATRICIA","LINDA","BARBARA", ...
▽⊸≠@"
⊜□⊸≠@,
["MARY"│"PATRICIA"│"LINDA"│"BARBARA"|...]
Let's sort and store the array in a variable Input.
Input ← ⍆ ⊜□⊸≠@, ▽ ⊸≠ @" $"MARY","PATRICIA",...
["AARON"│"ABBEY"│"ABBIE"│"ABBY"|...]Calculating a name
We can subtract a character from a string to get the string's character differences to it. Since the characters are in sequence, in the ASCII table, this is sufficient and no shuffling is needed.
We need a corrective character to subtract from a name, to get
the name's characters' distances from it.
A should be $1$ away, B should be $2$ and so on.
Let's find this character. We'll subtract $1$ from A in order to do so.
-1 @A
@@
OK, great. Then subtracting @@ should give us the alphabetical
value. For COLIN, as an example, we want $3 + 15 + 12 + 9 +14 = 53.$
- @@ "COLIN"
[3 15 12 9 14]
Then the alphabetical calculation is the summing up of the character values.
/+ -@@ "COLIN"
53
Let's store this as function F.
F ← /+ -@@
F "BARBARA"
43Unboxing the contents
To apply F to each value in our array of boxes we must unbox its contents.
We can do the following.
≡(F°□) Input
[49 35 19 30 ...]
We can clean that up by using the content modifier which unboxes arguments before
they are passed to a function, replacing un box.
≡◇F Input
[49 35 19 30 ...]Solution
We need to apply increasing multipliers to the sorted alphabetical values. Let's create the range of multipliers, starting at $1$, as long as the list of values.
⇡₁ ⊸⧻ ≡◇F Input
[49 35 19 30 40 ...]
[1 2 3 4 5 ...]
We multiply these arrays together and then reduce them for the final answer.
/+ × ⇡₁ ⊸⧻ ≡◇F Input
871198282