Skip to content

Instantly share code, notes, and snippets.

@junk0612
Created June 4, 2016 08:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junk0612/1a057f55504b1f0dc0e3ef09235f3f1e to your computer and use it in GitHub Desktop.
Save junk0612/1a057f55504b1f0dc0e3ef09235f3f1e to your computer and use it in GitHub Desktop.
import java.util.Set;
import java.util.HashSet;
import java.util.TreeSet;
public class Main {
public String solve(String input) {
String[] inputs = input.split(":");
char c = inputs[1].charAt(0);
Set<Character> set = new HashSet<>();
for (char s: inputs[0].toCharArray()) {
int i = s - '0';
set.add(c);
if ((c - 'A') % 8 == i % 8) {
c--;
if (c < 'A') {
c = 'H';
}
}
else if (c - 'A' + 1 == i) {
c++;
if (c > 'H') {
c = 'A';
}
}
}
set.add(c);
StringBuilder builder = new StringBuilder();
for (char s: "ABCDEFGH".toCharArray()) {
if (set.add(s)) {
builder.append(s);
}
}
String str = builder.toString();
set = new TreeSet<>();
for (char x: str.toCharArray()) {
for (char s: inputs[0].toCharArray()) {
int i = s - '0';
if ((x - 'A') % 8 == i % 8) {
x--;
if (x < 'A') {
x = 'H';
}
} else if (x - 'A' + 1 == i) {
x++;
if (x > 'H') {
x = 'A';
}
}
}
set.add(x);
}
builder = new StringBuilder();
for (char x: set) {
builder.append(x);
}
return builder.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment