Skip to content

Instantly share code, notes, and snippets.

@nikuuchi
Created August 20, 2011 12:47
Show Gist options
  • Select an option

  • Save nikuuchi/1159063 to your computer and use it in GitHub Desktop.

Select an option

Save nikuuchi/1159063 to your computer and use it in GitHub Desktop.
テキストファイルをbrainf*ckにエンコードするスクリプト。とりあえず思いつくままに書いた。
def square op,len
s = (len**(0.5)).to_i
t = (s<8)? "+"*s : square("+",s)
m = len - (s**2)
m = (m< 8) ? op * m : square( op,m)
">"+t+"[<"+op*s+">-]<"+m
end
def encode ch,point
if ch > point
if ch - point >= 8
str = square("+",(ch - point))
[str+".",ch]
else
["+"*(ch - point)+".",ch]
end
elsif ch < point
if point - ch >= 8
str = square("-",(point - ch))
[str+".",ch]
else
["-"*(point -ch)+".",ch]
end
else
[".",point]
end
end
file = File.open(ARGV[0])
point = 0
str = ""
file.each do |line|
line.each_byte do |ch|
tmp,point = encode ch,point
str += tmp
end
end
str2 = ""
str.split("<>").each do |s|
str2 += s
end
puts str2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment