# Euler Solution 22

### From ProgSoc Wiki

# Solutions for Problem 22

Given a list of names (all upper case), sort them and then determine the sum of the name score for all the names. Where the name score is given by the sum of the values of the letters (A = 1, B = 2, etc.) multiplied by the position in the (sorted) list.

## Haskell by SanguineV

Runtime: 57.81ms

names = [... insert 46K of names here ...] {- Define the mapping of characters to values myself, because I can. -} charToVal :: Char -> Integer charToVal 'A' = 1 charToVal 'B' = 2 charToVal 'C' = 3 charToVal 'D' = 4 charToVal 'E' = 5 charToVal 'F' = 6 charToVal 'G' = 7 charToVal 'H' = 8 charToVal 'I' = 9 charToVal 'J' = 10 charToVal 'K' = 11 charToVal 'L' = 12 charToVal 'M' = 13 charToVal 'N' = 14 charToVal 'O' = 15 charToVal 'P' = 16 charToVal 'Q' = 17 charToVal 'R' = 18 charToVal 'S' = 19 charToVal 'T' = 20 charToVal 'U' = 21 charToVal 'V' = 22 charToVal 'W' = 23 charToVal 'X' = 24 charToVal 'Y' = 25 charToVal 'Z' = 26 charToVal x = 0 {- Haskell has a nice feature that a string is a list of characters, so all you need to do - to solve the problem is do the sum of the zip of the natural numbers with the sum of the - map of charToVal of the sorted names! -} main = print (sum (zipWith (\nat nam -> nat * (sum (map charToVal nam))) [1..] (sort names)))

## Python by Althalus

Runtime: 5 seconds (On my laptop, which is right now badly overheating, and running everything 2 times+ as slow as normal - Took 3 seconds just to work out how much time had passed after displaying the actual result...)

import time start_time = time.time() def namescore(name): score = 0 scores = {'A':1, 'B':2, 'C':3, 'D':4, 'E':5, 'F':6, 'G':7, 'H':8, 'I':9, 'J':10, 'K':11, 'L':12, 'M':13, 'N':14, 'O':15, 'P':16, 'Q':17, 'R':18, 'S':19, 'T':20, 'U':21, 'V':22, 'W':23, 'X':24, 'Y':25, 'Z':26,} for c in name: score += scores[c] return score f = open("names.txt") contents = str(f.read()).split(",") contents.sort() total = 0 i=1 for c in contents: total += namescore(c[1:-1])*i i+=1 print(total) run_time = time.time() - start_time print (run_time)