Are circular tapes exciting?

32

1

A Brainfuck derivative

Let's define a simple Brainfuck-like programming language. It has a two-directional tape of cells, and each cell holds one bit. All bits are initially 0. There is a moving head on the tape, initially at position 0. A program is a string over the characters <>01!, executed from left to right, with the following semantics:

  • < moves the head one step to the left.
  • > moves the head one step to the right.
  • 0 puts 0 in the current cell.
  • 1 puts 1 in the current cell.
  • ! flips the current cell.

There are no loops, so a program of n characters terminates after exactly n steps. A program is boring if all cells contain 0 at the end of execution, and exciting if there is at least one 1. Note that the size of the tape is not specified, so depending on the implementation, it may be two-way infinite or circular.

An example program

Consider the program 1>>>!<<<<0>!>>>!. On an infinite tape, execution proceeds as follows:

     v
00000000000000  Put 1
     v
00000100000000  Move by >>>
        v
00000100000000  Flip
        v
00000100100000  Move by <<<<
    v
00000100100000  Put 0
    v
00000100100000  Move by >
     v
00000100100000  Flip
     v
00000000100000  Move by >>>
        v
00000000100000  Flip
        v
00000000000000

At the end, all cells are 0, so this program is boring. Now, let's run the same program on a circular tape of length 4.

v
0000  Put 1
v
1000  Move by >>>
   v
1000  Flip
   v
1001  Move by <<<< (wrapping around at the edge)
   v
1001  Put 0
   v
1000  Move by > (wrapping back)
v
1000  Flip
v
0000  Move by >>>
   v
0000  Flip
   v
0001

This time, there is a cell with value 1, so the program is exciting! We see that whether a program is boring or exciting depends on the size of the tape.

The task

Your input is a non-empty string over <>01! that represents a program in the above programming language. An array of characters is also an acceptable input format. The program is guaranteed to be boring when run on an infinite tape. Your output shall be the list of tape lengths on which the program is exciting. Note that you only need to test the program on tapes that are shorter than the program length.

The solution with the lowest byte count in each language is the winner. Standard rules apply.

Test cases

> : []
110 : []
1>0<! : [1]
0>>1>0<<>! : [1]
1>>>!<<<<0>!>>>! : [2, 4]
!<!<><<0>!>!<><1!>>0 : [2]
>>!>><>001>0<1!<<!>< : [1, 2, 3]
1!><<!<<<!!100><>>>! : [1, 3]
!!1>!>11!1>>0<1!0<!<1><!0<!<0> : [3, 4]
<><<>>!<!!<<<!0!!!><<>0>>>>!>> : [1, 2, 4]
0>>><!<1><<<0>!>>!<<!!00>!<>!0 : [3]
0000!!!!><1<><>>0<1><<><<>>!<< : []
!>!>!>!>!>1>!>0<!<!<!<0<!<0<!<!<!<1>!>0<<! : [1, 2, 5, 7]
<!!>!!><<1<>>>!0>>>0!<!>1!<1!!><<>><0<<!>><<!<<!>< : [1, 2, 4, 5]
!>1<<11<1>!>!1!>>>0!!>!><!!00<><<<0<<>0<<!<<<>>!!> : [1, 2, 3, 5, 6]

Zgarb

Posted 2018-02-23T17:54:52.143

Reputation: 39 083

1Can we choose any distinct and consistent characters instead of <>01!? – Mr. Xcoder – 2018-02-23T18:15:31.680

1Is an array of instructions an acceptable input? – Arnauld – 2018-02-23T18:21:36.553

@Mr.Xcoder No, you should use these exact characters. – Zgarb – 2018-02-23T18:24:00.987

@Arnauld An array of characters is close enough to a string, I'll allow it. – Zgarb – 2018-02-23T18:26:22.987

Answers

6

Haskell, 119 bytes

t#'<'=last t:init t
(h:t)#c|c<'#'=1-h:t|c>'='=t++[h]|1<2=read[c]:t
f p=[n|n<-[1..length p],sum(foldl(#)(0<$[1..n])p)>0]

Try it online!

Function # is the interpreter for a single command c. The whole program p is run by folding # with the starting tape into p. f executes p for every tape and keeps those where the sum of the cells is at least 1.

nimi

Posted 2018-02-23T17:54:52.143

Reputation: 34 639

n<-[1..length p] ... 0<$[1..n] seems quite long, there must be a shorter way. – nimi – 2018-02-23T20:49:37.080

I can't see a shorter way. The problem I see is that you actually need the value of n as the result, so if you constructed 0<$[1..n] a different way (say with scanr(:)), you'd need to take the length of it. (I also tried using 1 (to replace length with sum) or False (to use or for the test) instead of 0, but it didn't come out shorter.) – Ørjan Johansen – 2018-02-24T02:07:12.777

@ØrjanJohansen: yes, I tried n<-init$scanr(:)[]$0<$p ... n which is 2 bytes shorter, but it returns a list of starting tapes instead of their length, e.g.[[0],[0,0,0]]. With a little bit of rule bending so could see the tapes as unary numbers, so maybe it's ok. – nimi – 2018-02-24T11:05:13.557

init$ can be replaced by putting a [0] as initial list, but it still didn't get short enough. I think unary is only allowed for languages without a more natural number representation. – Ørjan Johansen – 2018-02-24T19:56:52.183

4

Stax, 56 54 43 38 35 bytesCP437

è¥%►BΣ░ÜY⌂y(â&.═ªê►V½▲y▌)▀♫♂╣ª?√»!#

42 bytes when unpacked,

%fz(y{{|(}{|)}{B!s+}{0_]e&}4ls"><! "I@!F|a

Run and debug online!

-2 bytes per comment by @recursive

Explanation

I will use the version with a prefix i(i.e. i%fz(y{{|(}{|)}{B!s+}{0_]e&}4ls"><! "I@!F|a) to explain and I will explain why the i can be removed

i               Suppress implicit eval
                    This prevents the test case "110" from being interpreted as a number
                    However, this can be removed because a program containing only numbers cannot be exciting and the output will be empty anyway.
                    This is based on the fact that the program is boring on non-circular tapes
 %f             Filter range [1..n] with the rest of this program
                    Where n is the length of the input
                    Implicit output the array after filtering, one element per line
   z(           Initialize the tape
     y{  F      Run the program
          |a    Any cell is non-zero

Code for running the program:

{|(}                                 Block to rotate left by one element
    {|)}                             Block to rotate right by one element
        {B!s+}                       Block to perform logical not on the element at index 0
              {0_]e&}                Block to obtain current instruction,
                                         Convert it to a number
                                         And assign to element at index 0

                     4l              Pack the 4 blocks in an array
                       s"<>! "I      Find the index of current instruction in string, if not found, the index will be -1
                                         And when indexed with -1, it wraps around to the 4th element.

                               @!    And execute the corresponding block.

Weijun Zhou

Posted 2018-02-23T17:54:52.143

Reputation: 3 396

1I added a test case of all digits to validate your i check. – Zgarb – 2018-02-23T21:45:38.883

0]* can be replaced with z(. Also, if you change the string to "<>!", then 0 and 1 will give index -1, so that way your block list only needs 4 blocks, instead of 5. This will work since the 0 and 1 handlers are identical anyway. – recursive – 2018-02-28T07:31:29.030

@recursive Good point taken. – Weijun Zhou – 2018-02-28T08:26:10.667

3

CJam, 57 56 49 46 bytes

-7 thanks to @MartinEnder

{:T,({)0a*T{i23%")!+(+ W0tW1t)\+"3/=~}/:+},:)}

Try it online!

Esolanging Fruit

Posted 2018-02-23T17:54:52.143

Reputation: 13 542

3

Perl 5, 83 82 79 77 bytes

Includes +3 for -F

Give instructions as one line on STDIN

#!/usr/bin/perl -F
1~~@$m&&say$m until++$m>map{*r=\$$m[($n+=/>/-/</)%$m];$r=/\d/?$&:$r^/!/}@F

Try it online!

Ton Hospel

Posted 2018-02-23T17:54:52.143

Reputation: 14 114

Flags now count as part of the language instead of scoring.

– Ørjan Johansen – 2018-02-25T00:56:04.447

2

JavaScript (ES6), 126 118 bytes

Saved 3 bytes thanks to @user71546

Takes input as an array of 1-character strings.

f=(s,l=0,p=0,t=[])=>s[l++]?s.map(c=>1/c?t[p%l]=+c:c>'='?p++:c>';'?p+=l-1:t[p%l]^=1)&&+t.join``?[l,...f(s,l)]:f(s,l):[]

Try it online!

Arnauld

Posted 2018-02-23T17:54:52.143

Reputation: 111 334

Replacing t.some(x=>x)? by +t.join``? instead check the array as digits (and 0 indicates an all-zero tape), but 3 bytes less. – Shieru Asakoto – 2018-02-26T01:08:51.837

2

Python 2, 139 135 133 132 131 bytes

-3 bytes thanks to Mr. Xcoder

def f(p):i=0;exec"i+=1;s=[0]*i\nfor c in p:c=ord(c)-61;s=c>-2and s[c:]+s[:c]or[[s.pop(0)^1,0,1][c%7]]+s\nif 1in s:print i\n"*len(p)

Try it online!

Rod

Posted 2018-02-23T17:54:52.143

Reputation: 17 588

131 bytes? – Mr. Xcoder – 2018-02-23T21:30:23.603

2

Perl 5 (-F), 101 bytes

map{$,=@a=(0)x$_;map{$,+=/>/-/</;$a[$,%@a]=$&if/0|1/;$a[$,%@a]=!$a[$,%@a]if/!/}@F;say if@a~~/1/}1..@F

Try it online!

Xcali

Posted 2018-02-23T17:54:52.143

Reputation: 7 671

2

Red, 243 bytes

func[p][repeat n length? p[b: copy[]insert/dup b 0 n i: 1
parse p[any["<"(i: i - 1 if i < 1[i: n])|">"(i: i + 1 if i > n[i: 1])|"0"(b/(i): 0)|"1"(b/(i): 1)|"!"(b/(i): either b/(i) = 0[1][0])|
skip]]s: 0 foreach c b[s: s + c]if s > 0[print n]]]

Try it online!

Prety verbose and straightforward implementation. Red's 1-indexing doesn't allow me to reduce the byte count by using modular arithmetic for looping through the circular tapes.

Ungolfed

f: func[p][ 
    repeat n length? p[
        b: [] 
        insert/dup b 0 n
        i: 1
        parse p[
            some [
                 "<" (i: i - 1 if i < 1[i: n])
               | ">" (i: i + 1 if i > n[i: 1])
               | "0" (b/(i): 0)
               | "1" (b/(i): 1)
               | "!" (b/(i): either b/(i) = 0 [1][0])
               | skip 
            ]
        ]
        s: 0
        foreach c b[s: s + c]
        if s > 0 [print n]
    ]
]

Galen Ivanov

Posted 2018-02-23T17:54:52.143

Reputation: 13 815

2

APL (Dyalog Unicode), 79 64 54 bytes (Adám's SBCS)

⍸⊂{∨/⍎⍕(↓',',⍨5 3⍴'0@11@1~@1 1⌽¯1⌽')['01!<'⍳⌽⍺]⍵}¨0=,\

Try it online!

-15 thanks to Adám (forgot about monadic ).
-10 thanks to ngn.

Erik the Outgolfer

Posted 2018-02-23T17:54:52.143

Reputation: 38 134

64 – Adám – 2018-03-08T22:11:39.157

@Adám Hm, looks like that's not optimal (for example you don't need the ). I'll look into it and update. :) – Erik the Outgolfer – 2018-03-09T12:27:18.297

But if you remove the you'll need a ;, no? – Adám – 2018-03-09T12:34:28.410

@Adám no, why would you?

– Erik the Outgolfer – 2018-03-09T12:35:22.097

Because rank-2 arrays need two index parts.

– Adám – 2018-03-09T12:41:20.717

@Adám Uh, I did the change on the wrong function. >_< (also why did I use '⊢,' instead of ,, while , already separates >>>___<<<) – Erik the Outgolfer – 2018-03-09T12:42:56.347

so many opportunities for improvement: ⍸⊂{∨/⍎⍕(↓',',⍨5 3⍴'0@11@1~@1 1⌽¯1⌽')['01!<'⍳⌽⍺]⍵}¨0=,\ – ngn – 2018-03-09T22:44:04.657

@ngn yeah, looks like "opportunities" has a different meaning for the rest of us :P – Erik the Outgolfer – 2018-03-10T11:25:21.717

2

Retina, 121 bytes

.+
$.&*0¶$&
\G0
0$`¶
{ms`^.(?=.*¶¶(0|1))
$1
"¶¶!"&mT`d`10`^.
"¶¶>"&`(.)(.*)¶
$2$1¶
"¶¶<"&`(.*)(.)¶
$2$1¶
)`¶¶.
¶¶
G`1
%`.

Try it online! Explanation:

.+
$.&*0¶$&
\G0
0$`¶

Create an array of tapes of each length up to the length of the input program.

{

Loop until the program is consumed.

ms`^.(?=.*¶¶(0|1))
$1

If the next character in the program is a 0 or 1, change the first character on each line to that character.

"¶¶!"&mT`d`10`^.

If it is a ! then toggle the first character on each line.

"¶¶>"&`(.)(.*)¶
$2$1¶
"¶¶<"&`(.*)(.)¶
$2$1¶

If it is a > or < then rotate the line. (Easier than moving the head.)

)`¶¶.
¶¶

Delete the instruction and end the loop.

G`1

Keep only the exciting lines.

%`.

Count the length of each line.

Neil

Posted 2018-02-23T17:54:52.143

Reputation: 95 035

1

MATL, 46 39 bytes

f"@:~G"@59>?@61-YS}@33=?t1)~}@U]1(]]a?@

Try it online! Or verify all test cases.

How it works

f             % Push indices of nonzero chars of (implicit) input string: gives
              % [1 2 ... n] where n is input length
"             % For each k in [1 2 ... n]. These are the possible tape lengths
  @:~         %   Push array of k zeros. This is the tape, in its initial state
  G           %   Push input string
  "           %   For each char in the input string
    @59>?     %     If code point of current char exceeds 59 (so it is '<' or '>')
      @61-    %       Push code point minus 61: gives -1 for '<', or 1 for '>'
      YS      %       Circularly shift the tape by that amount. Instead of moving
              %       the head, we shift the tape and keep the head at entry 1
    }         %     Else
      @33=?   %       If code point of current char is 33 (so it is '!')
        t1)   %         Duplicate the array representing the tape, and get its
              %         first entry
        ~     %         Logical negate
      }       %       Else
        @U    %         Push current char (it is '0' or '1') converted to number
      ]       %       End
      1(      %       Write (either 0, 1 or old value negated) at entry 1
    ]         %     End
  ]           %   End
  a?          %   If the tape contains at least a nonzero value
    @         %     Push tape length, k
              %   End (implicit)
              % End (implicit)
              % Display (implicit)

Luis Mendo

Posted 2018-02-23T17:54:52.143

Reputation: 87 464

1

APL (Dyalog Unicode), 192 78 bytes

⊂{t/⍵⊣⍵{t[m]←('01!'⍳⍵)⊃0 1,e,⍨~e←t[m←⍺|i⊣i+←¯1 1 0⊃⍨'<>'⍳⍵]}¨⍺⊣i←⊃t←⍬⍳⍺}¨1+⍳∘≢

Try it online! (non-flattened result)

Try it online! (flattened)

After some time spent banging my head against the wall, I decided to make a Tradfn instead of a Dfn. This is the result. Smarter people than me may be able to golf the heck out of this.

Surprise, surprise, someone smarter than me did golf the heck out of this. Thank you Adám for 114 bytes.

He said:

Notice that it is your exact program, except using indexing instead of the inner :Ifs and collapsing :For-loops to {_}¨ while giving a left argument to replace the global.

The function assumes ⎕IO←0.


How?

(This explanation uses an "ungolfed" version to facilitate reading)

⊂{                                  ⍝ Enclose
      i←⊃t←⍬⍳⍺                      ⍝ Assign a vector of 0s to t (the tape), then assign the first 0 to i.
      t/⍵⊣⍵{                        ⍝ Use ⍵ as left argument for the nested function, then compress the result into t. If there is a 1 anywhere in t, the result will be a vector of the result. If not, the result is an empty vector.
          i+←¯1 1 0⊃⍨'<>'⍳⍵         ⍝ Map the string '<>' to the argument (which is the BF program). That yields 0 for <, 1 for >, and 2 for anything else.
                                    ⍝ The resulting vector will then be used as the argument for ⊃ to add -1 (index 0), 1 (index 1) or 0 (index 2) to the variable i.
          e←t[m←⍺|i]                ⍝ Assign i mod ⍺ (left arg) to m, and use it to index t. Then, assign the value to e.
          t[m]←('01!'⍳⍵)⊃0 1,e,⍨~e  ⍝ Map the string '01!' to ⍵. As before, this yields 0 for 0, 1 for 1, 2 for ! and 3 for anything else.
                                    ⍝ Then, concatenate (not e) with e, then concatenate that with the vector 0 1. This is used as argument to be picked from, and it is assigned to t[m].
      }¨⍺                           ⍝ Do that for each argument
  }¨1+⍳∘≢                           ⍝ And do that for each possible tape length from 1 to the length of the input.

