-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPart2.java
More file actions
81 lines (64 loc) · 2.37 KB
/
Copy pathPart2.java
File metadata and controls
81 lines (64 loc) · 2.37 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package y2016.d03;
import java.io.File;
import java.util.*;
public class Part2 {
private static class Triangle {
private int shortest;
private int middle;
private int longest;
public Triangle(int[] sides) {
this.assignValuesSorted(sides);
}
public Triangle(String[] sides) {
int[] newSides = {
Integer.parseInt(sides[0]),
Integer.parseInt(sides[1]),
Integer.parseInt(sides[2]),
};
this.assignValuesSorted(newSides);
}
private void assignValuesSorted (int[] sides) {
Arrays.sort(sides);
this.shortest = sides[0];
this.middle = sides[1];
this.longest = sides[2];
}
public boolean couldBeTriangle() {
return (this.shortest + this.middle) > this.longest;
}
}
public static void main(String[] args) {
File file = new File("src/main/java/y2016/d03/in.txt");
try {
Scanner scanner = new Scanner(file);
String line = "";
List<Integer> values = new LinkedList<>();
while (scanner.hasNextLine()) {
line = scanner.nextLine();
String[] split = line.trim().split("\\s+");
for (String s: split) {
values.add(Integer.parseInt(s));
}
}
// iterate in steps of 9 and use offset to 3x check.
int count = 0;
for (int i = 0; i < values.size(); i += 9) {
Triangle triangle1 = new Triangle(new int[]{values.get(i ), values.get(i + 3), values.get(i + 6)});
Triangle triangle2 = new Triangle(new int[]{values.get(i + 1), values.get(i + 4), values.get(i + 7)});
Triangle triangle3 = new Triangle(new int[]{values.get(i + 2), values.get(i + 5), values.get(i + 8)});
if (triangle1.couldBeTriangle()) {
++count;
}
if (triangle2.couldBeTriangle()) {
++count;
}
if (triangle3.couldBeTriangle()) {
++count;
}
}
System.out.println("Count: " + count);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}