-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday08.java
80 lines (72 loc) · 2.69 KB
/
day08.java
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
73
74
75
76
77
78
79
80
import java.util.*;
import static java.lang.System.in;
import static java.lang.System.out;
public class day08 {
public static void main(String[] args) {
var antennas = new HashMap<Character, List<Pos>>();
var row = new int[]{0};
var cols = 0;
var scanner = new Scanner(in);
while (scanner.hasNextLine()) {
var line = scanner.nextLine();
for (int col = 0; col < line.length(); col++) {
if (line.charAt(col) == '.') { continue; }
int fcol = col;
antennas.compute(line.charAt(col), (a, p) -> {
if (p == null) { p = new ArrayList<>(); }
p.add(new Pos(row[0], fcol));
return p;
});
}
cols = line.length();
row[0]++;
}
out.printf("%d %d%n",
new Antinodes(row[0], cols, antennas).count(1),
new Antinodes(row[0], cols, antennas).countAll()
);
}
static class Antinodes {
final int rows, cols;
final HashMap<Character, List<Pos>> antennas;
final Set<Pos> antinodes = new HashSet<>();
Antinodes(int rows, int cols, HashMap<Character, List<Pos>> antennas) {
this.rows = rows;
this.cols = cols;
this.antennas = antennas;
}
int count(int distance) {
var prev = antinodes.size();
for (var an : antennas.values()) {
for (int i = 0; i < an.size() - 1; i++) {
for (int j = i + 1; j < an.size(); j++) {
var a1 = new Pos(
an.get(i).row - distance * (an.get(j).row - an.get(i).row),
an.get(i).col - distance * (an.get(j).col - an.get(i).col)
);
var a2 = new Pos(
an.get(j).row - distance * (an.get(i).row - an.get(j).row),
an.get(j).col - distance * (an.get(i).col - an.get(j).col)
);
if (inside(a1)) antinodes.add(a1);
if (inside(a2)) antinodes.add(a2);
}
}
}
return antinodes.size() - prev;
}
int countAll() {
var result = 0;
for (int d = 0; d < rows; d++) {
var step = count(d);
if (step == 0) break;
result += step;
}
return result;
}
boolean inside(Pos p) {
return p.row >= 0 && p.row < rows && p.col >= 0 && p.col < cols;
}
}
record Pos (int row, int col) {}
}