{"id":482,"date":"2012-07-30T18:35:51","date_gmt":"2012-07-30T22:35:51","guid":{"rendered":"http:\/\/www.joshho.com\/blog\/?p=482"},"modified":"2012-09-07T16:15:28","modified_gmt":"2012-09-07T20:15:28","slug":"project-euler-problem-22","status":"publish","type":"post","link":"https:\/\/www.joshho.com\/blog\/2012\/07\/30\/project-euler-problem-22\/","title":{"rendered":"Project Euler &#8211; Problem 22"},"content":{"rendered":"<p>Problem 22: Using <a href=\"http:\/\/archiver.joshho.com\/display.php?full=1&#038;q=https:\/\/projecteuler.net\/project\/names.txt\" target=\"_blank\">names.txt<\/a>, 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.<\/p>\n<p>What is the total of all the name scores in the file?<br \/>\n<!--more--><br \/>\n<code><\/p>\n<pre lang=\"java\">\r\nimport java.util.ArrayList;\r\nimport java.util.Collections;\r\nimport java.util.List;\r\n\r\nclass runner\r\n{\r\n\tpublic static void main (String[] args) throws java.lang.Exception\r\n\t{\r\n\t\tlong time = System.currentTimeMillis();\r\n\t\tString[] names = {\"list of names from http:\/\/archiver.joshho.com\/display.php?full=1&q=https:\/\/projecteuler.net\/project\/names.txt\"};\r\n\t\t\r\n\t\tint sum = 0;\r\n\t\tList<String> list = new ArrayList<String>();\r\n\t\tfor(String name: names){\r\n\t\t\tlist.add(name);\r\n\t\t}\r\n\t\tCollections.sort(list);\r\n\t\tfor(int i = 0; i< list.size(); i++){\r\n\t\t\tString name = list.get(i);\r\n\t\t\tint score = 0;\r\n\t\t\tfor(int j = 0; j < name.length(); j++){\r\n\t\t\t\tscore += (name.charAt(j)-64);\r\n\t\t\t}\r\n\t\t\tsum += score * (i+1);\r\n\t\t}\r\n\t\t\r\n\t\tSystem.out.println(sum);\r\n\t\t\r\n\t\tSystem.out.println(\"time: \"+(System.currentTimeMillis() - time));\r\n\t}\r\n}\r\n<\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem 22: Using <a href=\"http:\/\/archiver.joshho.com\/display.php?full=1&#038;q=https:\/\/projecteuler.net\/project\/names.txt\" target=\"_blank\">names.txt<\/a>, 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.<\/p>\n<p>What is the total of all the name scores in the file?<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[56],"tags":[],"_links":{"self":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/482"}],"collection":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/comments?post=482"}],"version-history":[{"count":0,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joshho.com\/blog\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}