# PicoCTF 2k13 - Evergreen


$ decompile('HexGame.class')
private long[] serializedState() {
 long s[] = new long[4];
 int bit = 0;
 Hexagon ahexagon[][];
 int j = (ahexagon = board).length;
 for(int i = 0; i < j; i++) {
  Hexagon row[] = ahexagon[i];
  Hexagon ahexagon1[];
  int l = (ahexagon1 = row).length;
  for(int k = 0; k < l; k++) {
   Hexagon hex = ahexagon1[k];
   if(hex != null) {
    if(hex.team == 0)
     s[(bit + 1) / 64] |= 1L << (bit + 1) % 64;
    else if(hex.team == 1)
     s[bit / 64] |= 1L << bit % 64; bit += 2;
   }
  }
 }
 return s;
}
private string stateString() {
 string s = "";
 Hexagon ahexagon[][];
 int j = (ahexagon = board).length;
 for(int i = 0; i < j; i++) {
  Hexagon row[] = ahexagon[i];
  Hexagon ahexagon1[];
  int l = (ahexagon1 = row).length;
  for(int k = 0; k < l; k++) {
   Hexagon hex = ahexagon1[k];
   if(hex != null)
    if(hex.team == 0)
     s = (new StringBuilder(string.valueOf(s))).append("1").toString();
    else if(hex.team == 1)
     s = (new StringBuilder(string.valueOf(s))).append("2").toString();
    else
     s = (new StringBuilder(string.valueOf(s))).append("0").toString();
  }
 }
 return s;
}
public void update() {
 Hexagon ahexagon[][];
 int j = (ahexagon = board).length;
 for(int i = 0; i < j; i++) {
  Hexagon row[] = ahexagon[i];
  Hexagon ahexagon1[];
  int l = (ahexagon1 = row).length;
  for(int k = 0; k < l; k++) {
   Hexagon hex = ahexagon1[k];
   if(hex != null && wasCaptured(hex) && hex.team != -1)
    handleCaptured(hex);
  }
 }
 if(justCaptured && !draft.alwaysPlaceAfterCapture)
  justCaptured = rand.nextBoolean();
 if(draft.hasLimitedPieces() && numPieces[currTeam] <= 0)
  justCaptured = false;
 long s[] = serializedState();
 if(s[0] == 0x60a0495842121146L && s[1] == 0x8024120912018061L && s[2] == 0x8824421240220921L && s[3] == 0x1211880604501801L) {
  system.out.println("Success! Your key is on the next line.");
  system.out.println(stateString());
 }
}
$ cat evergreen.py
#!/usr/bin/python

def init(num):
 num = bin(num)[2:]
 i = len(num)
 while i < 64:
  num = '0' + num
  i += 1
 return num

def reverse(num):
 if num == '10':
  return '1'
 elif num == '01':
  return '2'
 else:
  return '0'

s0 = init(0x60a0495842121146L)
s1 = init(0x8024120912018061L)
s2 = init(0x8824421240220921L)
s3 = init(0x1211880604501801L)

s = s3 + s2 + s1 + s0
l = len(s)
result = ''

for i in xrange(0,l-2,2):
 result += reverse(s[l-2-i] + s[l-1-i])

print result
$ ./evergreen.py
1202202010201002012221020011001220120001200010202100102002100001201021001010000210201002021001012000012000220200120001012020102

No comments: