-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay8.java
58 lines (51 loc) · 1.63 KB
/
Day8.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
import com.horstmann.adventofcode.*;
CharGrid grid;
Map<Character, List<Location>> freqs;
void parse(Path p) throws IOException {
grid = CharGrid.parse(p);
freqs = grid.locations().filter(l -> grid.get(l).toString().matches("[0-9a-zA-Z]")).collect(Collectors.groupingBy(grid::get));
}
Set<Location> antiNodes(Location p, Location q) {
int dr = q.row() - p.row();
int dc = q.col() - p.col();
return Set.of(p.movedBy(-dr, -dc), p.movedBy(dr, dc));
}
Set<Location> antiNodes2(Location p, Location q) {
int dr = q.row() - p.row();
int dc = q.col() - p.col();
var result = new HashSet<Location>();
for (var r = q; grid.isValid(r); r = r.movedBy(dr, dc)) result.add(r);
for (var r = p; grid.isValid(r); r = r.movedBy(-dr, -dc)) result.add(r);
return result;
}
Object part1() {
var as = new HashSet<Location>();
for (var s: freqs.values())
for (var p : s)
for (var q : s)
if (p != q)
as.addAll(antiNodes(p, q));
return as.stream().filter(grid::isValid).count();
}
Object part2() {
var as = new HashSet<Location>();
for (var s: freqs.values())
for (var p : s)
for (var q : s)
if (p != q)
as.addAll(antiNodes2(p, q));
return as.size();
}
void main() throws IOException {
Util.time(() -> {
parse(Util.inputPath("a"));
IO.println(part1());
IO.println(part2());
parse(Util.inputPath("b"));
IO.println(part1());
IO.println(part2());
parse(Util.inputPath("z"));
IO.println(part1());
IO.println(part2());
});
}