-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution42.java
More file actions
35 lines (34 loc) · 1.29 KB
/
Copy pathSolution42.java
File metadata and controls
35 lines (34 loc) · 1.29 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
import java.io.*;
public class Solution42{
public static void main(String[] args) {
BufferedReader reader;
try{
reader = new BufferedReader(new FileReader("Input42.txt"));
//Utilizing log power rule for much faster computations, log_{b}(x^y) = y log_{b}(x).
String line = reader.readLine();
int triangleCount = 0;
while(line != null){
//System.out.println(line);
String contents[] = line.split(",");
for(String str : contents){
int value = 0;
for(Character ch: str.toCharArray()){
if(Character.isLetter(ch)){
//Get position of letter in alphabet.
int alphChar = ch - 'A' + 1;
value += alphChar;
}
}
double triangleVal = (Math.sqrt(1 + (8 * value)) - 1)/2.0;
if(triangleVal == (int)triangleVal){
triangleCount++;
}
}
line = reader.readLine();
}
System.out.println(triangleCount);
}catch(IOException e){
e.printStackTrace();
}
}
}