Playing with the musical turtle

My two kids like to play with the following toy:

The colored areas with the shapes inside can be touched and the turtle then lights the area and plays a sound or says the name of the color or the shape inside. The middle button changes the mode. There is one mode in which the areas play different musical notes when touched, with a twist: if the kid touches three consecutive areas clockwise, a special melody 1 is played. If the three consecutive areas touched are placed counterclockwise, a special melody 2 is played.

The challenge

Let's simulate the internal logic of the toy. Given a string with 3 presses of the kid, return two distinct, coherent values if the three presses are for consecutive areas (clockwise or counterclockwise) and a third distinct value if they are not.

Details

• The input areas will be named with a character each, which can be their color: ROYGB for red, orange, yellow, green and blue; or their shape: HSRTC for heart, square, star (R), triangle and circle. Case does not matter, you can choose to work with input and output just in uppercase or in lowercase.
• The program will receive a string (or char array or anything equivalent) with three presses. Examples (using the colors): RBO, GYO, BBR, YRG, YGB, ORB...
• The program will output three distinct, coherent values to represent the three possible outcomes: a first value if the combination does not trigger a special melody, a second value if the combination triggers the clockwise special melody, and a third value if the combination triggers the counterclockwise special melody. Example: 0 for no special combination, 1 for the melody triggered by a clockwise combination and -1 for the melody triggered by a counterclockwise combination.
• You do not need to worry about handling wrong input.

Test cases

Input   Output      // Input based on colors
--------------
RBO     0           // No special combination
GYO     -1          // Counterclockwise melody triggered
BBR     0           // No special combination
YRG     0           // No special combination
YGB     1           // Clockwise melody triggered
ORB     -1          // Counterclockwise melody triggered
OOO     0           // No special combination
BRO     1           // Clockwise melody triggered

This is , so may the shortest code for each language win!

• Is [0,0], [1,0], [0,1] allowed as output? I see the Mathematica answer doing that, and it would save 3 bytes in the 05AB1E answer. – Kevin Cruijssen Oct 23 '18 at 14:52
• @KevinCruijssen of course you can. The output values can be anything as long as they are distinct and consistent. – Charlie Oct 23 '18 at 14:55

Java 8, 4839 33 bytes

s->"ROYGBRO BGYORBG".indexOf(s)|7

-6 bytes thanks to @RickHitchcock, so make sure to upvote him as well!

Takes uppercase color as input-String. Outputs -1 for none, 7 for clockwise, and 15 for counterclockwise.

Try it online.

Explanation:

s->      // Method with String parameter and integer return-type
"ROYGBRO BGYORBG".indexOf(s)
//  Get the index of the input in the String "ROYGBRO BGYORBG",
//  which will result in -1 if the input is not a substring of this String
|7   //  Then take a bitwise-OR 7 of this index, and return it as result

s->(char)"ROYGBRO BGYORBG".indexOf(s)/7

Takes uppercase color as input-String. Outputs 9362 for none, 0 for clockwise, and 1 for counterclockwise.

Try it online.

Explanation:

s->      // Method with String parameter and integer return-type
(char)"ROYGBRO BGYORBG".indexOf(s)
//  Get the index of the input in the String "ROYGBRO BGYORBG",
//  which will result in -1 if the input is not a substring of this String
//  And cast it to a char (-1 becomes character with unicode value 65535)
/7   //  Integer-divide it by 7 (the char is implicitly converted to int doing so)
• I think 9632 is the best distinct and consistent value I've seen in these answers so far :) – Misha Lavrov Oct 23 '18 at 15:25
• @MishaLavrov It could also be 10922 (integer-division by /6) or 8191 (integer-division by /8), but I choose /7 because it's the index of the space in the String. :) – Kevin Cruijssen Oct 23 '18 at 15:29
• @RickHitchcock Thanks! I thought I had tried some bitwise operations, but apparently not properly enough.. – Kevin Cruijssen Oct 23 '18 at 20:19

JavaScript (ES6), 41 bytes

