Created
August 20, 2011 12:47
-
-
Save nikuuchi/1159063 to your computer and use it in GitHub Desktop.
テキストファイルをbrainf*ckにエンコードするスクリプト。とりあえず思いつくままに書いた。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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