-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAoC232024.java
86 lines (77 loc) · 2.56 KB
/
AoC232024.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
package com.adventofcode.aoc2024;
import static com.adventofcode.utils.Utils.itoa;
import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.joining;
import com.adventofcode.Solution;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class AoC232024 implements Solution {
@Override
public String solveFirstPart(final Stream<String> input) {
return solve( input, true );
}
@Override
public String solveSecondPart(final Stream<String> input) {
return solve( input, false );
}
private String solve(final Stream<String> input, final boolean first) {
Set<String> computers = new HashSet<>();
Multimap<String, String> connections = HashMultimap.create();
input.forEach( pair -> {
String a = pair.substring( 0, 2 );
String b = pair.substring( 3, 5 );
computers.add( a );
computers.add( b );
if ( a.compareTo( b ) < 0 ) {
connections.put( a, b );
} else {
connections.put( b, a );
}
} );
if ( first ) {
return itoa( count3TGroup( computers, connections ) );
} else {
return computers.stream()
.parallel()
.map( start -> getLargestClique( start, connections ) )
.max( comparingInt( Set::size ) ).orElseThrow()
.stream()
.sorted()
.collect( joining( "," ) );
}
}
private long count3TGroup(final Set<String> computers,
final Multimap<String, String> connections) {
long result = 0;
for ( final String a : computers ) {
for ( final String b : connections.get( a ) ) {
for ( final String c : connections.get( b ) ) {
if ( connections.containsEntry( a, c )
&& (a.startsWith( "t" ) || b.startsWith( "t" ) || c.startsWith( "t" )) ) {
result++;
}
}
}
}
return result;
}
private Set<String> getLargestClique(final String start,
final Multimap<String, String> connections) {
Set<String> largestClique = new HashSet<>();
for ( final String next : connections.get( start ) ) {
Set<String> largeClique = getLargestClique( next, connections )
.stream()
.filter( n -> connections.containsEntry( start, n ) )
.collect( Collectors.toSet() );
if ( largeClique.size() > largestClique.size() ) {
largestClique = largeClique;
}
}
largestClique.add( start );
return largestClique;
}
}