-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4p2.txt
More file actions
52 lines (44 loc) · 1.6 KB
/
4p2.txt
File metadata and controls
52 lines (44 loc) · 1.6 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
public class Part2 {
private static int valid = 0;
public static void main(String[] args) {
try (Stream<String> stream = Files.lines(Paths.get("E:\\AdventOfCode\\Day 4\\input.txt"))) {
stream.forEach(string -> {
if (isValid(string)) {
valid++;
System.out.println("Valid: " + string);
} else {
System.out.println("Invalid: " + string);
}
});
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Result:\n " + valid);
}
private static boolean isValid(String word) {
List<String> parts = new ArrayList<>();
for (String part : word.split(" ")) {
if (!containsAnagram(part, parts)) {
parts.add(part);
} else {
return false;
}
}
return true;
}
private static boolean containsAnagram(String checkingWord, List<String> words) {
for (String word : words) {
if (isAnagram(checkingWord, word)) {
return true;
}
}
return false;
}
private static boolean isAnagram(String one, String two) {
List<Character> chars1 = new ArrayList<>();
List<Character> chars2 = new ArrayList<>();
Arrays.stream(one.split("")).forEachOrdered(pcha -> chars1.add(pcha.charAt(0)));
Arrays.stream(two.split("")).forEachOrdered(pcha -> chars2.add(pcha.charAt(0)));
return chars1.containsAll(chars2) && chars2.containsAll(chars1);
}
}