10
\$\begingroup\$

A very simple challenge today. Output a truthy value whether the ASCII character (or it's code) supplied is a brainfuck instruction (one of +-,.<>[]), and a falsy value if it's a comment (everything else).

Input

+
#
<
>
.
P
,

Output

true
false
true
true
true
false
true
\$\endgroup\$
8
  • \$\begingroup\$ As in, can the answer be formatted as either a program or function? (such as requiring main(){} and all of that in C answers) \$\endgroup\$ Apr 11 '20 at 17:57
  • 2
    \$\begingroup\$ @RedwolfPrograms doesn't matterdoesn't matter \$\endgroup\$ Apr 11 '20 at 18:02
  • 1
    \$\begingroup\$ Is the input character guaranteed to be printable ASCII? (32-126) \$\endgroup\$
    – Arnauld
    Apr 11 '20 at 18:47
  • 1
    \$\begingroup\$ No, but I'd like to see (a part of an already valid answer) that assumes it's printable as an "addon". As I've stated (or rather deleted the comment afterwards), as it's quite a boring challenge, I'm looking for some creativity \$\endgroup\$ Apr 11 '20 at 18:50
  • 2
    \$\begingroup\$ @LuisMendo No, according to the esolang wiki, it's often lowercase \$\endgroup\$ Apr 12 '20 at 15:56

38 Answers 38

18
\$\begingroup\$

brainfuck, 105 103 95 bytes

Outputs \x00 for false and \x01 for true.

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

Try it online!

The general principle is comparing against all values (decreasing partial distances of the char-codes), using loops to logically OR the results, and if none matched - change the output cell from 1 to 0.

See my 4 answer for a more detailed explanation (4 instructions read more easily).

\$\endgroup\$
9
\$\begingroup\$

unsure, 1302 bytes

um um yeah err hm yeah uhhhhhhhh then ummmm uhhh errrr uhh errr ummmmm yeah err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops oops oops oops oops um then heh then uhhhhhhhhh no oops oops then um err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops oops oops oops um then heh then uhhhhhhhh no oops oops then um err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops oops oops um then heh then uhhhhhhh no oops oops then um err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops oops um then heh then uhhhhhh no oops oops then ummmmmmm uh errr uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops oops um then heh then uhhhhh no oops oops then umm err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops oops um then heh then uhhhh no oops oops then ummmmmm uhhh ummmmm errrrrr uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops oops um then heh then uhhh no oops oops then umm err uh then heh then err um heh but um um yeah err heh no oops then but oops heh oops um then heh then uhh no oops oops oops okay

This was not easy. Unsure is a new stack based language I've created, that uses words people tend to say when they're not sure about something.

It's kind of hard to explain how such a large program works, but I'll try to give a high-level explanation. First, here's an expanded version of this program:

um um yeah err hm yeah uhhhhhhhh then ummmm uhhh errrr uhh errr ummmmm yeah err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops oops oops oops um then heh then uhhhhhhhhh no oops oops then um err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops oops oops um then heh then uhhhhhhhh no oops oops then um err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops oops um then heh then uhhhhhhh no oops oops then um err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops um then heh then uhhhhhh no oops oops then ummmmmmm uh errr uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops um then heh then uhhhhh no oops oops then umm err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops um then heh then uhhhh no oops oops then ummmmmm uhhh ummmmm errrrrr uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops um then heh then uhhh no oops oops then umm err uh

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops um then heh then uhh no oops oops

oops okay

The first part sets up the stacks. There are two stacks, one of which is selected as the active stack. The first stack contains a 0 at the bottom (the return value), as well as 8 copies of the input character's ASCII character code, negated.

After this, there are 8 sections to check for equality (first one shown as an example):

then heh then err um heh
but um um yeah err heh no oops then
but oops heh oops oops oops oops oops oops oops oops um then heh then uhhhhhhhhh no oops oops then um err uh

The first line will pop the first stack and add it to the first, which in this case is 43 (the character code for +). It also pushes 1 to the first stack. The next line is essentially an if statement, which is used to create a NOT operator by pushing 0 to the first stack if the result of adding the character code is not 0 (meaning they are not the same).

The third line is the most complicated. It starts with a sort of else statement, as it only runs when the second line's if statement doesn't push 0 (and therefore the character code was 43). It will then replace the bottom of the first stack with 1, the new output.

Finally, after the "else statement", the character code is incremented to 44, for ,, and the process is repeated 7 times.

The last line, oops okay, just outputs the bottom item in the first stack, as the previous operations have cleared it out except for one copy of the input (oops discards the top of the stack, and okay outputs).

There's definitely a few bytes I could golf here and there, might work on it later.

\$\endgroup\$
3
  • 10
    \$\begingroup\$ um err uh okay uh well then um uh yeah um sure \$\endgroup\$
    – S.S. Anne
    Apr 23 '20 at 20:57
  • \$\begingroup\$ Why is like not in there? \$\endgroup\$ Jul 13 '21 at 9:18
  • \$\begingroup\$ @EnderShadow8 Not sure how to answer (luckily this language gives me lots of options to express that :p) \$\endgroup\$ Jul 13 '21 at 14:06
9
\$\begingroup\$

Python, 16 bytes

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

Try it online!

An anonymous method is shorter here than a lambda. See my tip Object method as answer.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ <built-in method count of str object at 0x101520730>? \$\endgroup\$ Apr 12 '20 at 15:32
  • \$\begingroup\$ The str object "at 0x101520730" is the string literal in the program. It has a built-in method named count. This method will return 1 only if given an instruction character. You can call it in the normal way by providing an immediate parameter e.g. ('.') or you can assign it to a variable (e.g. f) and call f later. \$\endgroup\$
    – Neil
    Apr 13 '20 at 13:53
  • \$\begingroup\$ @VictorVosMottorthanksMonica Note also that the usual submissions of lamdba's are represented as a similarly weird value like <function <lambda> at 0x7fd4f39d7758>. \$\endgroup\$
    – xnor
    Apr 13 '20 at 21:37
  • 1
    \$\begingroup\$ Anything that doesn't have a __str__ or a __repr__ has that. \$\endgroup\$
    – PkmnQ
    Apr 14 '20 at 2:04
8
\$\begingroup\$

Retina 0.8.2, 9 bytes

[]+-.<>[]

Try it online! Link includes test cases. Explanation: This is actually a character class; the outer []s delimit the class, as (by being placed immediately after the opening [) the first ] is actually part of the class. The , isn't needed as the - creates a range from + to . which helpfully includes the - itself as well as the ,.

\$\endgroup\$
1
  • \$\begingroup\$ I am now worried that the stuff that parses regular expressions is actually a backtracking regex engine... \$\endgroup\$ Apr 12 '20 at 3:37
7
\$\begingroup\$

vJASS (Warcraft 3), 299 278 265 bytes

Using //! import zinc "<code_path>" command to exclude //! zinc and //! endzinc.


Prints T (True) and F (False).

library Q{trigger W=CreateTrigger();integer E;function onInit(){TriggerRegisterPlayerChatEvent(W,Player(0),"",false);TriggerAddAction(W,function(){for(0<=E<8){if(GetEventPlayerChatString()==SubString("+-<>.,[]",E,E+1)){BJDebugMsg("T");return;}}BJDebugMsg("F");});}}

Readable Version:

library CodeGolf{
    trigger chatEvent = CreateTrigger();
    integer Index;

    function onInit(){
        TriggerRegisterPlayerChatEvent(chatEvent, Player(0), "", false);
        TriggerAddAction(chatEvent, function(){
            for (0 <= Index < 8){
                if(GetEventPlayerChatString() == SubString("+-<>.,[]", Index, Index + 1)) {
                    BJDebugMsg("T");
                    return;
                }
            }
            BJDebugMsg("F");
        });
    }
}

Output: enter image description here

\$\endgroup\$
0
5
\$\begingroup\$

05AB1E, 11 bytes

+1 due to a bug fix noted by @Kevin Cruijssen

-1 thanks to @Kevin Cruijssen

"+-,.<["ºIå

Try it online!

an interesting approach, but sadly it's longer and it doesn't work:

•q”;ιÚ•2ôIÇ;îå

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Your 11-byter should be "+-,.<>[]"Iå or "+-,.<>[]"så. You want to check if the input is in that string, not if that string is in the input. And you can save a byte by using the mirror builtin: "+-,.<["ºIå \$\endgroup\$ Apr 14 '20 at 7:59
3
\$\begingroup\$

4, 131 bytes

Outputs \x00 for false, \x01 for true.

3.799600006010160202614146292964343199994389919999018991999901899199990189919999148991999902899199992989919999028995004999999995014

Try it online!

Explanation

4 doesn't have a simple comparison or condition instructions, so I used while-not-zero loops to fake them and match by distances:

3.

6 00 00            # set constants
6 01 01 
6 02 02 
6 14 14 
6 29 29 
6 43 43 

7 99               # input as integer

1 99 99 43 - 8 99  # for each brainfuck character
1 99 99 01 - 8 99  # compare to the input,
1 99 99 01 - 8 99  #  (subtracting differences to the input)
1 99 99 01 - 8 99  #  and jump to the end if true,
1 99 99 14 - 8 99  #  otherwise start a loop (to fake branching)
1 99 99 02 - 8 99 
1 99 99 29 - 8 99 
1 99 99 02 - 8 99 
5 00 4             # if all comparisons failed, print 'false' and exit
9 9 9 9 9 9 9 9    # close loops

5 01 4             # print 'true' and exit
\$\endgroup\$
3
\$\begingroup\$

Haskell, 18 bytes

(`elem`"+-.,<>[]")

Try it online!

Low-effort unnamed function.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ For convenience with pointfree TIO submissions, you can add the -cpp flag and use backslash to continue a line like this: Try it online! \$\endgroup\$
    – xnor
    Apr 13 '20 at 21:39
  • \$\begingroup\$ @xnor ah I tried to do that without the flag, ty \$\endgroup\$
    – clapp
    Apr 13 '20 at 23:30
3
\$\begingroup\$

Python 3, 26, 23 bytes

lambda s:s in'+-,.<>[]'

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Is this really a valid format? \$\endgroup\$ Apr 12 '20 at 6:55
  • \$\begingroup\$ @dingledooper , I might have gotten mixed up. I'm pretty new, still don't know all the valid formats. The only reason I changed this from my old answer was because I saw xnor's answer, which I thought was invalid, but, hey - he's way more experienced than me, so I probably just didn't know something. Am changing back to my old one. \$\endgroup\$
    – Dion
    Apr 12 '20 at 6:59
  • 2
    \$\begingroup\$ For your information, xnor's answer is still a function, in that it can be called with a parameter. Your previous answer was not, it was just a snippet since in is a keyword that doesn't make a function \$\endgroup\$
    – Jo King
    Apr 12 '20 at 12:32
  • \$\begingroup\$ @Dion See also my tip Object method as answer. \$\endgroup\$
    – xnor
    Apr 13 '20 at 21:59
  • 2
    \$\begingroup\$ This isn't a valid answer; it's an expression, not a function. \$\endgroup\$
    – FlipTack
    May 30 '20 at 15:38
2
\$\begingroup\$

Keg, -rt, 10 bytes

-`[]+-,.<>

This:

  • Pushes the string []+-,.<> onto the stack
  • Subtracts that string from the input.

If it is a BF instruction, an empty string will be printed, otherwise, the input character will be printed.

\$\endgroup\$
2
\$\begingroup\$

Python 3, 26 bytes

print(input()in"+-,.<>[]")
\$\endgroup\$
2
  • \$\begingroup\$ You need something to output the result, usually print. See the Python I/O summary. \$\endgroup\$
    – xnor
    Apr 13 '20 at 21:40
  • \$\begingroup\$ I'm pretty sure you can use exit instead of print. \$\endgroup\$
    – PkmnQ
    Apr 24 '20 at 6:14
2
\$\begingroup\$

C (gcc), 28 bytes

f(c){c=index("+-,.<>[]",c);}

No math here, no math there, no math anywhere.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Pyke, 3 bytes

Built-ins FTW!

~B{

Try it online!

Explanation

~B  "><+-.,[]"
  { Does it contain input?
\$\endgroup\$
2
\$\begingroup\$

Desmos, 43 bytes

1\left\{42<i<47,i=60,i=62,i=91,i=93\right\}

Try it online

Desmos doesn't support strings, so this turns from relatively trivial string comparison to relatively trivial integer comparison. I tried to compress the higher numbers, but I couldn't find a nice way to exclude 61 and 92, and the sequence doesn't show up in OEIS.

\$\endgroup\$
2
  • \$\begingroup\$ Where exactly can we test desmos answers at? Please do provide a link in later submissions. \$\endgroup\$
    – Razetime
    Aug 24 '20 at 2:21
  • 1
    \$\begingroup\$ @Razetime You can test Desmos answers at the link provided at Desmos (note: I accidentally put the wrong one for this question, fixing that as well). I didn't bother to generate a specific link for this answer as it's trivial, but I probably should have. Adding it now. \$\endgroup\$ Aug 24 '20 at 3:49
1
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 45 bytes

 "<>[]+-,." INPUT :F(F)
 X =1
F OUTPUT =X
END

Try it online!

1 for truthy, empty line for falsey.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 25 23 bytes

-2 thanks to @Arnauld

c=>/[+-.<>[\]]/.test(c)

Try it online!

\$\endgroup\$
5
  • \$\begingroup\$ 23 bytes \$\endgroup\$
    – Arnauld
    Apr 11 '20 at 20:01
  • \$\begingroup\$ @Arnauld Forgot about .test, thanks! \$\endgroup\$ Apr 11 '20 at 20:16
  • \$\begingroup\$ You can just use /[+-.<>[\]]/.test - point-free \$\endgroup\$
    – pxeger
    Aug 24 '20 at 7:59
  • 1
    \$\begingroup\$ @pxeger Doesn't work. Try f=/[+-.<>[\]]/.test then f("[") \$\endgroup\$ Aug 24 '20 at 13:14
  • \$\begingroup\$ @RedwolfPrograms wow I should never under-estimate the stupidity of JavaScript \$\endgroup\$
    – pxeger
    Aug 24 '20 at 15:29
1
\$\begingroup\$

Pyth, 10 bytes

/"+-,.<>[]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 18 bytes

{it in "+-<>[].,"}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Arn, 9 bytes

[‚‚◄┴´£Q7

Try it!

Explained

Unpacked: "+-.,<>[]"&

  "+-.,<>[]" Literal string
& Contains element
  _ Variable initialized to STDIN; implied
\$\endgroup\$
1
\$\begingroup\$

Scala 2.12, 15 bytes

"<>+-.,[]"toSet

This takes the string of all brainf*** instructions and turns it into a set. Since Scala's sets are also predicates, we can treat it like a function that returns true when it's a valid instruction.

Scala 2.12 is required for postfix operator toSet.

Try it online

\$\endgroup\$
1
\$\begingroup\$

Jelly, 10 bytes

ẇ“+-<>[],.

Try it online!

How it works

ẇ“+-<>[],. - Main link. Takes a character C on the left
 “+-<>[],. - Yield "+-<>[],."
ẇ          - Is C in that string?
\$\endgroup\$
1
\$\begingroup\$

Red, 27 bytes

""= exclude input"+-,.<>[]"
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to the site, and nice first answer! Would you mind editing in a link to an online interpreter such as TryitOnline, so that other's can test and verify your code? \$\endgroup\$ Nov 2 '20 at 0:37
1
\$\begingroup\$

Julia 1.0, 14 bytes

in("+-,.<>[]")

Try it online!

some Julia functions will return an anonymous function when fed with not enough arguments, basically in(x) = y -> in(y,x). It would be a bit more interesting with but it costs 1 more byte.

\$\endgroup\$
1
\$\begingroup\$

Pxem (pxem.mktemp.emb.posixism, BREAK_LOOP_WHEN_INSUFFICIENT=1), filename only: 33 bytes.

The \0001 is an unprintable whose codepoint is 0x01.

[<>+-,.].i.t@.w.m.-\0001.yT.o.d.a.c.a

Try it online!

Accepts one character from stdin as an input. Outputs a T for truthy, nothing for falsey.


Pxem (pxem.mktemp.emb.posixism, BREAK_LOOP_WHEN_INSUFFICIENT=0), filename only: 42 bytes.

The \0001 is an unprintable whose codepoint is 0x01.

[<>+-,.].i.t@.w.c.c.z.d.a.m.-\0001.yT.o.d.a@.a

Try it online!

Accepts one character from stdin as an input. Outputs a T for truthy, nothing for falsey.

\$\endgroup\$
1
\$\begingroup\$

Factor, 17 bytes

[ "+-,.<>[]"in? ]

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Ly, 14 bytes

"+-,.()[]"i~u>

Try it online!

"+-,.()[]"      - Push legit characters onto the stack
          i     - Read codepoint from STDIN
           ~    - Pop stack, search stack for that codepoint, push 1|0 result
            u   - Print top of stack as a number (will be 1 or 0)
             >  - Switch stacks to avoid printing the current contents
\$\endgroup\$
1
\$\begingroup\$

jq, 18 bytes

inside("+-,.<>[]")

Try it online!

  • Fix+4 thanks to @JoKing
\$\endgroup\$
6
  • \$\begingroup\$ Bounty started. By the way, this is 100 because SE has silly rules saying that if you've posted an answer, the minimum bounty is +100. \$\endgroup\$
    – emanresu A
    Sep 2 '21 at 8:56
  • 3
    \$\begingroup\$ The output is always true for every input. \$\endgroup\$ Sep 3 '21 at 0:07
  • 3
    \$\begingroup\$ You should be using inside \$\endgroup\$
    – Jo King
    Sep 4 '21 at 7:54
  • \$\begingroup\$ I'll award the bounty once this answer becomes valid. \$\endgroup\$
    – emanresu A
    Sep 8 '21 at 19:49
  • \$\begingroup\$ @emanresuA fixed thx to jo king, if you have already lost 100 rep for prev. Bounty you are free to not award the bounty again \$\endgroup\$
    – wasif
    Sep 9 '21 at 7:41
0
\$\begingroup\$

C (gcc) XPG4.2/Single UNIX Specification, Version 3, POSIX:2001, 30 bytes

f(c){c=!!index("+-,.<>[]",c);}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ nitpick: index() isn't included by the C standard, so shouldn't it be stated? \$\endgroup\$ Apr 11 '20 at 18:17
  • 1
    \$\begingroup\$ @FryAmTheEggman Ah, well deciphered! Ok, posted the version of POSIX it's good to go on. Thanks! :-) \$\endgroup\$
    – Noodle9
    Apr 11 '20 at 18:40
  • \$\begingroup\$ This is totally not necessary. C (gcc) works just fine. \$\endgroup\$
    – S.S. Anne
    Apr 23 '20 at 20:44
0
\$\begingroup\$

C (gcc), 31 bytes

f(c){c=!!strchr("+-,.<>[]",c);}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ruby -nl, 16 bytes

Input is STDIN. Prints 0 (Ruby truthy) or nil (falsy).

Annoyingly, it seems like Ruby demands that both [ and ] be escaped in the character class regex.

p~/[+-,.<>\[\]]/

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ That's apparently because Ruby allows nested character classes. \$\endgroup\$
    – Neil
    Apr 12 '20 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.