-
Notifications
You must be signed in to change notification settings - Fork 0
/
day19_1.groovy
39 lines (32 loc) · 857 Bytes
/
day19_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
import groovy.transform.Memoized
def sampleInput = '''r, wr, b, g, bwu, rb, gb, br
brwrr
bggr
gbbr
rrbgbr
ubwu
bwurrg
brgr
bbrgwb'''
static def parse(List<String> input) {
Set<String> ribbons = input[0].split(', ')
List<String> patterns = input.drop(2)
[ribbons, patterns]
}
@Memoized
static boolean isPossible(String pattern, Set<String> ribbons) {
if (ribbons.contains(pattern)) return true
(1..(pattern.size()-1)).any {
def part1 = pattern.substring(0, it)
def part2 = pattern.substring(it)
ribbons.contains(part1) && isPossible(part2, ribbons)
}
}
static long solve(List<String> input) {
def (ribbons, patterns) = parse(input)
patterns.count {
isPossible(it, ribbons)
}
}
assert 6L == solve(sampleInput.split('\n') as List)
println solve(new File('input/day19.txt').readLines())