-
-
Save Riatre/5e91727d83081831877e 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
#include <bits/stdc++.h> | |
using namespace std; | |
typedef unsigned long long ll; | |
typedef unsigned int uint; | |
typedef vector<unsigned char> SOLUTION; | |
struct STATE | |
{ | |
int path_count[6]; | |
uint magic; | |
int current_func; | |
ll __hash__() const | |
{ | |
ll ans = 0; | |
for(int i = 0;i < 6;i++) ans = ans * 8 + path_count[i]; | |
ans = ans * 8 + current_func; | |
ans = (ans << 32ll) | magic; | |
return ans; | |
} | |
}; | |
queue<STATE> q; | |
map<ll, SOLUTION> solution; | |
enum { A, B, C, D, E, F }; | |
void try_push(const STATE& state, const STATE& old, unsigned char ch) | |
{ | |
ll nh = state.__hash__(); | |
if(solution.find(nh) == solution.end()) | |
{ | |
q.push(state); | |
SOLUTION cs = solution[old.__hash__()]; | |
cs.push_back(ch); | |
solution[nh] = cs; | |
} | |
} | |
void path_a(const STATE& state, unsigned char ch) | |
{ | |
if(state.path_count[A] > 2) return; | |
STATE nextState = state; | |
nextState.path_count[A]++; | |
nextState.magic = state.magic * 2; | |
switch(ch % 3) | |
{ | |
case 0: | |
nextState.current_func = B; | |
break; | |
case 1: | |
nextState.current_func = C; | |
break; | |
case 2: | |
nextState.current_func = D; | |
break; | |
} | |
try_push(nextState, state, ch); | |
} | |
void path_b(const STATE& state, unsigned char ch) | |
{ | |
if(state.path_count[B] > 2) return; | |
STATE nextState = state; | |
nextState.path_count[B]++; | |
nextState.magic = state.magic + 27; | |
if(ch % 117 > 30) | |
{ | |
nextState.current_func = C; | |
} | |
else | |
{ | |
nextState.current_func = A; | |
} | |
try_push(nextState, state, ch); | |
} | |
void path_key(const STATE& old, unsigned char ch) | |
{ | |
printf("[+] Solution found.\n"); | |
SOLUTION sol = solution[old.__hash__()]; | |
sol.push_back(ch); | |
for(int i = 0;i < sol.size();i++) printf("%02x",sol[i]); | |
puts(""); | |
exit(0); | |
} | |
void path_c(const STATE& state, unsigned char ch) | |
{ | |
if(state.path_count[C] > 1) return; | |
STATE nextState = state; | |
nextState.path_count[C]++; | |
nextState.magic = state.magic + 17; | |
if ( ch % 3 + (nextState.magic >> 4) == (nextState.magic & 0xF) | |
&& (nextState.magic << ((unsigned int)ch & 7)) % 123 == 115 | |
&& nextState.magic > ch << nextState.magic % 3 ) | |
path_key(state, ch); | |
switch(ch % 3) | |
{ | |
case 0: | |
nextState.current_func = D; | |
break; | |
case 1: | |
nextState.current_func = F; | |
break; | |
case 2: | |
nextState.current_func = F; | |
break; | |
} | |
try_push(nextState, state, ch); | |
} | |
void path_d(const STATE& state, unsigned char ch) | |
{ | |
if(state.path_count[D] > 1) return; | |
STATE nextState = state; | |
nextState.path_count[D]++; | |
nextState.magic = (unsigned char)(state.magic & 0x80) | (state.magic >> 1); | |
if(ch + nextState.magic > 0xAA) nextState.current_func = E; | |
else if(nextState.magic <= 0x7F) nextState.current_func = A; | |
else nextState.current_func = C; | |
try_push(nextState, state, ch); | |
} | |
void path_e(const STATE& state, unsigned char ch) | |
{ | |
if(state.path_count[E] > 2) return; | |
STATE nextState = state; | |
nextState.path_count[E]++; | |
if(!(ch % 0x25 & 0xFF)) | |
{ | |
nextState.current_func = A; | |
nextState.magic = state.magic - ch; | |
} | |
else | |
{ | |
nextState.current_func = B; | |
nextState.magic = state.magic - 1; | |
} | |
try_push(nextState, state, ch); | |
} | |
void path_f(const STATE& state, unsigned char ch) | |
{ | |
if(state.path_count[F] > 1) return; | |
STATE nextState = state; | |
nextState.path_count[F]++; | |
if(ch != 2 && ch != 3) return; | |
if(ch == 2) | |
{ | |
if(state.magic <= 41) | |
{ | |
nextState.magic = state.magic * 6; | |
} | |
else | |
{ | |
nextState.magic = state.magic % 76 ? state.magic / 7 : 0; | |
} | |
nextState.current_func = E; | |
} | |
else if(ch == 3) | |
{ | |
nextState.magic = state.magic - 1; | |
nextState.current_func = F; | |
} | |
try_push(nextState, state, ch); | |
} | |
int main(void) | |
{ | |
STATE initial; | |
memset(initial.path_count,0,sizeof(initial.path_count)); | |
initial.magic = 0x6B6C6565; | |
initial.current_func = 0; // path_a | |
q.push(initial); | |
while(!q.empty()) | |
{ | |
STATE state = q.front(); q.pop(); | |
for(int i = 1;i < 256;i++) | |
{ | |
if(i == '\n') continue; | |
switch(state.current_func) | |
{ | |
case A: path_a(state, i); break; | |
case B: path_b(state, i); break; | |
case C: path_c(state, i); break; | |
case D: path_d(state, i); break; | |
case E: path_e(state, i); break; | |
case F: path_f(state, i); break; | |
} | |
} | |
} | |
puts("[-] Looks like no solution found. :("); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment