Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Created April 17, 2017 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuxXx/5766034885b5fe85e2eb1873cb77d10c to your computer and use it in GitHub Desktop.
Save LuxXx/5766034885b5fe85e2eb1873cb77d10c to your computer and use it in GitHub Desktop.
Project Euler - Problem 22
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