1 ! Copyright (c) 2007 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: ascii io.files kernel math project-euler.common sequences sorting splitting ;
6 ! http://projecteuler.net/index.php?section=problems&id=22
11 ! Using names.txt (right click and 'Save Link/Target As...'), a 46K text file
12 ! containing over five-thousand first names, begin by sorting it into
13 ! alphabetical order. Then working out the alphabetical value for each name,
14 ! multiply this value by its alphabetical position in the list to obtain a name
17 ! For example, when the list is sorted into alphabetical order, COLIN, which is
18 ! worth 3 + 15 + 12 + 9 + 14 = 53, is the 938th name in the list. So, COLIN
19 ! would obtain a score of 938 * 53 = 49714.
21 ! What is the total of all the name scores in the file?
29 : source-022 ( -- seq )
30 "extra/project-euler/022/names.txt" resource-path
31 file-contents [ quotable? ] subset "," split ;
33 : name-scores ( seq -- seq )
34 dup length [ 1+ swap alpha-value * ] 2map ;
38 : euler022 ( -- answer )
39 source-022 natural-sort name-scores sum ;
41 ! [ euler022 ] 100 ave-time
42 ! 123 ms run / 4 ms GC ave time - 100 trials