/* Euler #22 in Picat. """ 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 Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. main => go. go => euler22. % , euler22b. euler22 => Names = split(read_file_chars("names.txt"),","), A = 1, Sum = 0, foreach(Name in Names.sort()) CodeSum = sum(to_code([C : C in Name, C != '"'])), % " Sum := Sum + A*CodeSum, A := A + 1 end, writeln(Sum). % tighter euler22b => Names = split(read_file_chars("euler22_names.txt"),",").sort(), sum([A*sum(to_code([C:C in Name,C!='"'])):{Name,A}in zip(Names,1..Names.length)]).writeln(). % convert a string to position code % A=1, B=2, etc to_code(S) = [C.ord()-64 : C in S].