2
\$\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\$
  • \$\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\$ – Redwolf Programs Apr 11 at 17:57
  • 2
    \$\begingroup\$ @RedwolfPrograms doesn't matterdoesn't matter \$\endgroup\$ – Krzysztof Szewczyk Apr 11 at 18:02
  • \$\begingroup\$ Is the input character guaranteed to be printable ASCII? (32-126) \$\endgroup\$ – Arnauld Apr 11 at 18:47
  • \$\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\$ – Krzysztof Szewczyk Apr 11 at 18:50
  • 1
    \$\begingroup\$ @LuisMendo No, according to the esolang wiki, it's often lowercase \$\endgroup\$ – Redwolf Programs Apr 12 at 15:56

22 Answers 22

15
\$\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).

| improve this answer | |
\$\endgroup\$
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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ <built-in method count of str object at 0x101520730>? \$\endgroup\$ – Victor VosMottor thanks Monica Apr 12 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 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 at 21:37
  • \$\begingroup\$ Anything that doesn't have a __str__ or a __repr__ has that. \$\endgroup\$ – PkmnQ Apr 14 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 ,.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I am now worried that the stuff that parses regular expressions is actually a backtracking regex engine... \$\endgroup\$ – my pronoun is monicareinstate Apr 12 at 3:37
4
\$\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.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ um err uh okay uh well then um uh yeah um sure \$\endgroup\$ – S.S. Anne Apr 23 at 20:57
3
\$\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!

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – Kevin Cruijssen Apr 14 at 7:59
  • \$\begingroup\$ PS: Your more interesting approach doesn't work for the inputs ;=\^: try it online. \$\endgroup\$ – Kevin Cruijssen Apr 14 at 8:01
3
\$\begingroup\$

Python 3, 26 19 bytes

input()in'+-,.<>[]'

Try it online!

edit: removed lambda to save 5 bytes

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ Is this really a valid format? \$\endgroup\$ – dingledooper Apr 12 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 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 at 12:32
  • \$\begingroup\$ @Dion See also my tip Object method as answer. \$\endgroup\$ – xnor Apr 13 at 21:59
  • \$\begingroup\$ This isn't a valid answer; it's an expression, not a function. \$\endgroup\$ – FlipTack May 30 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.

| improve this answer | |
\$\endgroup\$
2
\$\begingroup\$

Python 3, 26 bytes

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

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 25 23 bytes

-2 thanks to @Arnauld

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

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ 23 bytes \$\endgroup\$ – Arnauld Apr 11 at 20:01
  • \$\begingroup\$ @Arnauld Forgot about .test, thanks! \$\endgroup\$ – Redwolf Programs Apr 11 at 20:16
1
\$\begingroup\$

Pyth, 10 bytes

/"+-,.<>[]

Try it online!

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Haskell, 18 bytes

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

Try it online!

Low-effort unnamed function.

| improve this answer | |
\$\endgroup\$
  • \$\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 at 21:39
  • \$\begingroup\$ @xnor ah I tried to do that without the flag, ty \$\endgroup\$ – clapp Apr 13 at 23:30
1
\$\begingroup\$

vJASS, 299 bytes

Prints T (True) and F (False).

//! zinc
library a{trigger t=CreateTrigger();function onInit(){TriggerRegisterPlayerChatEvent(t,Player(0),"",false);TriggerAddAction(t,function(){string s=GetEventPlayerChatString();if(s=="+"||s=="-"||s=="<"||s==">"||s=="."||s==","||s=="["||s=="]"){s="T";}else{s="F";}BJDebugMsg(s);});}}
//! endzinc

Readable Version:

//! zinc
library a{
   trigger t = CreateTrigger();

   function onInit(){
      // Create an INPUT EVENT
      TriggerRegisterPlayerChatEvent(t, Player(0), "", false);
      TriggerAddAction(t, function(){
         string s = GetEventPlayerChatString();

         if(s=="+"||
            s=="-"||
            s=="<"||
            s==">"||
            s=="."||
            s==","||
            s=="["||
            s=="]"
         ){
            s="T";
         }else{
            s="F";
         }

         BJDebugMsg(s);
      });
   }
}
//! endzinc

Output: enter image description here

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

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

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

Try it online!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ nitpick: index() isn't included by the C standard, so shouldn't it be stated? \$\endgroup\$ – Krzysztof Szewczyk Apr 11 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 at 18:40
  • \$\begingroup\$ This is totally not necessary. C (gcc) works just fine. \$\endgroup\$ – S.S. Anne Apr 23 at 20:44
0
\$\begingroup\$

C (gcc), 31 bytes

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Kotlin, 18 bytes

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

Try it online!

| improve this answer | |
\$\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!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ That's apparently because Ruby allows nested character classes. \$\endgroup\$ – Neil Apr 12 at 9:31
0
\$\begingroup\$

JS, 26 bytes

c=>'+-,.<>[]'.indexOf(c)+1

x86_16 machine code, 40 bytes

Outputs '0' (false) or '1' (true) on the screen.

bb1f 7cb1 5080 3f00 7409 380f 7409 83c3
01eb f2b0 30eb 02b0 31b4 0ecd 10eb fe2b
2d2c 2e3c 3e5b 5d00

To boot, pad it to 510 bytes with zeros and add 55aa at the end!
This means "uncomment the last two assembly lines and rebuild".

Ungolfed (compilable, compile using nasm -fbin -o is-bf is-bf.asm):

[org 0x7c00]

mov bx, string
mov cl, 'P' ; the character
loop_:
    cmp byte [bx], 0
    je false
    cmp byte [bx], cl
    je true
    add bx, 1
    jmp loop_
false:
    mov al, '0'
    jmp print
true:
    mov al, '1'
print:
    mov ah, 0eh
    int 10h
jmp $ ; hang forever



string: db "+-,.<>[]", 0
; times 510 - ($-$$) db 0
; dw 0xaa55
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ you don't have to make the assembly snippet bootable, you can aswell write a callable function \$\endgroup\$ – Krzysztof Szewczyk Apr 12 at 11:25
  • \$\begingroup\$ I like bootable snippets more because I can boot them in QEMU and see the results :) \$\endgroup\$ – Muskovets Apr 12 at 11:26
  • 2
    \$\begingroup\$ this makes your code bigger, though. \$\endgroup\$ – Krzysztof Szewczyk Apr 12 at 11:26
0
\$\begingroup\$

APL+WIN 12 bytes

3 bytes saved thanks to @Avi F. S.

Prompts for character and returns 1 if true 0 if false:

⎕∊'+-,.<>[]'

Try it online! Courtesy of Dyalog Classic

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Keeping in form with your answer, you can shave off the first ×, here. Though if you just flip them around, you can do ⎕∊'+-,.<>[]'`, here! \$\endgroup\$ – AviFS Apr 11 at 20:02
0
\$\begingroup\$

C (gcc), 28 bytes

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

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

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

LaTeX, 52 bytes

\usepackage{xstring}\def\f#1{\IfSubStr{[]+-.,}#1x{}}

Defined a macro \f that takes one argument and expands to x if the input is a brainfuck instruction and to nothing if not. Assumes there is only one character in the argument.

Explanation

\usepackage{xstring}  % load the xstring package for the IfSubStr macro
\def\f#1{             % define a macro \f that takes 1 argument
  \IfSubStr{[]+-.,}#1 % test if the second argument is a substring of the first
    x                 % if it is a substring expand to x
    {}                % else expand to nothing
}
| improve this answer | |
\$\endgroup\$

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.