-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day6.java
68 lines (52 loc) · 1.48 KB
/
Day6.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
package io.github.sirnikolai.days;
import io.github.sirnikolai.AbstractSolution;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Day6 extends AbstractSolution {
@Override
public void Part1(String input) {
String[] split = input.split("\n\n");
int count = 0;
for(String s : split) {
count += postProcess(s).size();
}
System.out.println(count);
}
@Override
public void Part2(String input) {
String[] split = input.split("\n\n");
int count = 0;
for(String s : split) {
count += postProcess2(s);
}
System.out.println(count);
}
private Set<Character> postProcess(String s) {
String[] arr = s.split("\n");
Set<Character> set = new HashSet<>();
for(String s2 : arr) {
for(char c : s2.toCharArray()) {
set.add(c);
}
}
return set;
}
private int postProcess2(String s) {
String[] arr = s.split("\n");
Map<Character, Integer> set = new HashMap<>();
for(String s2 : arr) {
for(char c : s2.toCharArray()) {
set.put(c, set.getOrDefault(c, 0) + 1);
}
}
int count = 0;
for(Map.Entry<Character, Integer> e : set.entrySet()) {
if(e.getValue() == arr.length) {
count++;
}
}
return count;
}
}