-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day4b.java
60 lines (49 loc) · 2.18 KB
/
Day4b.java
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
package aoc22;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class Day4b {
private static String filename = "./src/aoc22/input4.txt";
// private static String filename = "./src/aoc22/example4.txt";
private List<Integer> firstStarts = new ArrayList<>();
private List<Integer> firstEnds = new ArrayList<>();
private List<Integer> secondStarts = new ArrayList<>();
private List<Integer> secondEnds = new ArrayList<>();
public Day4b(List<String> input) {
parse(input);
System.out
.println("Number of assignments contained within other assignment: " + calculateDuplicateAssignments());
}
private void parse(List<String> input) {
for (String line : input) {
int separatorIndex = line.indexOf(',');
String firstAssignment = line.substring(0, separatorIndex);
String secondAssignment = line.substring(separatorIndex + 1);
firstStarts.add(Integer.parseInt(firstAssignment.split("-")[0]));
firstEnds.add(Integer.parseInt(firstAssignment.split("-")[1]));
secondStarts.add(Integer.parseInt(secondAssignment.split("-")[0]));
secondEnds.add(Integer.parseInt(secondAssignment.split("-")[1]));
}
}
private int calculateDuplicateAssignments() {
int duplicateAssignments = 0;
for (int i = 0; i < firstStarts.size(); i++) {
int firstStart = firstStarts.get(i);
int firstEnd = firstEnds.get(i);
int secondStart = secondStarts.get(i);
int secondEnd = secondEnds.get(i);
if ((firstStart >= secondStart && firstStart <= secondEnd)
|| (firstEnd >= secondStart && firstEnd <= secondEnd)
|| (secondStart >= firstStart && secondEnd <= firstEnd)
|| (secondEnd >= firstStart && secondEnd <= firstEnd)) {
duplicateAssignments++;
}
}
return duplicateAssignments;
}
public static void main(String[] args) throws IOException {
new Day4b(Files.readAllLines(Path.of(filename)));
}
}