-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday5.1.java
55 lines (52 loc) · 1.86 KB
/
day5.1.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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
class Day5 {
public static void main(String[] args) throws FileNotFoundException {
File txt = new File("input5.2.txt");
var order = new HashMap<Integer, Set<Integer>>();
var sum = 0;
try (Scanner scan = new Scanner(txt)) {
while (scan.hasNextLine()) {
var line = scan.nextLine();
if (line.contains("|")) {
var parts = line.split("\\|");
int n1 = Integer.parseInt(parts[0]),
n2 = Integer.parseInt(parts[1]);
System.out.println(n1 + " " + n2);
var orderSet = order.get(n1);
if (orderSet == null) {
orderSet = new HashSet<Integer>();
order.put(n1, orderSet);
}
orderSet.add(n2);
} else if (line.isEmpty()) {
continue;
} else {
var parts = line.split(",");
var l = new ArrayList<Integer>();
for (var part : parts) {
l.add(Integer.parseInt(part));
}
var correct = true;
for (int i = 1;i < l.size();i++) {
if (order.get(l.get(i)) != null && order.get(l.get(i)).contains(l.get(i-1))) {
correct = false;
break;
}
}
if (correct) {
sum += l.get(l.size() / 2);
}
}
}
}
System.out.println(sum);
}
}
/*
*/