Skip to content

Commit 34f2cea

Browse files
committed
Simplify and clean up solution for Day 6
1 parent e20ca94 commit 34f2cea

File tree

1 file changed

+16
-28
lines changed

1 file changed

+16
-28
lines changed

src/test/java/com/macasaet/Day06.java

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,54 +4,42 @@
44
import java.util.ArrayList;
55
import java.util.HashSet;
66
import java.util.List;
7+
import java.util.Set;
78
import java.util.stream.Collectors;
89
import java.util.stream.StreamSupport;
910

1011
public class Day06 {
1112

1213
public static void main(final String[] args) throws IOException {
13-
try( var spliterator = new LineSpliterator( Day06.class.getResourceAsStream("/day-6-input.txt" ) ) ) {
14+
try (var spliterator = new LineSpliterator(Day06.class.getResourceAsStream("/day-6-input.txt"))) {
1415
final var rawLines = StreamSupport.stream(spliterator, false)
1516
.collect(Collectors.toUnmodifiableList());
1617
var currentGroup = new ArrayList<String>();
1718
// collect all the text blocks into entries (separated by empty lines)
1819
final var groups = new ArrayList<List<String>>();
1920
for (final var line : rawLines) {
2021
if (line.isBlank()) {
21-
if( currentGroup != null && !currentGroup.isEmpty() ) {
22-
groups.add( currentGroup );
22+
if (currentGroup != null && !currentGroup.isEmpty()) {
23+
groups.add(currentGroup);
2324
currentGroup = new ArrayList<>();
2425
}
2526
} else {
26-
currentGroup.add( line );
27+
currentGroup.add(line);
2728
}
2829
}
29-
if( currentGroup != null && !currentGroup.isEmpty() ) {
30-
groups.add( currentGroup );
30+
if (currentGroup != null && !currentGroup.isEmpty()) {
31+
groups.add(currentGroup);
3132
}
3233
final int sum = groups.stream().mapToInt(group -> {
33-
final var uniqueCharacters = new HashSet<Integer>();
34-
for( final var answers : group ) {
35-
answers.chars().forEach(answer -> {
36-
uniqueCharacters.add(answer);
37-
});
38-
}
39-
int total = 0;
40-
for( final int c : uniqueCharacters ) {
41-
int numResponses = 0;
42-
for( final var answers : group ) {
43-
for( char a : answers.toCharArray() ) {
44-
if( (int)a == c ) {
45-
numResponses++;
46-
break;
47-
}
48-
}
49-
if( numResponses == group.size() ) {
50-
total++;
51-
}
52-
}
53-
}
54-
return total;
34+
final var uniqueCharacters = group.stream()
35+
.flatMapToInt(String::chars)
36+
.collect(HashSet<Integer>::new, Set::add, Set::addAll);
37+
return (int) uniqueCharacters.stream()
38+
.filter(c -> (int) group.stream()
39+
.map(String::chars)
40+
.filter(chars -> chars.anyMatch(answer -> answer == c))
41+
.count() == group.size())
42+
.count();
5543
}).sum();
5644
System.out.println(sum);
5745
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy