Skip to content

Instantly share code, notes, and snippets.

@michel-zimmer
Created May 8, 2015 10:05
Show Gist options
  • Save michel-zimmer/748486443149f3600c1c to your computer and use it in GitHub Desktop.
Save michel-zimmer/748486443149f3600c1c to your computer and use it in GitHub Desktop.
package hacked.puzzles.story.cyber_attack.match;
import java.util.ArrayList;
public enum Bracket {
CLOSE {
@Override
public String toString() {
return ")";
}
},
OPEN {
@Override
public String toString() {
return "(";
}
};
public static ArrayList<Bracket> generate(int n) {
final ArrayList<Bracket> pattern = new ArrayList<>();
while (n > 0) {
pattern.add(0, n % 2 == 0 ? CLOSE : OPEN);
n -= n % 2;
n /= 2;
}
return pattern;
}
public static boolean hacked(ArrayList<Bracket> input) {
input = new ArrayList<>(input);
if (input.size() % 2 != 0) {
return false;
}
while (input.size() > 0) {
int var_a = 0;
while (var_a < input.size() && input.get(var_a) != CLOSE) {
++var_a;
}
if (var_a == 0 || var_a == input.size()) {
return false;
}
input.remove(var_a);
input.remove(var_a - 1);
}
assert input.size() == 0;
return true;
}
public static void main(final String[] args) {
for (int i = 0; i < 64; i++) {
final ArrayList<Bracket> input = generate(i);
if (hacked(input)) {
final StringBuilder sb = new StringBuilder();
sb.append('[');
for (int j = 0; j < input.size(); ++j) {
if (j != 0) {
sb.append(',');
sb.append(' ');
}
sb.append(input.get(j).toString());
}
sb.append(']');
System.out.println(sb.toString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment