-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem042.java
More file actions
30 lines (30 loc) · 1.01 KB
/
Problem042.java
File metadata and controls
30 lines (30 loc) · 1.01 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Problem042 {
private static String letters;
private static ArrayList<Integer> triangles;
private static boolean isTriangle(String s) {
int total = 0;
for (String letter : s.split("")) {
total += letters.indexOf(letter) + 1;
}
return triangles.contains(total);
}
public static void main(String[] args) throws FileNotFoundException {
letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
triangles = new ArrayList<Integer>();
for (int i = 0; i < 30; i++) {
triangles.add((i + 1) * (i + 2) / 2);
}
int total = 0;
Scanner fileScan = new Scanner(new File("p042_words.txt"));
while (fileScan.hasNext()) {
for (String s : fileScan.next().split(",")) {
total += (isTriangle(s.replace("\"", ""))) ? 1 : 0;
}
}
System.out.println(total);
}
}