Takes color initials as input. Returns 2 for none, true for clockwise or false for counterclockwise.

s=>~(x='ROYGBRO_ORBGYOR'.search(s))?x<5:2

Try it online!

• At least an answer from Arnauld I can understand!! :) – Charlie Oct 23 '18 at 13:49
• @Charlie I think Arnauld is sick or something.. Not only is his answer readable and easy to understand, it's even longer than my Java answer! o.Ô Something is clearly wrong here. ;p – Kevin Cruijssen Oct 23 '18 at 15:34
• s=>('ROYGBRO_ORBGYOR'.search(s)+8)/8|0 – l4m2 Oct 23 '18 at 16:01
• Do you even royg, bro? – Quentin Oct 24 '18 at 14:24

Python 2, 36 bytes

lambda i:'ROYGBRO ORBGYOR'.find(i)/7

-1 - None
0 - Clockwise
1 - Counterclockwise

Try it online!

• Heh, great minds (or something)... codegolf.stackexchange.com/a/174635/53748 (if I'd seen this before trying stuff myself, I'd just have given you the byte save :p) – Jonathan Allan Oct 24 '18 at 12:33
• @JonathanAllan Don't worry, stuff happens :D I've upvoted your answer, as I can't dislike it! – Dead Possum Oct 24 '18 at 13:52

Excel, 29 bytes

=FIND(A1,"ROYGBRO_RBGYORB")<6

Uppercase colours as input.

Returns #VALUE! for no pattern, TRUE for clockwise, FALSE for anti-clockwise.

Can wrap in IFERROR( ,0) for +11 bytes to handle exception , and return '0' for no-pattern cases instead.

05AB1E, 15 11 bytes

Saved 4 bytes thanks to Kevin Cruijssen and Magic Octopus Urn.

Uses shapes.
Output [0, 0] for none, [1, 0] for clockwise and [0, 1] for counter-clockwise

Â‚.•ÌöJη•så

Try it online! or as a Test suite

Explanation

Â‚            # pair the input with its reverse
.•ÌöJη•     # push the string "hsrtchs"
så   # check if the input or its reverse is in this string
• Very similar as my 15-byte answer that I was about to post: .•1´₃éC•Â‚εXå}¥ resulting in [0], [1], or [-1], so +1 from me. Btw, I think (not sure) you can save three bytes by removing the last three, outputting [0, 0], [1, 0], and [0, 1] as distinct values. I see that the Mathematica answer does the same. Will ask OP for verification. – Kevin Cruijssen Oct 23 '18 at 14:52
• Just asked OP, and you are indeed allowed to drop the last three bytes. As long as the three outputs are consistent and distinct it's fine whatever it is. – Kevin Cruijssen Oct 23 '18 at 14:56
• Â‚.•ÌöJη•så is 11 bytes if [1,0], [0,1] and [0,0] are considered unique (unless I'm missing something obvious here, adding ƶO to the end of that 11-byter is still 13 for the same 0, 1, 2 answer you have now). Test cases. Test cases 2. Order reversal removes the need for the loop. – Magic Octopus Urn Oct 24 '18 at 16:12
• According to the comment on the original thread the 11-byter should be fine, as it uses 3 distinct values, you can beat Jelly with that one :). – Magic Octopus Urn Oct 24 '18 at 16:18
• @KevinCruijssen Thank you! I always seem to think of unique as "single digit" for no reason :) – Emigna Oct 26 '18 at 8:11

JavaScript (ES6), 32 bytes

s=>'ROYGBRO_ORBGYOR'.search(s)|7

Returns -1 if no combination, 15 if counterclockwise, 7 if clockwise.

let f =
s=>'ROYGBRO_ORBGYOR'.search(s)|7

console.log(f('RBO')) // -1
console.log(f('GYO')) // 15
console.log(f('BBR')) // -1
console.log(f('YRG')) // -1
console.log(f('YGB')) // 7
console.log(f('ORB')) // 15
console.log(f('OOO')) // -1
console.log(f('BRO')) // 7

Wolfram Language (Mathematica), 42 36 bytes

{"ROYGBRO","ORBGYOR"}~StringCount~#&

Try it online!

Counts the number of times the input appears in both "ROYGBRO" and "ORBGYOR". Returns {1,0} for clockwise, {0,1} for counterclockwise, and {0,0} for no special combination.

At the cost of only one more byte, we can get outputs of 0 for nothing, 1 for clockwise, and 2 for counterclockwise with "ROYGBRO.ORBGYORBGYOR"~StringCount~#&.

x86 machine code, 39 36 bytes

00000000: f30f 6f11 b800 0000 0066 0f3a 6310 0c89  ..o......f.:c...
00000010: c883 c807 c352 4f59 4742 524f 2042 4759  .....ROYGBRO BGY
00000020: 4f52 4247                                ORBG

Assembly:

section .text
global func
func:					;the function uses fastcall conventions
;no stack setup needed because we don't need to use stack
movdqu xmm2,[ecx]		;Move DQword (16 bytes) from 1st arg to func(ecx) to SSE reg
PcmpIstrI xmm2, [eax], 1100b	;Packed Compare String Return Index, get idx of [eax] in xmm2
mov eax, ecx			;Move returned result into reg eax
or eax, 7			;Bitwise OR eax with 7 to get consistent values
section .data
msg db 'ROYGBRO BGYORBG'

Try it online!

Output is 23 for none, 7 for clockwise, and 15 for counter-clockwise. Based on @RickHitchcock 's answer.

Saved 3 bytes by using an SSE string comparison instruction instead of using libc.

• Can you count the machine code bytes here, since those bytes rely on the plafement of strstr in the binary? You can't just take those same bytes in another context and expect them to work the same. – Robert Fraser Oct 24 '18 at 4:55
• There are pure machine code replacements that dont rely on the standard library strchr.com/strcmp_and_strlen_using_sse_4.2 – Robert Fraser Oct 24 '18 at 4:59
• @RobertFraser The placement of the function is resolved during linking, the byte count will always be the same because the address of the function strstr will always be a 32-bit (4 byte) address. The machine code in my post is unlinked. – Logern Oct 24 '18 at 10:52
• I guess it's kind of a gray area. The function relies on data in the object file (linker map) before it can be used. But you could argue that's no different than a Java lambda needing a type declaration. – Robert Fraser Oct 24 '18 at 17:02
• Note to be fully compliant with the ABI you'd have to use xmm5 or xmm6 which are caller-saved registers, instead of xmm2 which is callee-saved (but that shouldnt cost any bytes). – Robert Fraser Oct 25 '18 at 10:42

Perl 5-p, 30 bytes

$_=ROYGBRO=~/$_/-ORBGYOR=~/$_/ Try it online! APL(Dyalog), 22 18 bytes +/(⍷-⍷∘⌽)∘'ROYGBRO' -4 bytes thanks to @ngn Takes a string of color initials. Outputs 0 for no special pattern, -1 for counterclockwise, 1 for clockwise. Try it online! Python 2, 45 43 bytes lambda i,a='ROYGBRO':(i in a)-(i[::-1]in a) Try it online! With ideas from and serious credit to @DeadPossum -2 with thanks to @JoKing. Now outputs -1 = counterclockwise, 0 = none, 1 = clockwise. My original effort is below for historical purposes. Python 2, 52 51 bytes lambda i,a='ROYGBRO':((0,1)[i[::-1]in a],2)[i in a] Try it online! 0 = none, 1 = counterclockwise, 2 = clockwise • 45 bytes – Dead Possum Oct 23 '18 at 15:20 • @DeadPossum That is seriously cool and different enough from mine for you to post as your own answer. I will upvote. – ElPedro Oct 23 '18 at 17:32 Python 2, 35 36 bytes +1 - for some reason I thought all buttons would be distinct >_< Developed independently from what I have just seen (and now up-voted) by Dead Possum lambda s:'ORBGYO.BROYGBR'.find(s)/7 Try it online! • I think you need to separate the strings of characters. This returns 0 (anti-clockwise) for "RBR," which isn't in your list of combinations. – Rick Hitchcock Oct 24 '18 at 13:36 • @RickHitchcock ah I had assumed 3 different presses which is certainly incorrect. That'll make this effectively exactly the same as Dead Possum's! – Jonathan Allan Oct 24 '18 at 13:39 • Can't delete from mobile... will delete or address later – Jonathan Allan Oct 24 '18 at 13:39 • Yeah, it could be ambiguous, but one of the test cases ("OOO") shows that they could repeat. – Rick Hitchcock Oct 24 '18 at 13:41 • @RickHitchcock yep that's what I saw after your comment - thanks! – Jonathan Allan Oct 24 '18 at 13:42 Stax, 14 13 bytes ù♀e▌d─█£F'♦O▬ Run and debug it The output is • 1 for no special combination • 2 for counter-clockwise melody • 3 for clockwise melody Pip, 19 bytes Y"ROYGBRO"OaNyaNRVy Outputs 10 for clockwise, 01 for counterclockwise, 00 for neither. Try it online! Explanation a is 1st cmdline argument Y"ROYGBRO" Yank that string into y variable aNy Count of occurrences of a in y O Output without newline RVy y reversed aN Count of occurrences of a in that string Print (implicit) J, 21 bytes -&(OR@E.&'ROYGBRO')|. Try it online! How it works -&(OR@E.&'ROYGBRO')|. Monadic 2-verb hook. Input: a string. |. Map over [input string, input string reversed]: E.&'ROYGBRO' Find exact matches of input in 'ROYGBRO' (OR@ ) Reduce with OR; is it a substring of 'ROYGBRO'? -& Reduce with -; result is 1 for CW, -1 for CCW, 0 otherwise Achieves maximum amount of function reuse. Jelly, 12 bytes ,Ṛẇ€“¤ƈẎṬ%Ȥ» Try it online! -4 thanks to Jonathan Allan. Clockwise: [1, 0] Counter-clockwise: [0, 1] Other: [0, 0] • @JonathanAllan Phew, thanks. Just saw again what I did, you were just 15 hours quicker. :P – Erik the Outgolfer Oct 24 '18 at 14:41 R, 38 bytes grep(scan(,''),c('ROYGBRO','ORBGYOR')) Try it online! Returns : • No special combination : integer(0) • Counterclockwise melody triggered : 2 • Clockwise melody triggered : 1 Charcoal, 18 bytes ≔ROYGBROηＩ⁻№ηθ№⮌ηθ Try it online! Link is to verbose version of code. Explanation: ROYGBRO Literal string ≔ η Assign to variable η η Value of variable ⮌ Reversed θ θ Input string № № Count matches ⁻ Subtract Ｉ Cast to string Implicitly print Common Lisp, 68 bytes (defun x(s)(cond((search s "ROYGBRO")1)((search s "BGYORBG")-1)(0))) Try it online! Clean, 63 bytes import StdEnv,Text$s=map((>)0o indexOf s)["ROYGBRO","BGYORBG"]

Try it online!

[True, True] for no special noises, [True, False] for counterclockwise, [False, True] for clockwise.

Japt, 17 14 bytes

Takes the colours as input, in lowercase. Returns 0 for clockwise, 1 for counterclockwise or -1 if there's no combo.

ygßê qÔbøU

Try it

Expanation

...            :Compressed string "roygbrow"
ê           :Palindromise
qÔ        :Split on "w"
b       :Index of the first element
ø      : That contains
U     :  The input string

Ruby, 53 36 bytes

->n{"\a\fDch+".index(""<<n%111)&./3}

Try it online!

Input: a 3-digit integer, where the digits represent colors:

• 1 - red
• 2 - orange
• 3 - yellow
• 4 - green
• 5 - blue

Output:0 for clockwise, 1 for counterclockwise, nil otherwise.

C (gcc), 55 bytes

Returns 0 for none, -1 for CCW and 1 for CW.

f(char*s){s=!strstr("ORBGYOR",s)-!strstr("ROYGBRO",s);}

Try it online!