-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution22.java
More file actions
39 lines (37 loc) · 1.33 KB
/
Copy pathSolution22.java
File metadata and controls
39 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.io.*;
import java.util.*;
public class Solution22{
public static void main(String[] args) {
BufferedReader reader;
try{
reader = new BufferedReader(new FileReader("Input22.txt"));
//Utilizing log power rule for much faster computations, log_{b}(x^y) = y log_{b}(x).
String line = reader.readLine();
ArrayList<String> names = new ArrayList<String>();
while(line != null){
//System.out.println(line);
String contents[] = line.split(",");
for(String str : contents){
names.add(str);
}
line = reader.readLine();
}
Collections.sort(names);
int total = 0;
for(int i = 0; i < names.size(); i++){
int alphVal = 0;
for(Character ch: names.get(i).toCharArray()){
if(Character.isLetter(ch)){
//Get position of letter in alphabet.
int alphChar = ch - 'A' + 1;
alphVal += alphChar;
}
}
total += alphVal * (i + 1);
}
System.out.println(total);
}catch(IOException e){
e.printStackTrace();
}
}
}