Skip to content

Commit df41aca

Browse files
committed
Add Java answer.
1 parent d029aa3 commit df41aca

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

Diff for: Doukaku/E04/Main.class

1.97 KB
Binary file not shown.

Diff for: Doukaku/E04/Main.java

+66-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,70 @@
1+
import java.util.Set;
2+
import java.util.HashSet;
3+
14
public class Main {
25
public String solve(String input) {
3-
return "";
6+
StringBuilder builder = new StringBuilder();
7+
Set<Integer> stonePath = getStonePath(input);
8+
for (int i = 0; i < 8; i++) {
9+
Set<Integer> manPath = getManPath(input, (char)(i + 'A'));
10+
if (!manPath.removeAll(stonePath)) {
11+
builder.append((char)('A' + i));
12+
}
13+
}
14+
if (stonePath.isEmpty()) {
15+
builder.deleteCharAt(builder.indexOf(input.substring(input.length() - 1)));
16+
}
17+
return builder.toString();
18+
}
19+
20+
private Set<Integer> getManPath(String input, char position) {
21+
String[] inputs = input.split(":");
22+
String paths = inputs[0];
23+
Set<Integer> manPath = new HashSet<>();
24+
for (int i = paths.length() - 1; i >= 0; i--) {
25+
char path = paths.charAt(i);
26+
if (isClockwise(path, position)) {
27+
manPath.add(i);
28+
position = moveClockwise(position);
29+
} else if (isCounterclockwise(path, position)) {
30+
manPath.add(i);
31+
position = moveCounterclockwise(position);
32+
}
33+
}
34+
return manPath;
35+
}
36+
37+
private Set<Integer> getStonePath(String input) {
38+
String[] inputs = input.split(":");
39+
String paths = inputs[0];
40+
Set<Integer> stonePath = new HashSet<>();
41+
char position = inputs[1].charAt(0);
42+
for (int i = 0; i < paths.length(); i++) {
43+
char path = paths.charAt(i);
44+
if (isClockwise(path, position)) {
45+
stonePath.add(i);
46+
position = moveClockwise(position);
47+
} else if (isCounterclockwise(path, position)) {
48+
stonePath.add(i);
49+
position = moveCounterclockwise(position);
50+
}
51+
}
52+
return stonePath;
53+
}
54+
55+
private boolean isClockwise(char path, char position) {
56+
return (path - '0') % 8 == (position - 'A') % 8;
57+
}
58+
59+
private boolean isCounterclockwise(char path, char position) {
60+
return path - '1' == position - 'A';
61+
}
62+
63+
private char moveClockwise(char position) {
64+
return --position < 'A' ? 'H' : position;
65+
}
66+
67+
private char moveCounterclockwise(char position) {
68+
return ++position > 'H' ? 'A' : position;
469
}
570
}

0 commit comments

Comments
 (0)