Skip to content

Instantly share code, notes, and snippets.

@cgbahk
Last active May 18, 2019 05:42
Show Gist options
  • Save cgbahk/eb62ed00a2e6452c025e83a7ceee40c8 to your computer and use it in GitHub Desktop.
Save cgbahk/eb62ed00a2e6452c025e83a7ceee40c8 to your computer and use it in GitHub Desktop.
3120
extern int check(unsigned char key[200]);
unsigned char testbed[200];
// Number of correct combination
int correct()
{
return check(testbed) / 257;
}
void reset_testbed()
{
for (int i = 0; i < 200; ++i)
testbed[i] = 0;
}
// both start & end are inclusive
bool is_in(int whereis, int start, int end)
{
reset_testbed();
for (int i=start; i<=end; ++i)
testbed[i] = whereis;
return correct();
}
void test(unsigned char key[200])
{
int count = 0;
for(int whereis = 1; whereis < 256 && count < 200; ++whereis)
{
int start=0, end=199;
if(!is_in(whereis, start, end)) continue;
// binary search
while(start < end)
{
int end_org = end;
// test front half
end = (start + end) / 2;
if(is_in(whereis, start, end)) continue;
// else, it is in back half
start = end + 1;
end = end_org;
}
assert(start == end);
key[start] = whereis;
++count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment