Returns the total of all the name scores in the file. >>> solution() 871198282
()
| 19 | |
| 20 | |
| 21 | def solution(): |
| 22 | """Returns the total of all the name scores in the file. |
| 23 | |
| 24 | >>> solution() |
| 25 | 871198282 |
| 26 | """ |
| 27 | with open(os.path.dirname(__file__) + "/p022_names.txt") as file: |
| 28 | names = str(file.readlines()[0]) |
| 29 | names = names.replace('"', "").split(",") |
| 30 | |
| 31 | names.sort() |
| 32 | |
| 33 | name_score = 0 |
| 34 | total_score = 0 |
| 35 | |
| 36 | for i, name in enumerate(names): |
| 37 | for letter in name: |
| 38 | name_score += ord(letter) - 64 |
| 39 | |
| 40 | total_score += (i + 1) * name_score |
| 41 | name_score = 0 |
| 42 | return total_score |
| 43 | |
| 44 | |
| 45 | if __name__ == "__main__": |