Created
October 28, 2015 01:35
-
-
Save ChristianGaertner/a99ab735e27ac53ce82d to your computer and use it in GitHub Desktop.
Karel Goal Decompiling
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
import sys | |
def str_startswith(prefix): | |
def inner(i): | |
return i.strip().startswith(prefix) | |
return inner | |
gen_method_prefix = 'cg_m_' | |
byte_prefix = '\u' | |
simple_conversion = { | |
'0000': '', | |
'0001': 'moveForward();', | |
'0002': 'turnLeft();', | |
'0003': 'turnAround();', | |
'0004': 'turnRight();', | |
'0005': 'pickBeeper();', | |
'0006': 'dropBeeper();', | |
'0007': 'dropBeeper();', | |
'000b': 'if (rightIsClear()) {' | |
} | |
global_methods_added = [] | |
def decompile_simple(statement): | |
src = simple_conversion[statement] | |
src += '\n' | |
return src | |
def decompile_repeat(statement): | |
return 'repeat (' + statement[-1:] + ') {' | |
def decompile_method_call(statement): | |
return gen_method_prefix + statement[-1:] + '();' | |
def decompile_statement(statement, add_byte_code=False): | |
src = '// ??' | |
if statement in simple_conversion: | |
src = simple_conversion[statement] | |
if statement.startswith('8'): | |
# repeat (x) { | |
src = decompile_repeat(statement) | |
if statement.startswith('910'): | |
# closing } | |
# the last digit refers to the opening line number, but | |
# we ignore this here and just convert it. | |
src = '}' | |
if statement.startswith('a10'): | |
# method call | |
src = decompile_method_call(statement) | |
if add_byte_code: | |
src += ' // ' + byte_prefix + statement | |
src += '\n' | |
return src | |
def generate_method(method_name, statements): | |
return 'void ' + method_name + '() {\n\t' + '\t'.join(statements) + '\n}' | |
def decompile(main_method_name, string, add_byte_code=False): | |
statements = filter(None, string.split(byte_prefix)) | |
statements = [decompile_statement(s, add_byte_code) for s in statements] | |
cat_statements = filter(None, '--'.join(statements)) | |
cat_statement_groups = filter(None, cat_statements.split('\u0000\n')) | |
statement_groups = [filter(None, i.split('--')) for i in cat_statement_groups] | |
methods = [] | |
gen_method_names = [] | |
for i, sg in enumerate(statement_groups): | |
name = main_method_name | |
if i == 0: | |
gen_method_calls = filter(str_startswith(gen_method_prefix), sg) | |
gen_method_names = list(set([x.split(';')[0][:-2] for x in gen_method_calls])) | |
else: | |
try: | |
name = gen_method_names[i - 1] | |
except Exception, e: | |
pass | |
# a new method declaration, just add it. | |
if name not in global_methods_added: | |
global_methods_added.append(name) | |
methods.append((name, sg)) | |
elif len(statement_groups) > 1: | |
# some goals redefine methods... we have to give them an unqiue name | |
name += main_method_name | |
global_methods_added.append(name) | |
methods.append((name, sg)) | |
return '\n'.join([generate_method(name, body) for name, body in methods]) | |
with open('solutions') as f: | |
content = f.readlines() | |
content = [l.rstrip() for l in content] | |
solututions = []; | |
l_method_name = '' | |
for l in content: | |
if not l.startswith(byte_prefix): | |
l_method_name = l | |
continue | |
solututions.append(decompile(l_method_name, l, True)) | |
print '\n//--\n'.join(solututions) |
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
karelsFirstProgram | |
\u0001\u0005\u0001\u0002\u0001\u0004\u0001\u0006\u0001\u0000 | |
obtainArtefact | |
\u0004\ua106\u0005\ua106\u0006\u0000\u0001\u0002\u0001\u0001\u0001\u0002\u0001\u0000 | |
defuseOneBomb | |
\ua106\u0005\u0003\ua106\u0003\u0000\u8009\u0001\u9107\u0000 | |
defuseTwoBombs | |
\ua102\u0002\ua108\u0005\u0003\ua108\u0003\u0000\u8009\u0001\u9109\u0000 | |
practiceHomerun | |
\u8004\u8009\u0001\u9102\u0005\u0002\u9101\u0000 | |
climbTheStairs | |
\u0001\u8006\u0002\u0001\u0004\u0001\u9102\u0000 | |
fillTheHoles | |
\u8004\u0001\u0004\u0001\u0006\u0003\u0001\u0004\u0001\u9101\u0000" | |
saveTheFlower | |
\u0001\u0005\u8004\u0002\u0001\u0001\u0004\u0001\u9103\u0006\u8004\u0001\u0004\u0001\u0001\u0002\u910b\u0000 | |
mowTheLawn | |
\u8002\ua106\u0004\u0001\u0004\u9101\ua10a\u0002\u0001\u0002\u8006\u0001\u0005\u910b\u0001\u0000 | |
harvestTheField | |
\ua102\u0003\ua108\u8002\u0001\u0001\u0002\u9104\u8004\u0001\u0005\u0004\u0001\u0002\u9109\u0000 | |
repairTheStreet | |
\u8009\ua104\u0001\u9101\u000b\uc10c\u0004\u0001\u0006\u0003\u0001\u0004\u0000 | |
cleanTheRoom | |
\u8004\ua106\u0004\u0001\u0004\u9101\ua10a\u0002\u0001\u0002\u8009\ua10e\u0001\u910b\u0007\uc111\u0005\u0000 | |
tileTheFloor | |
\u8064\u0006\n\f\b\u000e\uc108\u0002\u0001\u9101\u0000 | |
stealOlympicFire | |
\u0001\u8006\u0002\u0001\u0004\u0001\u9102\u0005\u0001\u0004\u8006\u0001\u910b\u0002\u0001\u0000 | |
removeTheTiles | |
\u8064\u0005\b\ud105\u0002\u0001\u9101\u0000 | |
walkTheLabyrinth | |
\u8063\n\ud108\t\uc107\u0002\ub108\u0004\u0001\u9101\u0000 | |
hangTheLampions | |
\u8009\ua104\u0001\u9101\u0002\u0005\ua10c\u0006\u0003\ua10c\u0002\u0000\n\uc10b\u0001\ub10c | |
followTheSeeds | |
\b\uc109\b\uc107\u0001\u0005\ub102\u0002\ub100\u0000 | |
cleanTheTunnels | |
\u8009\ua104\u0001\u9101\u0007\uc113\u0002\u0005\b\uc10d\u0001\u0005\ub108\u0003\n\uc112\u0001\ub10e\u0002\u0000 | |
increment | |
\u0007\uc105\u0005\u0001\ub100\u0006\u0000 | |
decrement | |
\u0007\ud107\u0006\n\uc107\u0001\ub100\u0005\u0000 | |
addSlow | |
\ua112\n\uc11a\ua11b\u0004\u0001\u0004\u0007\uc10c\u0005\u0001\ub107\u0006\ua11b\u0002\u0001\u0002\ub100\u0007\ud119\u0006\n\uc119\u0001\ub112\u0005\u0000\u0003\n\uc11a\u0001\ub11c | |
saveTheFlowers | |
\u8004\ua110\u0005\u9101\ua110\u8004\u0006\u0001\u0004\n\uc10d\u0001\ub109\u0002\u9106\u0000\u0002\u000b\ud115\u0001\ub111\u0004\u0001\u0000 | |
findTeddyBear | |
\u0007\ud108\n\uc106\u0001\ub100\u0002\ub100\u0000 | |
jumpTheHurdles | |
\u0007\ud114\n\uc106\u0001\ub100\u0002\u000b\ud10b\u0001\ub107\u0004\u0001\u0004\n\uc112\u0001\ub10e\u0002\ub100\u0000 | |
solveTheMaze | |
\u0007\ud10f\t\uc106\u0002\ub10d\n\ud10d\u000b\uc10c\u0004\ub10d\u0003\u0001\ub100\u0000 | |
quantize | |
\u8009\ua104\u0001\u9101\u0007\uc124\u0002\u8005\u0001\u9108\u0007\uc11a\b\uc110\u0001\ub10c\n\uc115\u0001\u0006\ub110\u0003\u0001\n\ud116\ub123\u0003\b\ud11f\u0001\ub11b\u0001\u0005\b\ud11f\u0002\u0000 | |
addFast | |
\u8008\u0007\u0001\u0007\u0001\u0007\u0001\u0004\uc115\uc11c\uc10c\u0006\u0001\u0004\u0001\u0006\u0001\u0001\u0003\u9101\u0000\ud11c\uc118\u0006\u0001\u0004\u0001\ub110\ud10c\ub117 | |
partyAgain | |
\u8009\ua104\u0001\u9101\u0002\u0005\ua109\u0002\u0000\n\ud10e\u0006\u0003\u0000\u0001\ua109\u0001\u0000 | |
fetchTheStars | |
\u8009\ua104\u0001\u9101\u0002\ua109\u0006\u0002\u0000\n\ud10e\u0005\u0003\u0000\u0001\ua109\u0001\u0000 | |
secureTheCave | |
\u8009\ua104\u0001\u9101\u0002\ua109\ua10e\u0004\u0000\u0001\n\ud109\u0003\u0000\u0007\uc109\u0005\u0001\ua10e\u0006\u0001\u0000 | |
layAndRemoveTiles | |
\u0007\uc104\u0003\u0000\u0006\n\b\f\r\uc10e\u0001\ua100\u0001\ub113\u0002\u0001\ua100\u0001\u0004\u0005\u0000 | |
countTheLines | |
\u8009\ua10b\u0003\n\uc107\u0001\ub103\u0002\u0001\u0002\u9101\n\uc112\u0007\uc113\u0005\ua10b\u0006\u0000\u0001\ub10b | |
countTheBeepers | |
\ua103\u0002\ub10e\u8009\ua10e\u0003\n\uc10a\u0001\ub106\u0002\u0001\u0002\u9104\n\uc115\u0007\uc116\u0005\ua10e\u0006\u0000\u0001\ub10e | |
pascalsTriangle | |
\u0006\n\uc11b\t\uc10f\u0002\u0001\u0004\u0001\ua100\u0004\u0001\u0004\u0001\u0003\u000b\uc11b\u0004\u0001\u0002\u0001\ua100\u0002\u0001\u0002\u0001\u0003\u0000 | |
findShelters | |
\u0006\n\t\u000e\u000b\u000e\ud108\u0006\u8004\n\b\f\r\uc113\u0001\ua100\u0003\u0001\u0003\u0002\u9109\u0000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment