-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd25.java
53 lines (50 loc) · 1.73 KB
/
d25.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
import static java.lang.System.out;
import java.io.*;
import java.util.*;
public class d25 {
record Pos(int x, int y) {}
public static void main(String[] args) throws IOException {
var reader = new BufferedReader(new InputStreamReader(System.in));
var west = new HashSet<Pos>(10_000);
var south = new HashSet<Pos>(10_000);
var wedge = 0;
var sedge = 0;
for (int y = 0; reader.ready(); y++, sedge++) {
var line = reader.readLine();
wedge = Math.max(wedge, line.length());
for (int x = 0; x < line.length(); x++) {
switch (line.charAt(x)) {
case '>': west.add(new Pos(x, y)); break;
case 'v': south.add(new Pos(x, y)); break;
}
}
}
var step = 0;
var moved = false;
do {
moved = false; step++;
var wstep = new HashSet<Pos>(10_000);
var sstep = new HashSet<Pos>(10_000);
for (var c : west) {
var cc = new Pos(c.x + 1 >= wedge ? 0 : c.x + 1, c.y);
if (west.contains(cc) || south.contains(cc))
wstep.add(c);
else {
wstep.add(cc);
moved = true;
}
}
for (var c : south) {
var cc = new Pos(c.x, c.y + 1 >= sedge ? 0 : c.y + 1);
if (wstep.contains(cc) || south.contains(cc))
sstep.add(c);
else {
sstep.add(cc);
moved = true;
}
}
west = wstep; south = sstep;
} while (moved);
out.println(step);
}
}