Skip to content

Instantly share code, notes, and snippets.

@pb10005
Last active July 21, 2018 09:01
Show Gist options
  • Save pb10005/3c5dff45b2bed00f47c7d6c9674455c4 to your computer and use it in GitHub Desktop.
Save pb10005/3c5dff45b2bed00f47c7d6c9674455c4 to your computer and use it in GitHub Desktop.
Brainfxxk覚え書き

Brainfxxk覚え書き

シンタックスハイライトがあっても読みやすさが一切変わらないのが凄い
参考URL: https://qiita.com/koi_kotya/items/c730c0be5c898092ac79

文字

全8種類

  • ,
  • .
  • +
  • -
  • >
  • <
  • [
  • ]

入力

数字

48を引く

,>++++++[<-------->-]<

出力

ASCIIコードを参考に値を設定する

数字

48を足す

>++++++[<++++++++>-]<.

アルファベット

A: 65

>+++++++++++++[<+++++>-]<.

a: 97

>++++++++++[<++++++++++>-]<---.

Hello world

1行目がHello,(スペース)
2行目がWorld!

>+++++[<+++++++++++++>-]<+++++++.>+++[<++++++++++>-]<-.+++++++..+++.>++++++[<----------->-]<-.>+++[<---->-]<.
>+++++[<+++++++++++>-]<.>++++[<++++++>-]<.+++.------.--------.>++++++[<----------->-]<-.

加算

1つ右の番地に足す

[>+<-]

減算

1つ右の番地から引く

[>-<-]

コピー

2つ右の番地に値を移してから、それを元の番地と1つ右の番地に足し合わせる

[>>+<<-]>>[<+<+>>--]

乗算

番地を5個使う

[>>>+<<<-]>>>[<+<<+>>>-]<<<<[>[>+<-]>>[>+<-]>[<+<<+>>>-]<<<<-]>>

2つの数字を受け取って積を返す(入力、答えともに1桁だけ...)
1行目が入力 2行目が乗算 3行目が出力

,>++++++[<-------->-]<>,,>++++++[<-------->-]<
[>>>+<<<-]>>>[<+<<+>>>-]<<<<[>[>+<-]>>[>+<-]>[<+<<+>>>-]<<<<-]>>
>[-]++++++[<++++++++>-]<.

実行

入力: 2 3 出力: 6

条件分岐

入力が0なら0, それ以外なら1を出力

+
>,>++++++[<-------->-]<
[
[-]+>[-]++++++[<++++++++>-]<.[-]
<[-]>
]
<
[
[-]>[-]++++++[<++++++++>-]<.[-]
]

入力した数字が偶数なら"Even", 奇数なら"Odd"を出力

,>++++++[<-------->-]+[>+>+<<<[[->>>>+<<<<]>>[-]<<]>>>>[-<<<<+>>>>]<<<<-[[->>>>+<<<<]>>>[-]<<<]>>>>[-<<<<+>>>>]<<<<->>[[-]>++++++[<++++++++++>-]<+++++++++.>++++[<++++++++++>-]<+++++++++.>+[<---------->-]<-------.+++++++++.[-]<[-]>]>[[-]>+++++++[<++++++++++>-]<+++++++++.>++[<++++++++++>-]<+..[-]<<[-]>>]<<]<[-]

2桁の表示

10で除算して商と余りを表示すればよい

+>>,>>-<<<<[>>>>+<++++++++++[-<[->>>>+<<<<]>>>>[-<+<<<+>>>>]<<<<<+>[[-]<[-]>][-]>>>[-<<<+>>>]<<<<[[-]<[-]>>>[->>-<<]>>+++++++++++<<<<]>->]>>[-<<<+>>>]<<<<<]>>>>>>++++++[<<++++++++>>-]<<.<<>++++++[<++++++++>-]<-.
+
>>++
>>-
<<<<
[
>>>>+
<
++++++++++
[-
<
[->>>>+<<<<]
>>>>
[-<+<<<+>>>>]
<<<<<+
>
[
[-]
<[-]
>]
[-]
>>>
[-<<<+>>>]
<<<
<
[
[-]
<[-]
>>>[->>-<<]
>>+++++++++++
<<<<
]
>-
>
]
>>[-<<<+>>>]
<<<<<
]
>>>>
[
>>++++++[<<++++++++>>-]<<.
[-]
]
<<
>++++++[<++++++++>-]<-.[-]<<
,>++++++[<-------->-]
+
[
>+
>+
<<<
[
[->>>>+<<<<]
>>[-]<<
]
>>>>[-<<<<+>>>>]
<<<<-
[
[->>>>+<<<<]
>>>[-]<<<
]
>>>>[-<<<<+>>>>]
<<<<-
>>
[
[-]
>++++++[<++++++++++>-]<+++++++++.>++++[<++++++++++>-]<+++++++++.>+[<---------->-]<-------.+++++++++.[-]
<[-]>
]
>
[
[-]
>+++++++[<++++++++++>-]<+++++++++.>++[<++++++++++>-]<+..[-]
<<[-]>>
]
<<
]<[-]
>++++[<++++++++++>-]<+++++++++.-----.+++++.-----.[-]
+++++++++++
>+>+
<<
[
>[->>+<<]
>[->>+<<]
>>[-<<+<+>>>]
<[-<+>]
<
[->+<]
>[-<+>>>>>+<<<<]
>>
+
>>
>>-
<<<<
[
>>>>+
<
++++++++++
[-
<
[->>>>+<<<<]
>>>>
[-<+<<<+>>>>]
<<<<<+
>
[
[-]
<[-]
>]
[-]
>>>
[-<<<+>>>]
<<<
<
[
[-]
<[-]
>>>[->>-<<]
>>+++++++++++
<<<<
]
>-
>
]
>>[-<<<+>>>]
<<<<<
]
>>>>
[
>>++++++[<<++++++++>>-]<<.
[-]
]
<<
>++++++[<++++++++>-]<-.[-]<<
<<<
>>++++[<+++++++++++>-]<.[-]<
<<-
]
>+++++[<+++++++++++++>-]<+++++++.>+++[<++++++++++>-]<-.+++++++..+++.>++++++[<----------->-]<-.>+++[<---->-]<.
>+++++[<+++++++++++>-]<.>++++[<++++++>-]<.+++.------.--------.>++++++[<----------->-]<-.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program{
public static void Main(){
/* ASCII文字列を標準入力から受け取って、同じ文字列を出力するBFのコードを返す */
var line = System.Console.ReadLine();
var array = new CharArray(line);
var res = array.DifArray().Select(x=>new BFBuilder(x).Build());
Console.WriteLine(string.Join("",res));
}
}
public class CharArray{
public CharArray(string str){
value = str;
}
string value;
public IEnumerable<int> DifArray(){
yield return value[0];
for(int i=1;i<value.Length;i++){
yield return value[i] - value[i-1];
}
}
}
public class BFBuilder{
public BFBuilder(int n){
num = n;
}
int num;
public string Build(){
return Build("",num);
}
public string Build(string tmp, int num){
if(Math.Abs(num) < 10){
for(int i=0;i<Math.Abs(num);i++){
if(num > 0)
tmp+="+";
else
tmp += "-";
}
tmp += ".";
}
else{
tmp += ">";
for(int i=0;i<Math.Abs(num)/10;i++){
tmp += "+";
}
if(num < 0)
tmp += "[<---------->-]<";
else
tmp += "[<++++++++++>-]<";
tmp = Build(tmp, num%10);
}
return tmp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment