Skip to content

Commit a08d627

Browse files
committed
22
1 parent 083c1af commit a08d627

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

022.clj

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
; Problem 22: Names scores
2+
; 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.
3+
4+
; 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.
5+
6+
; What is the total of all the name scores in the file?
7+
8+
(use '[clojure.string :only [replace split]])
9+
10+
(def names (map #(replace % #"[\\\"]" "") (split (slurp "./names.txt") #",")))
11+
12+
(defn alpha-value [word]
13+
(reduce (fn [sum letter]
14+
(+ sum (- (int letter) 64))) 0 word))
15+
16+
(->>
17+
(map #(list %1 (inc %2)) (sort names) (range))
18+
(reduce (fn [total [word, position]]
19+
(+ total
20+
(* position
21+
(alpha-value word))))
22+
0)
23+
); 871198282

0 commit comments

Comments
 (0)