Created
April 17, 2017 20:56
-
-
Save LuxXx/5766034885b5fe85e2eb1873cb77d10c to your computer and use it in GitHub Desktop.
Project Euler - Problem 22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package euler; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.util.Arrays; | |
public class Names { | |
public static void main(String[] args) throws Throwable { | |
BufferedReader br = new BufferedReader(new FileReader(new File("p022_names.txt"))); | |
String[] arr = br.readLine().split("\",\""); | |
arr[0] = arr[0].substring(1, arr[0].length()); | |
arr[arr.length - 1] = arr[arr.length - 1].substring(0, arr[arr.length - 1].length() - 1); | |
br.close(); | |
Arrays.sort(arr); | |
int sum = 0; | |
for (int i = 0; i < arr.length; i++) { | |
sum += (i+1)*score(arr[i]); | |
} | |
System.out.println(sum); | |
} | |
public static int score(String s) { | |
int score = 0; | |
for (int i = 0; i < s.length(); i++) { | |
score += s.charAt(i) - 64; | |
} | |
return score; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment