-
Notifications
You must be signed in to change notification settings - Fork 0
/
day23_1.groovy
62 lines (60 loc) · 1.19 KB
/
day23_1.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
def sampleInput = '''kh-tc
qp-kh
de-cg
ka-co
yn-aq
qp-ub
cg-tb
vc-aq
tb-ka
wh-tc
yn-cg
kh-ub
ta-co
de-co
tc-td
tb-wq
wh-td
ta-ka
td-qp
aq-cg
wq-ub
ub-vc
de-ta
wq-aq
wq-vc
wh-yn
ka-de
kh-ta
co-tc
wh-qp
tb-vc
td-yn'''
static long solve(List<String> input) {
def lookup = [:].withDefault { [] as Set }
input.each { line ->
def (a, b) = line.split('-')
lookup[a] << b
lookup[b] << a
}
def setsOfThreeWithT = [] as Set
lookup.findAll { it.key.startsWith('t') }.each { A, Bs ->
Bs.findAll { it != A }.each { B ->
// compare the smaller of
def CsFromB = lookup[B]
if(CsFromB.size() < Bs.size()-1) {
CsFromB.findAll { it != A && it != B }.each{ C ->
setsOfThreeWithT << ([A, B, C] as Set)
}
} else {
Bs.findAll { it != A && it != B && CsFromB.contains(it) }.each { C ->
setsOfThreeWithT << ([A, B, C] as Set)
}
}
}
}
setsOfThreeWithT.each { println it }
setsOfThreeWithT.size()
}
assert 7L == solve(sampleInput.split('\n') as List)
println solve(new File('input/day23.txt').readLines())