-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23_2.groovy
39 lines (34 loc) · 1.08 KB
/
day23_2.groovy
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
import groovy.transform.Memoized
def sampleInput = '''ka-co
ta-co
de-co
ta-ka
de-ta
ka-de'''
@Memoized
static def largestClique(def input, Set current, Set subsetConsidered) {
def remaining = subsetConsidered - current
remaining.findAll { A -> current.every { input[it].contains(A) }}.collect { A ->
largestClique(input, current + A, subsetConsidered)
}.max { it.size() } ?: current
}
static def largestClique(def input) {
def subsetConsidered = input.keySet() as Set
input.keySet().withIndex().collect { it, i ->
def largest = largestClique(input, [it] as Set, subsetConsidered.intersect(input[it]))
subsetConsidered -= it
largest
}.max { it.size() }
}
static String solve(List<String> input) {
def lookup = [:].withDefault { [] as Set }
input.each { line ->
def (a, b) = line.split('-')
lookup[a] << b
lookup[b] << a
}
println lookup.size()
largestClique(lookup).sort().join(',')
}
assert 'co,de,ka,ta' == solve(sampleInput.split('\n') as List)
println solve(new File('input/day23.txt').readLines())