| Bytes | Lang | Time | Link |
|---|---|---|---|
| 091 | Tcl | 180403T102013Z | sergiol |
| 074 | Red | 210604T170511Z | 9214 |
| 081 | Factor | 210601T153554Z | chunes |
| 113 | Swift | 180322T151552Z | user3151 |
| 063 | Bash + GNU Utilities | 180321T174954Z | Digital |
| 089 | Lua | 180323T170823Z | ivzem |
| 084 | Wolfram Language Mathematica | 180323T143309Z | Kelly Lo |
| 093 | R | 180322T091435Z | DS_UNI |
| 072 | sed | 180322T074044Z | lucasb |
| 085 | Python 3 | 180322T182835Z | Dat |
| 023 | SOGL V0.12 | 180322T101829Z | dzaima |
| 088 | C gcc | 180322T102124Z | GPS |
| 087 | Python 2 | 180321T135314Z | AvahW |
| 077 | Java JDK 10 | 180321T085524Z | Olivier |
| 062 | J | 180321T113605Z | Galen Iv |
| 029 | Stax | 180321T070105Z | recursiv |
| 043 | Japt | 180321T212449Z | Oliver |
| 028 | Husk | 180321T213400Z | Leo |
| 055 | q/kdb+ | 180321T215320Z | mkst |
| 074 | C gcc preprocessor macro | 180321T190144Z | Digital |
| 052 | Vim | 180321T114310Z | oktupol |
| 056 | Perl 6 | 180321T145251Z | nwellnho |
| 057 | Perl 5 p | 180321T074002Z | Ton Hosp |
| 067 | Gema | 180321T093052Z | manatwor |
| 084 | SmileBASIC | 180321T162108Z | 12Me21 |
| 073 | Kotlin | 180321T160900Z | Makotosa |
| 092 | PHP | 180321T142847Z | Dave |
| 074 | IBM/Lotus Notes Formula Language | 180321T084840Z | ElPedro |
| 074 | PowerShell | 180321T134355Z | AdmBorkB |
| 058 | Retina | 180321T115111Z | Martin E |
| 060 | Ruby n | 180321T114004Z | Kirill L |
| 066 | APL+WIN | 180321T125805Z | Graham |
| 075 | Haskell | 180321T073643Z | Cristian |
| 076 | Julia 0.6 | 180321T111304Z | niczky12 |
| 079 | Coconut | 180321T111931Z | Laikoni |
| 069 | Ruby n | 180321T093439Z | Asone Tu |
| 085 | Excel | 180321T102609Z | Wernisch |
| 097 | Batch | 180321T094906Z | Neil |
| 068 | JavaScript | 180321T071744Z | tsh |
| 030 | 05AB1E | 180321T082937Z | Emigna |
| 079 | Python | 180321T073912Z | xnor |
| 087 | Red | 180321T081113Z | Galen Iv |
Tcl, 91 bytes
puts [lindex [set L {Red Orange Yellow Green Blue Indigo Violet Red}] [lsearch $L $argv]+1]
puts [lindex [set L {Red Orange Yellow Green Blue Indigo Violet}] [expr ([lsearch $L $argv]+1)%7]]
puts [lindex [set L {Red Orange Yellow Green Blue Indigo Violet Red}] [expr [lsearch $L $argv]+1]]
Red, 74 bytes
func[c][prin select[Red Orange Yellow Green Blue Indigo Violet Red]load c]
Factor, 81 bytes
[ qw{ Red Orange Yellow Green Blue Indigo Violet } [ index 1 + 7 mod ] keep nth ]
Swift, 121 113 bytes
let s=readLine()!;let i=["Red","Orange","Yellow","Green","Blue","Indigo","Violet"];print(i[(i.index(of:s)!+1)%7])
Bash + GNU Utilities, 63
grep -Po $1\\K.[a-z]+<<<RedOrangeYellowGreenBlueIndigoVioletRed
The list of colours is reversed so that a regex lookahead may be used. The lookahead boilerplate is one byte shorter than the lookbehind boilerplate. Of course we can do better the PCRE \K.
Lua, 89 bytes
c="Red OrangeYellowGreen Blue IndigoVioletRed"r=c:find(io.read())+6print(c:sub(r,r+5))
Outputs some trailing spaces. Example:
input: Yellow
output: Green
space here: ^
Lua, 102 bytes
c="Red OrangeYellowGreen Blue IndigoVioletRed"r=c:find(io.read())+6print(c:sub(r,r+5):match("%a+"))
Doesn't output any trailing spaces.
Wolfram Language (Mathematica), 84 bytes
TextWords version (84 Bytes but slow)
#/.Thread[#->RotateLeft@#&[TextWords@"Red Orange Yellow Green Blue Indigo Violet"]]&
StringSplit (86 Bytes)
#/.Thread[#->RotateLeft@#&[StringSplit@"Red Orange Yellow Green Blue Indigo Violet"]]&
Built-in color names called by number, also 86 Bytes:
#/.Thread[#->RotateLeft@#&[ColorData["HTML","Range"][[1,{114,100,139,52,10,57,135}]]]]
R, 109 93 bytes
function(x){y=c("Red","Orange","Yellow","Green","Blue","Indigo","Violet");y[match(x,y)%%7+1]}
-16 thanks to Giuseppe for the use of match advice
sed, 72 bytes
s/$/%RedOrangeYellowGreenBlueIndigoVioletRed/;s/(.+)%.*\1(.[a-z]+).*/\2/
Example 1:
Input:
Red
Orange
Yellow
Green
Blue
Indigo
Violet
Output:
Orange
Yellow
Green
Blue
Indigo
Violet
Red
Example 2:
Input:
Indigo
Yellow
Red
Red
Blue
Green
Orange
Violet
Green
Green
Green
Blue
Blue
Violet
Output:
Violet
Green
Orange
Orange
Indigo
Blue
Yellow
Red
Blue
Blue
Blue
Indigo
Indigo
Red
Python 3, 85 bytes
c='Red Orange Yellow Green Blue Indigo Violet'.split()
f=lambda x:c[(c.index(x)+1)%7]
SOGL V0.12, 23 bytes
k‰³d∆|ΝμHō↑≥░δ÷f‘θ⁽,WIw
Explanation:
...‘θ⁽,WIw
...‘ push "red orange yellow green blue indigo violet"
θ split on spaces
⁽ uppercase the 1st letter of every item (SOGLs dictionary only has lowercase words)
,W get the inputs index in the array
I increment
w and get that item in the array, wrapping if necessary
C (gcc), 88 bytes
f(char*s){printf("%.6s",(strstr("red violetindigoblue green yelloworangered",s)+6));}
Python 2, 89 87 bytes
def f(c):a="red orange yellow green blue indigo violet".split();print a[-~a.index(c)%7]
Test cases:
f("red") -> orange
f("orange") -> yellow
f("yellow") -> green
f("green") -> blue
f("blue") -> indigo
f("indigo") -> violet
f("violet") -> red
f("notAColour") -> Error
Java (JDK 10), 77 bytes
s->"Red Orange Yellow Green Blue Indigo Violet Red".split(s)[1].split(" ")[1]
Credits
- -10 bytes thanks to Kevin Cruijssen
- -1 byte thanks to Okx
J, 67 64 62 bytes
-2 bytes thank to FrownyFrog
>:&.((cut'Red Orange Yellow Green Blue Indigo Violet Red')i.<)
Stax, 31 30 29 bytes
ÇôF┘≡▓ƒ◄╙>┘☼░⌂╪B<U[ÇQ╒eöΣQ╔÷n
This uses the ring translation instruction. It replaces each element in an array with the following one from the "decoder ring". Usually, it's used to do character replacement in a string, but it can be used on an entire string too, if it's wrapped in a singleton array.
Here's the unpacked, ungolfed, commented ascii representation of the same program.
] wrap input in singleton array
`5^bZ_9G*h]h%oM~X9e-0ZQJkb2` compressed string literal with color names
:.j title case and split on spaces
:t do ring translation
Husk, 28 bytes
S!o→€⁰w¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉
Maybe there are better options for managing the arguments, but this is the best I could find
Explanation
S!o→€⁰w¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉
¨ṙ}ΘΞĠ«ẇ₇G²€ḃλBżḃIÿö⌉ Compressed string with all the colors
"Red Orange Yellow Green Blue Indigo Violet"
w Split on spaces
S Pass the list to both the following functions:
€⁰ 1) Find the index of the input in the list
o→ and increase it by one
! 2) Get the element of the list at the
resulting position (indexing is cyclical)
q/kdb+, 59 55 bytes
Solution:
.[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]
Examples:
q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Red"
"Violet"
q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Orange"
"Red"
q).[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange]"Blue"
"Green"
Explanation:
Create a dictionary of colour => next colour, the input is the key to the dictionary:
.[!;2 8#($)`Red`Violet`Indigo`Blue`Green`Yellow`Orange] / the solution
.[ ; ] / apply multiple args to function
`Red`Violet`Indigo`Blue`Green`Yellow`Orange / list of colours
($) / convert to strings
2 8# / reshape into 2x8 grid
! / create dictionary
Bonus:
It's 53 bytes in K4:
.[!;2 8#$`Red`Violet`Indigo`Blue`Green`Yellow`Orange]
C (gcc) preprocessor macro, 74
Score is 74 bytes passed to the compiler.
Same idea as @nwellnhof's Perl answer.
-Df(c)=printf("%.6s","IndigoBlue VioletYellowOrangeRed Green"+*c/4%8*6)
Vim, 59 56 53 52 Bytes
-1 byte thanks to tsh
2IYellow Green Blue Indigo Violet Red Orange <Esc>*wywVp
Perl 6, 56 bytes
{<Indigo Blue Violet Yellow Orange Red Green>[.ord/4%8]}
Exploits the fact that bits 2-4 of the ASCII codes of each color's first letter happen to map to 0-6.
say map (*.ord +> 2) % 8, <R O Y G B I V>
# (4 3 6 1 0 2 5)
Here's a nice non-competing solution that uses "purple" instead of "indigo" and "violet" (38 chars, 59 bytes):
{'🍎🧡💛💚💙💜🍎'.uninames~~m/$^a.\S+.<(\S+/}
Perl 5 -p, 58 57 bytes
#!/usr/bin/perl -p
$_={(Red,Orange,Yellow,Green,Blue,Indigo,Violet)x2}->{$_}
Now that the challenge has been changed to be cyclic the regex solution
say RedOrangeYellowGreenBlueIndigoVioletRed=~/$_(.[a-z]+)/
isn't optimal anymore (due to the double Red)
Also 57 bytes:
#!/usr/bin/perl -p
$_=(Indigo,Blue,Violet,Yellow,Orange,Red,Green)[ord>>2&7]
Gema, 67 characters
*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}
Sample run:
bash-4.4$ echo -n Yellow | gema '*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}'
Green
bash-4.4$ echo -n Violet | gema '*=@subst{*\?<J>=\?\$2\;\?=;RedOrangeYellowGreenBlueIndigoVioletRed}'
Red
Gema, 59 characters
R=Orange
O=Yellow
Y=Green
G=Blue
B=Indigo
I=Violet
V=Red
*=
Boring one. Dumbest approach ever, but quite short.
Sample run:
bash-4.4$ gema 'R=Orange;O=Yellow;Y=Green;G=Blue;B=Indigo;I=Violet;V=Red;*=' <<< 'Yellow'
Green
bash-4.4$ gema 'R=Orange;O=Yellow;Y=Green;G=Blue;B=Indigo;I=Violet;V=Red;*=' <<< 'Violet'
Red
SmileBASIC, 94 84 bytes
C$="Red OrangeYellowGreen Blue IndigoViolet
INPUT I$?MID$(C$*2,INSTR(C$,I$)+6,6)
Kotlin, 73 bytes
x->"RedOrangeYellow Green BlueIndigoVioletRed".substringAfter(x).take(6)
Taking advantage of the fact that many colors are 6 characters, the ones that are not are prefixed with spaces to make them 6 characters long. Hopefully it's acceptable that some of the colors are outputted with spaces before them.
e.g. Red is " Red", Blue is " Blue"
PHP, 92 bytes
$a=" Red OrangeYellowGreen Blue IndigoVioletRed";echo substr($a,strpos($a,$argv[1])+6,6)
IBM/Lotus Notes Formula Language, 79 74 bytes
@Left(@Right("Red Orange Yellow Green Blue Indigo Violet Red ";a+" ");" ")
Previous version for 79:
R:=@Explode("Red,Orange,Yellow,Green,Blue,Indigo,Violet,Red");R[@Member(a;R)+1]
Takes input from an editable text field called a.
There is no TIO for formula language so here's a couple of screenshots.
PowerShell, 74 bytes
(-split("Red Orange Yellow Green Blue Indigo Violet "*2-split$args)[1])[0]
Takes the string "Red ... Violet " and string-multiplies it out by two to properly handle the Violet -> Red test case. We then -split that string on input $args to give us an array of two strings. We take the second string thereof [1], then -split that on whitespace to give us an array of strings and take the first [0].
For example, for input "Yellow", the first step will result in @("Red Orange ", " Green Blue Indigo ... Indigo Violet "). We take the second one of that, split it on whitespace (which removes the whitespace), resulting in @("Green", "Blue", ... "Violet"), so taking the [0] one thereof results in the proper next string.
Retina, 65 58 bytes
$
(.[a-z]+)
L$:`RedOrangeYellowGreenBlueIndigoVioletRed
$1
Explanation
$
(.[a-z]+)
We start by appending (.[a-z]+) to the input, thereby turning it into a regex which matches the input colour, immediately followed by exactly one more colour (capturing the latter).
L$:`RedOrangeYellowGreenBlueIndigoVioletRed
$1
Now the : swaps the stage's input with its own regex. So the previous result becomes the regex and it's matched against the list of colours. The (single) match gets replaced with its first capturing group (i.e. the next colour in the cycle) and returned. Output at the end of the program happens automatically.
Ruby -n, 62 60 bytes
-2 by Asone Tuhid.
p"RedVioletIndigoBlueGreenYellowOrangeRed"[/.[a-z]+(?=#$_)/]
Regex approach looks promising for Ruby too. However, I arrived at a shorter solution using a lookahead and directly printing the match, rather than playing with capturing groups. The list of colors is in reverse direction since lookahead is 1 byte cheaper than lookbehind.
APL+WIN, 66 bytes
↑(c⍳⊂⎕)⌽c←'Red' 'Orange' 'Yellow' 'Green' 'Blue' 'Indigo' 'Violet'
Prompts for screen input of colour as a string.
Haskell, 80 71 75 bytes
Thanks to Laikoni for shortening 9 bytes!
g x=snd(span(/=x)$words"Red Orange Yellow Green Blue Indigo Violet Red")!!1
Another solution, slightly more idiomatic, but I could not get it shorter:
data R=Red|Orange|Yellow|Green|Blue|Indigo|Violet deriving(Enum,Read,Eq)
succ.read
It needs to derive Read because of the requirement that the input is a string and at least Eq or Show in order to either test for equality or show the result.
Julia 0.6, 76 bytes
f(s)=match(Regex("$s(.[a-z]*)"),"RedOrangeYellowGreenBlueIndigoViolet"^2)[1]
This handles the Violet->Red by recycling the string with the power ^ operator.
Here's a slightly longer solution without regexes:
g(s,l=split("Red Orange Yellow Green Blue Indigo Violet"," "))=l[(findin(l,[s])[1])%7+1]
Coconut, 79 bytes
s->"# Violet Red # # Green Indigo Yellow # # Orange Blue".split()[ord(s[0])%12]
Ruby -n, 75 69 bytes
a=%w{Red Orange Yellow Green Blue Indigo Violet};p a[-~(a.index$_)%7]
Excel, 85 bytes
=CHOOSE(MOD(CODE(A1),12),"green","indigo","yellow",,,"orange","blue",,"violet","red")
Uses lowercase names.
Same approach, with Uppercase letters 86 bytes:
=CHOOSE(MOD(CODE(A1),12),"Violet","Red",,,"Green","Indigo","Yellow",,,"Orange","Blue")
Batch, 97 bytes
@set s=Red Orange Yellow Green Blue Indigo Violet Red
@call set s=%%s:*%1 =%%
@echo %s: =&rem %
Explanation: The call on the second line has the effect of substituting the parameter into the command and evaluating it, turning it into e.g. set s=%s:Red =%, which deletes the prefix of the string that includes the parameter. The substitution on the third line then replaces all the spaces with statement separators and comments. This works because string substitution happens before parsing.
JavaScript, 68 bytes
s=>'RedOrangeYellowGreenBlueIndigoVioletRed'.match(s+'(.[a-z]*)')[1]
For input "Red", this function first construct an RegExp /Red(.[a-z]*)/ to match the string 'RedOrangeYellowGreenBlueIndigoVioletRed' and then return the first capture result.
f=
s=>'RedOrangeYellowGreenBlueIndigoVioletRed'.match(s+'(.[a-z]*)')[1]
document.write('<table><tr><th>Input<th>Output')
for(i='Red';;){
document.write(`<tr><td>${i}<td>${i=f(i)}`);
if(i=='Red')break;
}
05AB1E, 30 bytes
“†¾›ÈŠÛˆ¨‡—ëßigo°Íolet“#™DIk>è
Explanation
“†¾›ÈŠÛˆ¨‡—ëßigo°Íolet“ # push a string of colours
# # split on spaces
™ # title-case each
D # duplicate
Ik # get the index of the input
> # increment
è # get the element at that index
Python, 79 bytes
z="Red Orange Yellow Green Blue Indigo Violet".split()*2
dict(zip(z,z[1:])).get
Handles Violet -> Red. The desired function is given anonymously in the second line.
80 bytes
lambda x:"Red Orange Yellow Green Blue Indigo Violet Red".split(x)[1].split()[0]
Red, 87 bytes
func[c][print first find/tail[Red Orange Yellow Green Blue Indigo Violet Red]to-word c]


