-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8_2.groovy
69 lines (63 loc) · 1.76 KB
/
day8_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
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
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 inMap(int n, int m, def pos) {
pos[0] >= 0 && pos[0] < n &&
pos[1] >= 0 && pos[1] < m
}
static def getAntinodesPolarizedInMap(int n, int m, def posA, def posB) {
def antinodes = [] as Set
int k = 1
while (true) {
def antinode = [posA[0] + k * (posA[0] - posB[0]), posA[1] + k * (posA[1] - posB[1])]
if (!inMap(n, m, antinode)) {
break
}
antinodes << antinode
k++
}
antinodes
}
static def getAntinodesInMap(int n, int m, def posA, def posB) {
getAntinodesPolarizedInMap(n, m, posA, posB) +
getAntinodesPolarizedInMap(n, m, posB, posA)
}
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.addAll(antennas.values().collectMany { it })
antinodes.size()
}
assert 34L == solve(sampleInput.split('\n') as List)
println solve(new File('input/day8.txt').readLines())