I wanted to write the code with Brainfuck, but it seemed to be a pain to write the code, so I decided to have python write it instead. However, I can't make something that difficult, so I made a program that outputs a character string as it is.
The first thing I thought about was to convert each character of the string received as input to ASCII and then simply increment the pointer and output it. In terms of code, it looks like this.
makebf0.py
import os
#Already hoge.Delete any file called bf
if os.path.exists("/hoge.bf"):
os.remove("hoge.bf")
s = input()
array = []
#Get ASCII for each character
for si in s:
array.append(ord(si))
f = open("hoge.bf", "w")
for ai in array:
for i in range(ai):
f.write("+")
f.write(".>")
#new line
f.write("++++++++++++.")
f.close()
When you do this
$ python makebf0.py
brainf*ck
$ cat hoge.bf
hoge.bf
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++++++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>+++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++.>++
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
.>++++++++++++++++++++++++++++++++++++++++++.>++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++.>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++++++++++++.>++++++++++++.
Code like this is generated.
This makes the code long and uninteresting, so I'll try to compress the generated code.
The idea for compressing the code was to use a loop to reduce the number of +. As an algorithm, if the number of loop turns is n and the ASCII of the i-th character of the string is si, min (si / n, si / n + 1) is incremented in the loop and the fraction is the loop. Incremented or decremented outside. I tried to generate the code with the smallest number of +. The code looks like this.
makebf1.py
import os
import math
if os.path.exists("./hoge2.bf"):
os.remove("hoge2.bf")
s = input()
array= [] #Character si ascii
for si in s:
array.append(ord(si))
f = open("hoge2.bf", "w")
MAX = 0
for ai in array:
if MAX < ai:
MAX = ai
list = [] #Difference
#i's decisive decision
for i in range(MAX):
if i == 0:
continue
sum = i
for ai in array:
r1 = ai // i
r2 = (ai + i) // i
if ai - r1 * i > r2 * i - ai:
r = r2 * i - ai
sum += r2
else :
r = ai - r1 * i
sum += r1
sum = sum + r
list.append(sum)
m = 10000000000
for i in range(len(list)):
#print(li)
li = list[i]
if m > li:
#print(li)
m = li
std = i
#Number of times to turn the loop
for i in range(m):
f.write('+')
flag = []
#Loop contents
f.write('[')
for ai in array:
r1 = ai // m
r2 = (ai + m) // m
f.write('>')
if ai - r1 * m > r2 * m - ai:
flag.append(-(r2 * m - ai))
for j in range(r2):
f.write('+')
else:
flag.append(ai - r1 * m)
for j in range(r1):
f.write('+')
#Return pointer to loop counter
for i in range(len(array)):
f.write('<')
f.write('-')
f.write(']')
#Handle fractions
for i in range(len(array))
f.write('>')
if flag[i] < 0:
for j in range(-flag[i]):
f.write('-')
else:
for j in range(flag[i]):
f.write('+')
f.write('.')
#Begin on a new line
f.write('>++++++++++++.')
f.close()
When you run
$ python makebf1.py
brainf*ck
$ cat hoge2.bf
hoge2.bf
+++++++++++++++++++++[>+++++>+++++>+++++>+++++>+++++>+++++>++>+++++>+++++
<<<<<<<<<-]>-------.>+++++++++.>--------.>.>+++++.>---.>.>------.>++.>++++++++++++.
The code has been compressed considerably. I was able to generate code in 158 bytes in compressed code, compared to 921 bytes in straightforward code. You did it.
I wasn't used to writing python, so I was able to write this code to get used to python a bit. Brainfuck is also an interesting language so I would like to be able to do more complicated things. If you have any better algorithms or bad things about your code, please let us know. Thank you for reading this far.
https://qiita.com/TomoShiozawa/items/25dcce1540085df71053 https://qiita.com/saba383810/items/39e20b11c71b3dfd2589
Recommended Posts