-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay6.java
61 lines (54 loc) · 1.38 KB
/
Day6.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
import com.horstmann.adventofcode.*;
import static com.horstmann.adventofcode.Direction.*;
CharGrid grid;
void parse(Path p) throws IOException {
grid = CharGrid.parse(p);
}
Set<Location> escape() {
var locations = new HashSet<Location>();
record Arrow(Location l, Direction d) {}
var arrows = new HashSet<Arrow>();
var p = grid.findFirst('^');
var d = N;
arrows.add(new Arrow(p, d));
for (;;) {
var next = p.moved(d);
var c = grid.get(next);
if (c == null) {
return locations;
} else {
if (c != '.' && c != '^') {
d = d.turn(2);
} else {
p = next;
locations.add(p);
}
if (!arrows.add(new Arrow(p, d))) return null;
}
}
}
Object part1() {
return escape().size();
}
Object part2() {
var locations = escape();
int count = 0;
for (var p : locations) {
if (grid.get(p) == '.') {
var old = grid.put(p, 'O');
if (escape() == null) count++;
grid.put(p, old);
}
}
return count;
}
void main() throws IOException {
Util.time(() -> {
parse(Util.inputPath("a"));
IO.println(part1());
IO.println(part2());
parse(Util.inputPath("z"));
IO.println(part1());
IO.println(part2());
});
}