Commit 2a43a300 authored by Trevor Cappallo's avatar Trevor Cappallo
Browse files

fib working

parent c011ce0c
import random
import copy
MAGIC_NUMBER = -12345
MAX_PROG_SIZE = 100
STEP_LIMIT = 200
INSTRUCTIONS = [
'LOCO_0', 'INS', 'LOCO_N', 'JGTE', 'SWAB',
'SWAC', 'MULT', 'PUSH', 'POP', 'INC',
'ADD', 'JMP', 'JNE', 'NAND', 'DIV',
'NEG', 'XOR', 'LOCO_2', 'DEC', 'SWBC',
'PEEK', 'SKIP', 'LAND', 'LOR', 'IFDO',
'IFF', 'NOT', 'EQ', 'LOCO_1', 'MOD',
]
ENABLED_FIB = [ 'SWAB', 'PUSH', 'INC', 'ADD', 'JNE', ]
ENABLED = [
'LOCO_0', 'INS', 'SWAB',
'SWAC', 'MULT', 'PUSH', 'POP', 'INC',
'ADD', 'JMP', 'JNE', 'DIV',
'NEG', 'XOR',
'PEEK', 'SKIP', 'LAND', 'LOR', 'IFDO',
'IFF', 'NOT', 'MOD',
]
TIMEOUT = 250
def make_program(length):
prog = []
for _ in range(length):
prog += [INSTRUCTIONS.index(random.choice(ENABLED))]
return prog
def remove_magic_number(output, sp):
for i in range(sp):
if output[i] == MAGIC_NUMBER:
for n in range(i, sp - 1):
output[n] = output[n + 1]
return output, sp - 1
return output, sp
def run_program(prog, model_in, model_out, timeout=TIMEOUT, trace=False):
a = b = c = pc = step = la = lb = lc = lpc = lsp = 0
output = [MAGIC_NUMBER] + model_in[:] + [0] * (99 - len(model_in))
sp = len(model_in) + 1
if trace:
print "\t\t\t[init\ta={}\tb={}\tc={}\tpc={}\tsp={}]\t\t(".format(a, b, c, pc, sp),
for i in range(sp):
if output[i] == MAGIC_NUMBER:
print "__",
else:
print "{}".format(output[i]),
print ")"
while (True):
if pc >= len(prog) or pc < 0:
break
op = prog[pc]
if op == 0:
a = op
elif op == 1:
for i in range(sp, 0, -1):
output[i] = output[i - 1]
output[0] = a
if sp < 99:
sp += 1
elif op == 2:
pass
elif op == 3:
if a >= b:
pc = c - 1
elif op == 4: # SWAB
a, b = b, a
elif op == 5: # SWAC
a, c = c, a
elif op == 6:
a *= b
a %= 10000000
elif op == 7: # PUSH
if sp < 99:
output[sp] = a
sp += 1
elif op == 8:
if sp > 0:
sp -= 1
a = output[sp]
elif sp == 0:
a = output[sp]
if a == MAGIC_NUMBER:
pc = MAX_PROG_SIZE
elif op == 9: # INC
a += 1
elif op == 10: # ADD
a += b
elif op == 12: # JNE
if a != b:
pc = c - 1
elif op == 20:
if sp > 0:
b = output[sp - 1]
elif op == 21:
pc *= 2
elif op == 22:
a = 1 if a and b else 0
elif op == 23:
a = 1 if a or b else 0
pc += 1
step += 1
if step >= 10 and la == a and lb == b and lc == c and lsp == sp and lpc == pc:
return [], 666
else:
la = a
lb = b
lc = c
lsp = sp
lpc = pc
if trace:
print "\t{}\t\t[{}\ta={}\tb={}\tc={}\tpc={}\tsp={}]\t\t(".format(INSTRUCTIONS[op], step, a, b, c, pc, sp),
for i in range(sp):
if output[i] == MAGIC_NUMBER:
print "__",
else:
print "{}".format(output[i]),
print ")"
if step >= STEP_LIMIT:
break
# cool = False
# if sp >= 15 and output[0] != output[2] and output[3] != output[7] and output[8] != output[13]:
# cool = True
output, sp = remove_magic_number(output, sp)
return output, sp
timeout = total = redundant = successes = counter = degen = exact = length = 0
lower_bound, upper_bound = 5, 8
model_in = []
model_out = [1, 1, 2, 3, 5, 8, 13]
#model_out = [0, 1, 2, 3, 4, 5]
while (True):
prog = make_program(random.randint(lower_bound, upper_bound))
output = [MAGIC_NUMBER] + [0] * 99
for i in range(1, len(model_in) + 1):
output[i] = model_in[i - 1]
total += 1
if total % 10**6 == 0:
print total / 10**6
(output, r) = run_program(prog, model_in, model_out)
# if prog == [4, 9, 7, 10, 12]:
# raise Exception
if r < 0:
redundant += 1
r = -r
continue
if r == 0:
timeout += 1
continue
if r == 666:
degen += 1
continue
if exact and r != len(model_out):
continue
elif not exact and r < len(model_out):
continue
match = 0
for i in range(len(model_out)):
if output[i] == model_out[i]:
match += 1
else:
break
if match == len(model_out):
run_program(prog, model_in, model_out, trace=True)
print "---VICTORY--- total =", total
break
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment