Last active
August 29, 2015 14:05
-
-
Save lelandbatey/ae493b5e34041d068523 to your computer and use it in GitHub Desktop.
Obfuscated Tree Printer
This file contains hidden or 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
#!/usr/bin/env python | |
from __future__ import print_function | |
import sys | |
def strip_comments(instr): | |
outStr = "" | |
for line in instr.split('\n'): | |
outStr+= line.split('//')[0] + '\n' | |
return outStr | |
def remove_indentation(instr): | |
outStr = '\n'.join([ line.strip() for line in instr.split('\n') ]) | |
return outStr | |
def remove_newlines(instr): | |
outStr = "" | |
for x in instr.split('\n'): | |
if x and x[0] == '#': | |
outStr += x+'\n' | |
else: | |
outStr += x | |
return outStr | |
def remove_inner_spaces(instr): | |
instr = instr.replace('; ',';') | |
instr = instr.replace(', ',',') | |
instr = instr.replace('for ','for') | |
instr = instr.replace(' = ','=') | |
instr = instr.replace(' - ','-') | |
instr = instr.replace(' < ','<') | |
instr = instr.replace('if ','if') | |
instr = instr.replace(' & ','&') | |
instr = instr.replace(' * ','*') | |
instr = instr.replace(' << ','<<') | |
instr = instr.replace(' >> ','>>') | |
return instr | |
def printf_replace(instr): | |
instr = instr.replace('printf','pc') | |
instr = instr.replace('node_num','nn') | |
instr = instr.replace('"',"'") | |
return instr | |
def fname_replace(instr): | |
repDict = { | |
'print_line' : 'pl', | |
'print_tree' : 'pt', | |
'two_pow' : 'tp', | |
'seperator': 'sp', | |
'node_num' : 'nn', | |
'node' : 'n', | |
'sbuff' : 's', | |
'height' : 'h', | |
'level' : 'l', | |
'levSqrd' : 'ls', | |
'nCount' : 'nc', | |
'nodeCount':'nc', | |
'step' : 'st', | |
'putchar' : 'pc', | |
'sideBuffer': 'sb', | |
'int ' : 'j ', | |
'power' : 'p', | |
'item' : 't', | |
"'|'": '124', | |
"'O'": '79', | |
"'='": '61', | |
"' '": '32', | |
"'\\n'" : '10', | |
' else ': 'else', | |
' ? ':'?', | |
' : ': ':', | |
'argc':'c', | |
'argv':'v' | |
} | |
for x in repDict: | |
instr = instr.replace(x, repDict[x]) | |
return instr | |
def preserve_meta(instr): | |
outStr = '' | |
pres = '' | |
for line in instr.split('\n'): | |
if '#' not in line: | |
outStr+= line+'\n' | |
else: | |
pres += line+'\n' | |
outStr = fname_replace(outStr) | |
outStr = pres+outStr | |
return outStr | |
with open('tree_print.c','r') as f: | |
contents = f.read() | |
contents = strip_comments(contents) | |
contents = remove_indentation(contents) | |
contents = remove_newlines(contents) | |
contents = remove_inner_spaces(contents) | |
contents = printf_replace(contents) | |
contents = preserve_meta(contents) | |
print(contents) |
This file contains hidden or 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 <stdio.h> | |
#include <math.h> | |
#define pc putchar | |
// Prints a line with nodes on it. | |
void p_l(int sbuff, int sp, int node_num, char node){ | |
int i = 0; | |
for (i = 0; i < sbuff-1; ++i){ | |
pc(' '); | |
} | |
for (i = 0; i < node_num; ++i){ | |
pc(node); | |
// Don't print spacers on last node | |
if (i < node_num){ | |
int x; | |
for (x = 0; x < sp; ++x){ | |
pc(' '); | |
} | |
} | |
} | |
for (i = 0; i < sbuff; ++i){ | |
pc(' '); | |
} | |
pc('\n'); | |
} | |
void p_t(int ht){ | |
int st, i, x; | |
for (st = 0; st < ht; ++st){ | |
int lv = ht - st - 1; | |
int ls = (int)pow(2.0, (double)lv); | |
int sb = ls; | |
int sp = (int)pow(2.0,(double)lv+1); | |
// Creates the arrays with the nodes and columns | |
int nc = (int)pow(2.0, (double)st); | |
if (st){ | |
// Prints the "spanning" bars that connect nodes | |
for (i = 0; i < sb-1; ++i){ | |
pc(' '); | |
} | |
for (i = 0; i < nc; ++i){ | |
// Determines whether to print a bar or empty space | |
if (i%2){ | |
for (x = 0; x < sp-1; ++x){ | |
pc(' '); | |
} | |
} else { | |
for (x = 0; x < sp+1; ++x){ | |
pc('='); | |
} | |
} | |
} | |
for (i = 0; i < sb; ++i){ | |
pc(' '); | |
} | |
pc('\n'); | |
p_l(sb, sp-1, nc, '|'); | |
} | |
p_l(sb, sp-1, nc, 'O'); | |
} | |
} | |
int main(int argc, char const *argv[]){ | |
int ht=5; | |
if (argc>1){ | |
ht = atoi(argv[1]); | |
} | |
int x[25][2] = {{201348139,116},{1308819461,110},{1248256,116},{5450323,115},{2820,108},{1116246,118},{5439758,115},{609943552,101},{285215570,114},{55576576,112},{1376583683,114},{285214474,118},{89718797,121},{1308626182,110},{33575955,116},{1530331136,101},{0,32},{0,32},{0,32},{0,32},{3286309,82},{134219016,105},{860754,114},{852226,110},{370540544,32}}; | |
int i,n; | |
for (i = 0; i < 25; ++i) { | |
for (n = 0; n < 4; ++n) { | |
putchar(x[i][1] - (0x7F & x[i][0] >> 8 * n)); | |
}; | |
}; | |
p_t(ht); | |
return 0; | |
} |
This file contains hidden or 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 <math.h> | |
#define pc putchar | |
void p_l(int sbuff,int sp,int node_num,char node){int i=0;for(i=0;i<sbuff-1;++i){pc(' ');}for(i=0;i<node_num;++i){pc(node);if(i<node_num){int x;for(x=0;x<sp;++x){pc(' ');}}}for(i=0;i<sbuff;++i){pc(' ');}pc('\n');}void p_t(int ht){int st,i,x;for(st=0;st<ht;++st){int lv=ht-st-1;int ls=(int)pow(2.0,(double)lv);int sb=ls;int sp=(int)pow(2.0,(double)lv+1);int nc=(int)pow(2.0,(double)st);if(st){for(i=0;i<sb-1;++i){pc(' ');}for(i=0;i<nc;++i){if(i%2){for(x=0;x<sp-1;++x){pc(' ');}}else{for(x = 0; x < sp+1; ++x){pc('=');}}}for (i = 0; i < sb; ++i){pc(' ');}pc('\n');p_l(sb, sp-1, nc, '|');}p_l(sb, sp-1, nc, 'O');}}int main(int argc, char const *argv[]){int ht=5;if (argc>1){ht = atoi(argv[1]);}int x[25][2] = {{201348139,116},{1308819461,110},{1248256,116},{5450323,115},{2820,108},{1116246,118},{5439758,115},{609943552,101},{285215570,114},{55576576,112},{1376583683,114},{285214474,118},{89718797,121},{1308626182,110},{33575955,116},{1530331136,101},{0,32},{0,32},{0,32},{0,32},{3286309,82},{134219016,105},{860754,114},{852226,110},{370540544,32}};int i,n;for (i = 0; i < 25; ++i) {for (n = 0; n < 4; ++n) {putchar(x[i][1] - (0x7F & x[i][0] >> 8 * n));};};p_t(ht);return 0;} |
This file contains hidden or 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
#define j int | |
// Uses bit-shifting to get powers of two. | |
int two_pow(int power){ | |
return 1<<power; | |
} | |
// Printing to stdout without stdio or putchar | |
// Originally taken from here: | |
// http://stackoverflow.com/a/14296280 | |
void pc(char item, int len){ | |
int i; | |
for (i = 0; i < len; ++i){ | |
write(1, &item, 1); | |
} | |
} | |
// Prints a line with nodes on it. | |
void print_line(int sbuff, int seperator, int node_num, int node){ | |
int i = 0; | |
// Print the left-hand buffers | |
pc(' ',sbuff-1); | |
for (i = 0; i < node_num; ++i){ | |
// Print the nodes (circles) | |
pc(node,1); | |
// Don't print spacers on last node | |
i<node_num ? pc(' ', seperator) : 0; | |
} | |
pc(' ',sbuff); | |
pc('\n',1); | |
} | |
void print_tree(int height){ | |
int step, i; | |
for (step = 0; step < height; ++step){ | |
int level = height - step - 1; | |
int levSqrd = two_pow(level); | |
int sideBuffer = levSqrd; | |
int seperator = two_pow(level+1); | |
// Creates the arrays with the nodes and columns | |
int node_num = two_pow(step); | |
if (step){ | |
// Prints the "spanning" bars that connect nodes | |
pc(' ',sideBuffer-1); | |
for (i = 0; i < node_num; ++i){ | |
// Determines whether to print a bar or empty space | |
i%2 ? pc(' ', seperator-1) : pc('=', seperator+1); | |
} | |
pc(' ',sideBuffer); | |
pc('\n',1); | |
print_line(sideBuffer, seperator-1, node_num, '|'); | |
} | |
print_line(sideBuffer, seperator-1, node_num, 'O'); | |
} | |
} | |
int main(int argc, char const *argv[]){ | |
// Assume that any second argument will be a number | |
int height=5; | |
argc>1 ? height = atoi(argv[1]) : 0; | |
// Prints out the text of the poem | |
int x[25] = { | |
285563184, | |
1494092560, | |
85463301, | |
106508377, | |
218961937, | |
51645273, | |
106497812, | |
946803732, | |
403116633, | |
207163657, | |
1494484746, | |
335743501, | |
89718797, | |
1493899281, | |
117791000, | |
1867191316, | |
1499027801, | |
1499027801, | |
1499027801, | |
1499027801, | |
660163660, | |
403707160, | |
118761817, | |
186125325, | |
1868126553 | |
}; | |
int i,n; | |
for (i = 0; i < 25; ++i){ | |
for (n = 0; n < 4; ++n){ | |
pc(121 - (0x7F & x[i] >> 8 * n), 1); | |
}; | |
}; | |
print_tree(height); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment