Skip to content

Instantly share code, notes, and snippets.

@fbwright
Created March 28, 2015 22:36
Show Gist options
  • Save fbwright/c6c0e856357880dbc449 to your computer and use it in GitHub Desktop.
Save fbwright/c6c0e856357880dbc449 to your computer and use it in GitHub Desktop.
Some of my golfed code
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#Finally finished! 2014-07-26T16:25, 768 bytes - with interactive prompt
# and reading from file
#TODO: exception-checking, maybe continuation
#Brainfuck in python
#Brainfuck instructions:
# > increment the data pointer (to point to the next cell to the right).
# < decrement the data pointer (to point to the next cell to the left).
# + increment (increase by one) the byte at the data pointer.
# - decrement (decrease by one) the byte at the data pointer.
# . output the byte at the data pointer.
# , accept one byte of input, storing its value in the byte at the data pointer.
# [ if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
# ] if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.
#s='+++.>++.>++..[-] '
#s='++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.'
#s='++++++++++++++++[->++++++++++++++++<]>-[.-]'
#Prints every ASCII character, from highest to lowest
from __future__ import print_function;from sys import*
from colorama import *; init(); print(Style.DIM+Fore.GREEN, end='')
if version_info.major<3:input=raw_input
_,L,D,P,p=print,0,{},0,' ';G=lambda:D.get(P,0);C=dict(zip("><+-.,[]",("P+=1","P-=1","D[P]=G()+1","D[P]=G()-1","_(chr(G()),end='')","D[P]=ord(stdin.read(1))","I=I if G()else s[I][1]","I=s[I][1] if G() else I")))
def S(s):
S,r,p=[],[],0
for i in s:S.append(p)if i=='['else 0;x=S.pop(-1)if i==']'else 0;r.append([i,x]);r[x][1]=p if i==']'else 0;p+=1
return r
def R(X):
global D,P,I,s;I,s=0,S(X);_('Out[%s]: '%L,end='')
while I<len(s):exec(C.get(s[I][0],"pass"),globals());I+=1
_('\n')
if len(argv)<2 or argv[1]=='-i':
_("Brainfuck Interpreter 1.0.0 - by fbwright\nType 'q' to exit")
while p!='q':p=input('In[%s]: '%L);R(p);L+=1
else:R(open(argv[1]).read())
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function,division;import argparse
N=(lambda a:dict(zip(a[::2],map(lambda n:[(ord(n)//(2**i))%2 for i in range(7)],a[1::2]))))("A? \x00CSBzE[D|F\x1B1$0w3m2]5k4.7%6{9o8\x7F-\x08^\x0F'\x04T\x13P\x1FLRGsH:I\x12JtK;M9N8OxQ/R\x18SkUpVvWNX>YnZ]");G=lambda x:N.get(x,[0]*7)
r=((0,1),(0,2),(1,3,4),(2,3,5),(4,6),(5,6));R=lambda k,n:(S,C[k])[any(map(lambda i:n[i],r[k]))]+('',S)[k%2];H=lambda k,n,s:(S,h)[n[k]]*s;I=lambda s,n,k:(S,V)[n[k]]+S*s+(S,V)[n[k+1]]+S
def D(n,s=3):
O=[S]*(s*2+3)
for c in n:_=G(c);O[0]+=R(0,_)+H(0,_,s)+R(1,_);O[1:s+1]=[O[1+i]+I(s,_,1) for i in range(s)];O[s+1]+=R(2,_)+H(3,_,s)+R(3,_);O[2+s:-1]=[O[2+s+i]+I(s,_,4) for i in range(s)];O[-1]+=R(4,_)+H(6,_,s)+R(5,_)
_=[S*(1+(3+s)*len(n))];return _+O+_
if __name__=="__main__":
c=('lifelike','lines','empty','full');P=argparse.ArgumentParser();P.add_argument("str",nargs="+");P.add_argument("--style",choices=c,default=c[0]);P.add_argument("--size",nargs="?",type=int,default=3);A=P.parse_args();C,h,V,S=dict(zip(c,(('\\/<>/\\','#','#',' '),('+'*6,'-','|',' '),(' '*6,' ',' ','#'),('#'*6,'#','#',' '))))[A.style]
for l in D(' '.join(A.str).upper(),A.size):print(l)
variable &i 64 constant W 16 constant H create _M W H * chars
allot 6 constant MS 32 constant MM create _O MS MM * chars allot
: U 0 -1 ; : D 0 1 ; : R 1 0 ; : L -1 0 ; : # W * + _M + ; : &
MS * _O + ; : b? H 1- mod 0= swap W 1- mod 0= or ; : p? c@ '# <>
; : #d 2dup at-xy # c@ emit ; : #n 2dup # -rot b? if '# else bl
then swap c! ; : &xy@ & 1+ dup c@ swap 1+ c@ ; : &xy! & 1+ rot
over c! 1+ c! ; : &d dup &xy@ at-xy & c@ emit ; : &n &i @ & swap
if '@ else 'Z then swap c! &i @ &xy! 1 &i +! ; : +d rot + -rot +
swap ; : &m { n } n &xy@ +d 2dup # p? if n &xy@ #d n &xy! else
2drop then ; : (#) { x } H 0 u+do W 0 u+do i j x execute loop
loop ; : (&) { x } &i @ 0 u+do i x execute loop ; : @m 0 &m ;
: g# ['] #n (#) ; : d# page ['] #d (#) ; : g& 20 8 0 &n ; : d&
['] &d (&) ; : g@ W 2 / H 2 / -1 &n ; : g g# g@ g& ; : hK 0 0 H
at-xy key case 'i of U @m endof 'k of D @m endof 'l of R @m
endof 'j of L @m endof 'Q of drop -1 endof ." Unknown command."
endcase ; : main g d# begin d& hk until bye ; main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment