Created
December 21, 2021 16:44
-
-
Save Samathy/2178b991e33370d8ed57bfa368079f05 to your computer and use it in GitHub Desktop.
A Python script which prints a brainfuck program in the shape of an xmas tree, which prints an xmas tree. `python3 tree.py [size of tree e.g 11 ]`
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 | |
import io | |
SPACE = 32 | |
CHAR_X = 88 | |
NEWLINE = 10 | |
def print_tree(s): | |
lines = [] | |
width = 1 | |
while len(s) >= width: | |
line, s = s[:width], s[width:] | |
lines.append(line) | |
width += 2 | |
if s: | |
lines.append(s) | |
else: | |
lines.append('#') | |
output = io.StringIO() | |
for line in lines: | |
print(('{:^' + str(width) + 's}').format(line), file=output) | |
return output.getvalue() | |
def bf_tree(levels=5): | |
program = [] | |
program.append(SPACE * "+") | |
program.append(">") | |
program.append(CHAR_X * "+") | |
program.append(">") | |
program.append(NEWLINE * "+") | |
max_no_spaces = levels | |
for level in range(1, levels + 1): | |
program.append("<<") # move cursor to SPACE cell | |
program.append((max_no_spaces-level) * ".") # output spaces | |
program.append(">") # move cursor to CHAR_X cell | |
if level == 1: | |
program.append(".") | |
else: | |
program.append((level*2-1) * ".") | |
program.append(">") # move cursor to NEWLINE cell | |
program.append(".") | |
program.append("<<") # move cursor to SPACE | |
program.append((max_no_spaces-1) * ".") | |
program.append(">") # move cursor to CHAR_X | |
program.append("---") # deduct 3 from X to make U | |
program.append(".") | |
return ''.join(program) | |
if __name__ == '__main__': | |
try: | |
levels = int(sys.argv[1]) | |
except: | |
levels = 5 | |
print(print_tree(bf_tree(levels))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment