-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25_1.groovy
72 lines (64 loc) · 1.11 KB
/
day25_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
63
64
65
66
67
68
69
70
71
72
def sampleInput = '''#####
.####
.####
.####
.#.#.
.#...
.....
#####
##.##
.#.##
...##
...#.
...#.
.....
.....
#....
#....
#...#
#.#.#
#.###
#####
.....
.....
#.#..
###..
###.#
###.#
#####
.....
.....
.....
#....
#.#..
#.#.#
#####'''
static def parseKeyOrLock(List<String> input) {
assert input.size() == 7
assert input.every { it.size() == 5 }
boolean isLock = input[0].every { it == '#' }
def sizes = (0..<5).collect { i ->
(0..<7).count { input[it][i] == '#' } - 1
}
[isLock, sizes]
}
static boolean matches(List lock, List key) {
assert lock.size() == key.size()
lock.withIndex().every { l, i ->
key[i] + l <= 5
}
}
static long solve(String input) {
def locksAndKeys = input.split('\n\n').collect {
parseKeyOrLock(it.split('\n') as List)
}
def locks = locksAndKeys.findAll { it[0] }.collect { it[1] }
def keys = locksAndKeys.findAll { !it[0] }.collect { it[1] }
locks.collect { l ->
keys.count { k ->
matches(l, k)
}
}.sum() as long
}
assert 3 == solve(sampleInput)
println solve(new File('input/day25.txt').text)