Created
May 8, 2015 10:05
-
-
Save michel-zimmer/748486443149f3600c1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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