J. Sallé

Posted 2018-02-23T17:54:52.143

Reputation: 3 233

1Save one byte by making t←l⍴0 be t←l⍴i←0, and removing the line above it. You can also save another by changing t[i|⍨≢t]←1-t[i|⍨≢t] to t[i|⍨≢t]←~t[i|⍨≢t]. – Zacharý – 2018-03-08T18:24:29.220

2

@Zacharý right, and further save an additional 112 bytes. Exactly same code, just golfed a bit.

– Adám – 2018-03-08T20:09:48.827

Yeah, it's just goled "a bit". Don't you need the s? – Zacharý – 2018-03-08T22:55:04.450

@Zacharý What s? It is a tacit function. – Adám – 2018-03-09T10:34:43.823

@Zacharý I would consider this one pretty Adám'd, wouldn't you? – J. Sallé – 2018-03-09T13:42:58.507

That comment was BEFORE @J.Sallé updated it, and it's been Erik'd, Adám'd, and then Adám'd again... – Zacharý – 2018-03-09T16:12:21.877

0

Jelly, 41 bytes

JR0ṁð3“1⁺¦01¦¬1¦ṙ1 ṙ- ”s“10!<>”,yẎvЀ⁸Ẹ€T

Try it online!

Erik the Outgolfer

Posted 2018-02-23T17:54:52.143

Reputation: 38 134

0

C (clang), 171 bytes

l,i;f(S){for(char*p,t[l=strlen(S)];l;memchr(t,1,l)&&printf("%d ",l),l--)for(memset(t,i=0,l),p=S;*p;p++)*p==60?i=i?i-1:l-1:*p==62?i=i^l-1?i+1:0:*p^33?t[i]=*p-48:(t[i]^=1);}

Try it online!

Had to use clang, since using char*p,t[l=strlen(S)] as the initialisation expression for some reason makes GCC think I want to declare strlen instead of calling it.

Pretty straight-forward: Runs the program on circular tapes of decreasing length, outputting any length that resulted in a 1 somewhere on the tape.

Tried shortening the tangle of the ternary operators, but ended up needing more parenthesis than was healthy.

gastropner

Posted 2018-02-23T17:54:52.143

Reputation: 3 264

Suggest i=0,bzero(t,l) instead of memset(t,i=0,l) and *p-62?t[i]=*p^33?*p-48:t[i]^1:(i=~i+l?i+1:0) instead of *p==62?i=i^l-1?i+1:0:*p^33?t[i]=*p-48:(t[i]^=1) – ceilingcat – 2018-12-20T01:09:29.340