-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.java
101 lines (88 loc) · 3.64 KB
/
Solution2.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
// --- Part Two ---
// As you finish the last group's customs declaration, you notice that you
// misread one word in the instructions:
// You don't need to identify the questions to which anyone answered "yes"; you
// need to identify the questions to which everyone answered "yes"!
// Using the same example as above:
// abc
// a
// b
// c
// ab
// ac
// a
// a
// a
// a
// b
// This list represents answers from five groups:
// In the first group, everyone (all 1 person) answered "yes" to 3 questions: a,
// b, and c.
// In the second group, there is no question to which everyone answered "yes".
// In the third group, everyone answered yes to only 1 question, a. Since some
// people did not answer "yes" to b or c, they don't count.
// In the fourth group, everyone answered yes to only 1 question, a.
// In the fifth group, everyone (all 1 person) answered "yes" to 1 question, b.
// In this example, the sum of these counts is 3 + 0 + 1 + 1 + 1 = 6.
// For each group, count the number of questions to which everyone answered
// "yes". What is the sum of those counts?
public class Solution2 {
public static void main(String[] args) {
final List<HashSet<Character>> input = getInput();
System.out.println(totalCount(input));
}
private static List<HashSet<Character>> getInput() {
// we use HashSet again to also control duplicate char values per line
final List<HashSet<Character>> input = new ArrayList<>();
HashSet<Character> choices = new HashSet<>();
try (BufferedReader br = new BufferedReader(new FileReader("input.txt"))) {
String line;
boolean control = true;
while ((line = br.readLine()) != null) {
if (!line.isEmpty()) {
if (control) { // control if 'choices' is empty because no matching characters left
if (choices.isEmpty())
for (int i = 0; i < line.length(); i++)
choices.add(line.charAt(i));
else {
HashSet<Character> temp = new HashSet<>();
for (int i = 0; i < line.length(); i++)
temp.add(line.charAt(i));
// this helper is to allow us to iterate over our choices and bypass
// ConcurrentModificationException
HashSet<Character> choicesHelper = new HashSet<>(choices);
choicesHelper.forEach(c -> {
if (!temp.contains(c))
choices.remove(c);
});
if (choices.isEmpty())
control = false; // we control 'choices' emptiness
}
}
} else {
if (!choices.isEmpty()) {
input.add(new HashSet<>(choices));
choices.clear();
}
control = true;
}
}
if (!choices.isEmpty()) // add values left after EOF
input.add(choices);
} catch (Exception e) {
System.out.println(e);
}
return input;
}
private static int totalCount(List<HashSet<Character>> input) {
int totalCount = 0;
for (HashSet<Character> choices : input)
totalCount += choices.size();
return totalCount;
}
}