Skip to content

Instantly share code, notes, and snippets.

@yingtai
Created September 23, 2011 12:10
Show Gist options
  • Save yingtai/1237203 to your computer and use it in GitHub Desktop.
Save yingtai/1237203 to your computer and use it in GitHub Desktop.
Malbolge インタプリタ
#coding: UTF-8
A = C = D = 0 #レジスタ
memory = [-1 for i in range(59049)] #メモリ
output = '' #出力
def run(code):
global C, D, memory
code = code.replace(' ', '').replace('\n', '')
for i, ch in enumerate(code):
memory[i] = ord(ch)
for i in range(len(code), len(memory)):
memory[i] = crz(memory[i-1], memory[i-2])
while True:
if not execute(): break
table2 = "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1CB6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@"
memory[C] = ord(table2[memory[C] - 33])
C += 1
D += 1
print output
def rotr(word):
return (word / 3) + (word % 3) * (3**9)
def crz(word1, word2):
crzed = 0
table = [[1,0,0],
[1,0,2],
[2,2,1]]
level = 1
for i in range(10):
crzed += table[word1 % 3][word2 % 3] * level
word1 /= 3
word2 /= 3
level *= 3
return crzed
def execute():
global A, C, D, memory, output
table1 = "+b(29e*j1VMEKLyC})8&m#~W>qxdRp0wkrUo[D7,XTcA\"lI.v%{gJh4G\\-=O@5`_3i<?Z';FNQuY]szf$!BS/|t:Pn6^Ha"
crypted = ord(table1[(memory[C] - 33 + C) % 94])
if crypted == 106: #j
D = memory[D]
elif crypted == 105: #i
C = memory[D]
elif crypted == 42: #*
memory[D] = rotr(memory[D])
A = memory[D]
elif crypted == 112: #p
A = memory[D] = crz(memory[D],A)
elif crypted == 60: #<
output += chr(A % 128)
elif crypted == 47: #/
A = ord(raw_input()) if A != 10 else '\n'
elif crypted == 118: #v
return False
elif crypted == 111: #o
pass
elif crypted not in range(33, 126): #印字可能なascii文字でない
return False
return True
if __name__ == '__main__':
run("(=<`:9876Z4321UT.-Q+*)M'&%$H\"!~}|Bzy?=|{z]KwZY44Eq0/{mlk**hKs_dG5[m_BA{?-Y;;Vb'rR5431M}/.zHGwEDCBA@98\\6543W10/.R,+O<'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment