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 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?
This one is very straight-forward. Parse the file, sort it, and calculate the scores.
We'll start off with a simple function that loads in a file, splitting on commas and stripping off quotes:
(defn load-names [filename] ; The data is stored as a comma-separated list of strings ; with double-quotes. (->> (clojure.string/split (slurp filename) #",") (map #(clojure.string/replace % #"\"" ""))))We'll then define a function that calculates the score for a name. Since A is 1, B is 2, etc. we can just use the ASCII value of the character, and subtract 64 (as A is ASCII 65):
(defn calculate-score [name] (->> (upper-case name) (map int) (map #(- % 64)) (reduce +)))Now that we have everything, it's a simple pass over all the names in the list to get the scores:
(defn total-scores [filename] (->> (load-names filename) (sort) ; Get the score for the name (map calculate-score) ; Scale by the index of the name. Note that map-indexed ; is zero-based, so we need to add one. (map-indexed (fn [idx value] (* (+ 1 idx) value))) (reduce +)))This runs very fast:
$ lein run Processing... Total scores: 871198282 "Elapsed time: 31.831402 msecs"
No comments:
Post a Comment