8
\$\begingroup\$

Partially inspired by this question.

Overview

The execution of Goggle kids Code starts with an empty binary tape with 100 items, with the pointer starting at the index 0:

00000...
^

At the end of the execution, you can assume that the tape is implicitly outputted.

Instructions

Goggle kids Code is a simple language involving a stack-like tape.

  • /: Move the pointer right.
  • \: Move the pointer left.
  • >: Take two items from the tape, the left item and the current item. Zero the current item, move left, and set the item as 1 if left is larger than current, else 0.
  • =: Similar to >, but performs an equality comparison instead.

The only looping structure in Goggle kids Code is the repeat loop, which takes a strictly positive number on the right introduced by a x symbol. (So things like (...)x0 is impossible here.) The syntax for this is:

(...)xI

The parentheses can only contain single instructions, although you can introduce other instructions with commas.

Sample program + Explanation

((/,>)x4,/)x13

(         )x13  Do this 13 times:
 (   )x4            Do this 4 times:
  /                    Move right
   ,                   After that,
    >                  Greater than
        ,           After that,
         /          Move pointer right.

Another example that does something useful (fills 13 items from the tape with 1's):

(   )x13    Do this 13 times:
 /              Move right
  ,             After that,
   =            Equality

Rules

  • You can assume the tape pointer never wraps around.
    • The test cases won't abuse out-of-bounds tape-moving.
  • To simplify the solution, you are allowed to output part of the tape, as long as that part is modified.
  • You are allowed to have a different instruction mapping, but if you do that, please make sure to clarify which mapping you used.
  • You could ignore checking the commas in the source code, they'll always be there.
    • Instructions are always separated by the comma.

Test cases

The trailing 0 digits are omitted here, for compactness of example outputs.

((/,>)x4,/)x13  -> 0000000000000 (doesn't modify the tape)

(/,=,/)x13      -> 1111111111111

(/,=,/,/)x13    -> 1010101010101010101010101

(/,=)x13,/,=    -> 00000000000000000

(/,/,=,\)x3     -> 0100
```
\$\endgroup\$
11
  • 5
    \$\begingroup\$ Also, a link to where we can learn more about "Goggle kids Code" is much appreciated. I have tried searching for the term, but couldn't find any thing relevant. I also can't see the linked question, it says 404 not found. \$\endgroup\$ May 15, 2020 at 5:30
  • \$\begingroup\$ Will the test case always have a loop out most? If not, suggest (/,=)x13,/,= as a test case. \$\endgroup\$
    – tsh
    May 15, 2020 at 5:43
  • 2
    \$\begingroup\$ Relevant: Google doodle - Turing machine. That's the closest thing I could find. \$\endgroup\$ May 15, 2020 at 6:50
  • 4
    \$\begingroup\$ So Google is teaching kids Brainfuck? I approve! \$\endgroup\$
    – Jonah
    May 15, 2020 at 12:28
  • \$\begingroup\$ @Jonah I guess Google is teaching Boolfuck instead of Brainfuck. \$\endgroup\$
    – user92069
    May 15, 2020 at 13:13

5 Answers 5

12
\$\begingroup\$

Python 2, 131 126 123 110 104 bytes

Saved 13 bytes thanks to @dingledooper's approach of flattening nested list using backtick.
Saved 2 bytes thanks to @l4m2

p,t=0,[0]*100
for i in`input()`:
 if"/"<i:i=int(i);p+=i/3-1
 if i<2:t[p:p+2]=cmp(*t[p:p+2])==i,0
print t

Try it online! or Verify all test cases

Takes input from STDIN, where /\>=x are replaced with 6210*

Big idea:

Python 2 input() implicitly evaluates the string read from STDIN. Thus, reading modified input like ((6,1)*4,2)*13 returns a nested tuple of integers, aka

((6, 1, 6, 1, 6, 1, 6, 1), 2, (6, 1, 6, 1, 6, 1, 6, 1), 2, ...)

Note that loops containing single instruction must be terminated with comma, e.g (1,)*10.

Now that all loops have been automatically unrolled for us, we just have to flatten the resulting tuple, and decode the instructions.

To flatten the nested tuple, we simply convert it to string, then takes all numerical characters.

\$\endgroup\$
8
  • \$\begingroup\$ @dingledooper Very clever approach, using back tick to flatten nested lists. Thanks! \$\endgroup\$ May 15, 2020 at 8:01
  • 1
    \$\begingroup\$ p+=i/3-1 using 6 as / \$\endgroup\$
    – l4m2
    May 15, 2020 at 8:18
  • 1
    \$\begingroup\$ @l4m2 I'm not sure about this, but since False is 0, I see no reason this is not allowed. \$\endgroup\$ May 15, 2020 at 9:12
  • 1
    \$\begingroup\$ but "False" is not "0" \$\endgroup\$
    – l4m2
    May 15, 2020 at 10:53
  • 1
    \$\begingroup\$ It's generally been allowed. \$\endgroup\$
    – xnor
    May 15, 2020 at 19:16
4
\$\begingroup\$

JavaScript (Node.js), 149 bytes

s=>eval(`[${s.replace(/ \d+/g,'.toString().repeat($&)')}]+6`).replace(/\d/g,c=>c>4?p+=c-6:(X[p-1]=X[p-1]-X[p]==c,X[p--]=0),X=Array(100).fill(p=0))&&X

Try it online!

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

Ruby, 89 bytes

t=[i=0]*100;(eval"[#$_]").flatten.map{|x|x<2?t[i-=1,2]=[x==t[i]-t[i+1]?1:0,0]:i+=x-3};p t

Try it online!

The translation map of the characters used for instructions is indicated in TIO header.

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

Retina 0.8.2, 135 bytes

,

\d+
$*
+`\([^()]+\)x|(?<=\(([^()]+)\)x1*)1
$1
^
0
{`(\d)/(\D*)((1)|0)?
$1$#4$2
(\d)\\(\D*)
$2$1
(?=(10>|(.)\2=))?.\d[>=](\D*)
$#1$+0

Try it online! Link includes test cases. Explanation:

,

Delete commas.

\d+
$*

Convert the multipliers to unary.

+`\([^()]+\)x|(?<=\(([^()]+)\)x1*)1
$1

Expand all repetitions from the inside out. (The last two stages can be performed by a single stage in Retina 1 at a saving of 19 bytes although you do then have to use ${3} instead of $+ in the last stage.)

^
0

Start with a tape with a 0. (The tape is automatically extended to the right as necessary, as this was slightly golfier than predefining the tape..)

{`

Process the instructions until they have all been executed.

(\d)/(\D*)((1)|0)?
$1$#4$2

Move right, extending the tape as necessary.

(\d)\\(\D*)
$2$1

Move left.

(?=(10>|(.)\2=))?.\d[>=](\D*)
$#1$+0

(In)equality. ($+ is used instead of $3 because the next character is a digit.)

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

JavaScript (Node.js), 235 bytes

p=>{s=[],l=Array(13).fill(i=q=0),e={'(':`s.unshift(1,i)`,')':`++s[0]<=parseInt(p.substr(i+1))?i=s[1]:s.splice(0,2)`,'/':`q++`,'\\':`q--`,'>':`l[q]=0,q--`,'=':`l[q]=0,q--,l[q]=+!l[q]`};while(i<p.length)eval(e[p[i++]]);return l.join('')}

Try it online!

JavaScript (Node.js), 231 bytes

p=>{s=[],l=Array(13).fill(i=q=0),e={'(':`s.unshift(1,i)`,')':`++s[0]<=parseInt(p.substr(i+1))?i=s[1]:s.splice(0,2)`,'/':`q++`,'\\':`q--`,'>':`l[--q]=0`,'=':`l[q]=0,l[--q]=+!l[q]`};while(i<p.length)eval(e[p[i++]]);return l.join('')}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.