-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8_1.groovy
58 lines (53 loc) · 1.41 KB
/
day8_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
def sampleInput = '''............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............'''
static def read(List<String> input) {
int n = input.size()
int m = input[0]?.size() ?: 0
def antennas = [:].withDefault { [] }
assert input.every { it.size() == m }
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
def element = input[i][j]
if (element == '.') continue
antennas[element] << [i, j]
}
}
[n, m, antennas]
}
static def getAntinodes(def posA, def posB) {
[
[2*posA[0]-posB[0], 2*posA[1]-posB[1]],
[2*posB[0]-posA[0], 2*posB[1]-posA[1]]
]
}
static def getAntinodesInMap(int n, int m, def posA, def posB) {
getAntinodes(posA, posB).findAll {
it[0] >= 0 && it[0] < n &&
it[1] >= 0 && it[1] < m
}
}
static long solve(List<String> input) {
def antinodes = [] as Set
def (n, m, antennas) = read(input)
antennas.each { type, positions ->
int l = positions.size()
for (int i = 0; i < l; i++) {
for (int j = i+1; j < l; j++) {
antinodes.addAll(getAntinodesInMap(n, m, positions[i], positions[j]))
}
}
}
antinodes.size()
}
assert 14L == solve(sampleInput.split('\n') as List)
println solve(new File('input/day8.txt').readLines())