51
\$\begingroup\$

So, last week I posted a challenge to play Duck, Duck, Goose. This lead to a number of Minnesotans commenting about their regional 'Gray duck' variation.

So here's the rules:

Using this list of colours:

Red
Orange
Yellow
Green
Blue
Indigo
Violet
Gray

Write a program to follow these rules:

  1. Select one of these colours, and prepend it to the word 'duck' and print the result to a new line.
  2. If the colour was not 'Gray', repeat step 1.
  3. If the colour was 'Gray', end your program.

Rules which must be followed:

  • The program should not consistently print the same number of lines.
  • It can begin on 'Gray duck', but should not do consistently.
  • There should be exactly one duck on each line and no empty lines are output.
  • There should be at least one space between a colour and a duck.
  • White space before and after the significant output does not matter.
  • The case of the output doesn't matter.
  • Colours can be repeated.
  • The output doesn't have to contain every colour every time, but it must be possible that your code will output every colour.
  • No colours outside the above array can be included.
  • Either grey or gray are acceptable in your answer.
  • Colours should not consistently be in the same order.
  • Aim for the shortest program. Smallest number of bytes wins.

Example output:

Green duck
Orange duck
Yellow duck
Indigo duck
Yellow duck 
Gray duck

Thanks to @Mike Hill for first alerting me to this variation.

\$\endgroup\$
9
  • \$\begingroup\$ Any rules on the distribution on outcomes? Because I could generate valid outputs by picking from non-grey colours a random number of times, followed by printing Grey once (so I wouldn't have to pick from all of them and check whether I've picked Grey). \$\endgroup\$ Mar 19, 2018 at 10:10
  • \$\begingroup\$ @MartinEnder That's fine. "Colours should not consistently be in the same order." matters, but there's nothing to stop you picking grey separately at the end. \$\endgroup\$
    – AJFaraday
    Mar 19, 2018 at 10:12
  • 3
    \$\begingroup\$ Is the alternative spelling "gray" allowed? \$\endgroup\$
    – 12Me21
    Mar 19, 2018 at 12:09
  • \$\begingroup\$ @12Me21 Curious, tho. Is there a technical reason for that? Or just an aesthetic one? \$\endgroup\$
    – AJFaraday
    Mar 19, 2018 at 12:10
  • 3
    \$\begingroup\$ In my not-so-humble opinion, you are missing one extremely important duck color. Blue is close, but not precise enough. \$\endgroup\$
    – cobaltduck
    Mar 20, 2018 at 23:29

53 Answers 53

51
\$\begingroup\$

LuaLaTeX, 220 211 characters

command:

lualatex -interaction nonstopmode

Not the shortest, but the fanciest. Based on @skillmon's solution

enter image description here

\RequirePackage{tikzducks}\newcount\b\let~\def~\0{red}~\1{orange}~\2{yellow}~\3{green}~\4{blue}~\5{cyan}~\6{violet}~\7{gray}\loop\b\uniformdeviate8\tikz\duck[body=\csname\the\b\endcsname]; \ifnum\b<7\repeat\stop
\$\endgroup\$
5
  • 10
    \$\begingroup\$ Good heavens! This is just the best thing! You've made my day, Alex. \$\endgroup\$
    – AJFaraday
    Mar 20, 2018 at 16:17
  • \$\begingroup\$ @AJFaraday indeed amazing idea to use tikzducks there :) \$\endgroup\$
    – Skillmon
    Mar 20, 2018 at 16:19
  • 7
    \$\begingroup\$ +1 and my apologies for choosing such a long package name! \$\endgroup\$
    – user79198
    Mar 20, 2018 at 16:19
  • \$\begingroup\$ @sam, you get my absolution \$\endgroup\$
    – AlexG
    Mar 21, 2018 at 7:55
  • 9
    \$\begingroup\$ Hello mr. person who I definitely don't know from other technical communities like TeX.sx. Since this answer covers some birds in the family Anatidae, I feel compelled to provide a token of approval by upvoting it. :) \$\endgroup\$ Mar 21, 2018 at 11:33
27
\$\begingroup\$

6502 machine code (C64), 124 bytes

00 C0 AD 12 D0 85 02 A2 17 8E 18 D0 A5 02 F0 03 0A 90 02 49 1D 85 02 A8 CA 10
02 A2 2F BD 42 C0 D0 F6 88 D0 F3 86 FB E8 BD 42 C0 F0 06 20 16 E7 E8 D0 F5 AA
BD 73 C0 F0 06 20 16 E7 E8 D0 F5 A6 FB D0 C9 60 00 C7 52 45 59 00 D2 45 44 00
CF 52 41 4E 47 45 00 D9 45 4C 4C 4F 57 00 C7 52 45 45 4E 00 C2 4C 55 45 00 C9
4E 44 49 47 4F 00 D6 49 4F 4C 45 54 00 20 44 55 43 4B 0D 00

Online demo -- Usage: SYS49152.

screenshot


Explanation (commented disassembly):

         00 C0       .WORD $C000        ; load address
.C:c000  AD 12 D0    LDA $D012          ; current rasterline as seed
.C:c003  85 02       STA $02            ; to "random" value
.C:c005  A2 17       LDX #$17           ; cfg for upper/lower, also use as
.C:c007  8E 18 D0    STX $D018          ;    initial index into colors array
.C:c00a   .loop:
.C:c00a  A5 02       LDA $02            ; load current random val
.C:c00c  F0 03       BEQ .doEor         ; zero -> feedback
.C:c00e  0A          ASL A              ; shift left
.C:c00f  90 02       BCC .noEor         ; bit was shifted out -> no feedback
.C:c011   .doEor:
.C:c011  49 1D       EOR #$1D
.C:c013   .noEor:
.C:c013  85 02       STA $02            ; store new random val
.C:c015  A8          TAY                ; use as counter for next color string
.C:c016   .findloop:
.C:c016  CA          DEX                ; next char pos in colors (backwards)
.C:c017  10 02       BPL .xok           ; if negative ...
.C:c019  A2 2F       LDX #$2F           ;    load length of colors - 1
.C:c01b   .xok:
.C:c01b  BD 42 C0    LDA .colors,X      ; load character from colors
.C:c01e  D0 F6       BNE .findloop      ; not zero, try next character
.C:c020  88          DEY                ; decrement random counter
.C:c021  D0 F3       BNE .findloop      ; not zero, continue searching
.C:c023  86 FB       STX $FB            ; save character position
.C:c025  E8          INX                ; increment to start of color
.C:c026   .outloop:
.C:c026  BD 42 C0    LDA .colors,X      ; output loop for color string
.C:c029  F0 06       BEQ .duckout
.C:c02b  20 16 E7    JSR $E716
.C:c02e  E8          INX
.C:c02f  D0 F5       BNE .outloop
.C:c031   .duckout:
.C:c031  AA          TAX                ; A is now 0, use as char pos for duck
.C:c032   .duckoutloop:
.C:c032  BD 73 C0    LDA .duck,X        ; output loop for duck string
.C:c035  F0 06       BEQ .outdone
.C:c037  20 16 E7    JSR $E716
.C:c03a  E8          INX
.C:c03b  D0 F5       BNE .duckoutloop
.C:c03d   .outdone:
.C:c03d  A6 FB       LDX $FB            ; load saved character position
.C:c03f  D0 C9       BNE .loop          ; not zero -> continue main loop
.C:c041  60          RTS                ; zero was start of "Grey" -> done
.C:c042   .colors:
.C:c042  00 c7 52 45    .BYTE 0, "Gre"
.C:c046  59 00 d2 45    .BYTE "y", 0, "Re"
.C:c04a  44 00 cf 52    .BYTE "d", 0, "Or"
.C:c04e  41 4e 47 45    .BYTE "ange"
.C:c052  00 d9 45 4c    .BYTE 0, "Yel"
.C:c056  4c 4f 57 00    .BYTE "low", 0
.C:c05a  c7 52 45 45    .BYTE "Gree"
.C:c05e  4e 00 c2 4c    .BYTE "n", 0, "Bl"
.C:c062  55 45 00 c9    .BYTE "ue", 0, "I"
.C:c066  4e 44 49 47    .BYTE "ndig"
.C:c06a  4f 00 d6 49    .BYTE "o", 0, "Vi"
.C:c06e  4f 4c 45 54    .BYTE "olet"
.C:c072  00             .BYTE 0
.C:c073   .duck:
.C:c073  20 44 55 43    .BYTE " duc"
.C:c077  4b 0d 00       .BYTE "k", $d, 0
\$\endgroup\$
6
  • \$\begingroup\$ I'm sorry, your demo doesn't seem to output any duck-related content. \$\endgroup\$
    – AJFaraday
    Mar 19, 2018 at 10:47
  • 1
    \$\begingroup\$ @AJFaraday please notice the "usage" part ... the command to run it is sys 49152. \$\endgroup\$ Mar 19, 2018 at 10:48
  • \$\begingroup\$ Okay, that made my day :) \$\endgroup\$
    – AJFaraday
    Mar 19, 2018 at 10:49
  • 1
    \$\begingroup\$ That was awesome. It's been a while since I've written c64 assembly. \$\endgroup\$
    – lsd
    Mar 19, 2018 at 13:31
  • 2
    \$\begingroup\$ @lsd: same here! my very first program was 6502 assembly on a C64,because I just got it and a friend gave me the 6502 book with a task "write a game!" to jumsptart me into writing something! (great way! it was a great motivator to not just read, but actually write something). wrote a 192 bytes (iirc) "Snake" program (with a, imo, clever double index pointing to the head & to the tail of the snake's position)... first try: it zoomed past the bottom edge, 'eating' into the rest of the ram, growing on any "@", as I forgot to set up the walls ^^ \$\endgroup\$ Mar 19, 2018 at 16:12
12
\$\begingroup\$

Perl 5, 79 bytes

say$_=(Grey,Red,Orange,Yellow,Green,Blue,Indigo,Violet)[rand 8]." duck"until/y/

Try it online!

\$\endgroup\$
11
\$\begingroup\$

Taxi, 1995 bytes

Go to Heisenberg's:w 1 r 3 r 1 l.[a]Pickup a passenger going to Divide and Conquer.8 is waiting at Starchild Numerology.8 is waiting at Starchild Numerology.Go to Starchild Numerology:n 1 l 3 l 1 l 3 l.Pickup a passenger going to Divide and Conquer.Pickup a passenger going to Multiplication Station.Go to Divide and Conquer:w 1 r 3 r 1 r 2 r 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:e 1 l 1 l 2 l.Pickup a passenger going to What's The Difference.Pickup a passenger going to Trunkers.Go to Zoom Zoom:n.Go to Trunkers:w 3 l.Pickup a passenger going to What's The Difference.Go to What's The Difference:w 2 r 1 l.Pickup a passenger going to Multiplication Station.1 is waiting at Starchild Numerology.Go to Starchild Numerology:e 1 r 1 l 3 l.Pickup a passenger going to Addition Alley.Go to Multiplication Station:w 1 r 2 r 1 r 4 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:n 2 l 1 r 3 l 1 l.Pickup a passenger going to The Underground.'Red duck\n' is waiting at Writer's Depot.'Orange duck\n' is waiting at Writer's Depot.'Yellow duck\n' is waiting at Writer's Depot.'Green duck\n' is waiting at Writer's Depot.'Blue duck\n' is waiting at Writer's Depot.'Indigo duck\n' is waiting at Writer's Depot.'Violet duck\n' is waiting at Writer's Depot.'Grey duck' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 1 l.[b]Pickup a passenger going to Narrow Path Park.Go to Narrow Path Park:n 3 r 1 l 1 r.Go to The Underground:e 1 r.Switch to plan "c" if no one is waiting.Pickup a passenger going to The Underground.Go to Writer's Depot:s 2 r 1 l 2 l.Switch to plan "b".[c]Go to Narrow Path Park:n 4 l.Pickup a passenger going to Post Office.Go to Post Office:e 1 r 4 r 1 l.Go to Writer's Depot:s 1 r 1 l 2 l.Switch to plan "a" if no one is waiting.[d]Pickup a passenger going to Sunny Skies Park.Go to Sunny Skies Park:n 2 r.Go to Writer's Depot:n 1 l.Switch to plan "e" if no one is waiting.Switch to plan "d".[e]Go to Heisenberg's:n 3 r 3 r.Switch to plan "a".

Try it online!

I think it's worth noting that 47% of this code is just picking a random integer from 1 to 8.
Also, Taxi is so verbose that it is way shorter to hard code the duck\n after each color rather than concatenating it later.
Here's the un-golfed version:

Go to Heisenberg's: west 1st right 3rd right 1st left.

[Pick up a random INT 1-8 going to The Underground]
[a]
Pickup a passenger going to Divide and Conquer.
8 is waiting at Starchild Numerology.
8 is waiting at Starchild Numerology.
Go to Starchild Numerology: north 1st left 3rd left 1st left 3rd left.
Pickup a passenger going to Divide and Conquer.
Pickup a passenger going to Multiplication Station.
Go to Divide and Conquer: west 1st right 3rd right 1st right 2nd right 1st right.
Pickup a passenger going to Cyclone.
Go to Cyclone: east 1st left 1st left 2nd left.
Pickup a passenger going to What's The Difference.
Pickup a passenger going to Trunkers.
Go to Zoom Zoom: north.
Go to Trunkers: west 3rd left.
Pickup a passenger going to What's The Difference.
Go to What's The Difference: west 2nd right 1st left.
Pickup a passenger going to Multiplication Station.
1 is waiting at Starchild Numerology.
Go to Starchild Numerology: east 1st right 1st left 3rd left.
Pickup a passenger going to Addition Alley.
Go to Multiplication Station: west 1st right 2nd right 1st right 4th left.
Pickup a passenger going to Addition Alley.
Go to Addition Alley: north 2nd left 1st right 3rd left 1st left.
Pickup a passenger going to The Underground.

[Use the random INT to select a color]
'Red duck\n' is waiting at Writer's Depot.
'Orange duck\n' is waiting at Writer's Depot.
'Yellow duck\n' is waiting at Writer's Depot.
'Green duck\n' is waiting at Writer's Depot.
'Blue duck\n' is waiting at Writer's Depot.
'Indigo duck\n' is waiting at Writer's Depot.
'Violet duck\n' is waiting at Writer's Depot.
'Grey duck' is waiting at Writer's Depot.
Go to Writer's Depot: north 1st left 1st left.
[b]
Pickup a passenger going to Narrow Path Park.
Go to Narrow Path Park: north 3rd right 1st left 1st right.
Go to The Underground: east 1st right.
Switch to plan "c" if no one is waiting.
Pickup a passenger going to The Underground.
Go to Writer's Depot: south 2nd right 1st left 2nd left.
Switch to plan "b".

[Output the selected color]
[c]
Go to Narrow Path Park: north 4th left.
Pickup a passenger going to Post Office.
Go to Post Office: east 1st right 4th right 1st left.

[If the color was grey, exit by error]
Go to Writer's Depot: south 1st right 1st left 2nd left.
Switch to plan "a" if no one is waiting.

[Get rid of the rest of the colors]
[You could throw them off a bridge but you won't get paid]
[d]
Pickup a passenger going to Sunny Skies Park.
Go to Sunny Skies Park: north 2nd right.
Go to Writer's Depot: north 1st left.
Switch to plan "e" if no one is waiting.
Switch to plan "d".

[Start over from the beginning]
[e]
Go to Heisenberg's: north 3rd right 3rd right.
Switch to plan "a".
\$\endgroup\$
3
  • \$\begingroup\$ That was fun to read. \$\endgroup\$
    – Makotosan
    Mar 20, 2018 at 2:59
  • \$\begingroup\$ Golly, that's esoteric! I can't begin to understand how it works. Lots of fun, tho :) \$\endgroup\$
    – AJFaraday
    Mar 20, 2018 at 9:02
  • \$\begingroup\$ This seems related to Fetlang \$\endgroup\$
    – Skillmon
    Mar 21, 2018 at 19:36
9
\$\begingroup\$

Java (OpenJDK 9), 133 bytes

v->{for(int x=9;x>0;)System.out.println("Grey,Red,Orange,Yellow,Green,Blue,Indigo,Violet".split(",")[x+=Math.random()*8-x]+" duck");}

Try it online!

Explanations

v->{                              // Void-accepting void lambda function
  for(int x=9;x>0;)               //  Loop until x is zero
    System.out.println(           //   Print...
        "Grey,Red,Orange,         //       colors, "Grey" first
         Yellow,Green,Blue,       //          more colors
         Indigo,Violet"           //          more colors
        .split(",")               //       as an array
          [x+=Math.random()*8-x]  //       pick one randomly, use implicit double to int cast with "x+=<double>-x" trick
        +" duck");                //      Oh, and append " duck" to the color.
}
\$\endgroup\$
8
\$\begingroup\$

Operation Flashpoint scripting language, 133 bytes

f={s="";v=s;while{v!="grey"}do{v=["Red","Orange","Yellow","Green","Blue","Indigo","Violet","Grey"]select random 7;s=s+v+" duck\n"};s}

Call with:

hint call f

Example output:

At first I somehow misread the challenge as if the goal was to just output a varying amount of lines, not necessarily ending at the "Grey duck" -line. Following that incorrect interpretation produced a slightly more interesting piece of code:

f={s="";c=[1];c set[random 9,0];{s=s+(["Red","Orange","Yellow","Green","Blue","Indigo","Violet","Grey"]select random 7)+" duck\n"}count c;s}
\$\endgroup\$
8
\$\begingroup\$

Ruby, 93 91 90 89 87 86 85 bytes

Thanks to Dom Hastings for saving 2 bytes, Kirill L. 1 byte and Asone Tuhid 1 byte!

puts %w(Red Orange Yellow Green Blue Indigo Violet Grey)[$.=rand(8)]+" duck"while$.<7

Try it online!

\$\endgroup\$
8
  • \$\begingroup\$ You can remove () around the code and if you use $. instead of s to store the index, you can avoid creating s altogether (since $. is pre-initialised to the line number!) You'll need to move Grey to the end of the list and check for $.<7 instead though. Hope that helps! \$\endgroup\$ Mar 19, 2018 at 10:22
  • \$\begingroup\$ @DomHastings Thanks! I was looking for a way to get rid of s=1 and $. was perfect! \$\endgroup\$ Mar 19, 2018 at 10:31
  • \$\begingroup\$ I think you can also remove space after while, Ruby doesn't seem to complain about that. \$\endgroup\$
    – Kirill L.
    Mar 19, 2018 at 10:46
  • \$\begingroup\$ @KirillL. That's right, thanks! I previously had while s... and the space was needed. Didn't see this opportunity after changing s to $.. \$\endgroup\$ Mar 19, 2018 at 12:35
  • \$\begingroup\$ Color+" duck" saves 1 byte \$\endgroup\$ Mar 21, 2018 at 8:58
7
\$\begingroup\$

pdfTeX, 231 220 219 209 207 bytes

\newcount\b\let~\def~\0{Red}~\1{Orange}~\2{Yellow}~\3{Green}~\4{Blue}~\5{Indigo}~\6{Violet}~\7{Gray}~\9{ }\newlinechar`z\loop\b\pdfuniformdeviate8\message{z\csname\the\b\endcsname\9duck}\ifnum\b<7\repeat\bye

LuaTeX, 216 206 204 bytes

\newcount\b\let~\def~\0{Red}~\1{Orange}~\2{Yellow}~\3{Green}~\4{Blue}~\5{Indigo}~\6{Violet}~\7{Gray}~\9{ }\newlinechar`z\loop\b\uniformdeviate8\message{z\csname\the\b\endcsname\9duck}\ifnum\b<7\repeat\bye
\$\endgroup\$
6
\$\begingroup\$

05AB1E, 42 40 bytes

Saved 2 bytes thanks to Erik the Outgolfer

[“ëßigo°¯†¾›ÈŠÛˆ¨‡—°Íolet“#7ÝΩ©è'Ðœðý,®#

Try it online!

\$\endgroup\$
8
  • 3
    \$\begingroup\$ I've noticed a lot of these unreadable golfing languages. Is there a compiler from some more human-readable form? \$\endgroup\$
    – AJFaraday
    Mar 19, 2018 at 9:58
  • \$\begingroup\$ @AJFaraday: Not that I'm aware of. The only golfing language I know which has one of those is Charcoal. \$\endgroup\$
    – Emigna
    Mar 19, 2018 at 10:00
  • 1
    \$\begingroup\$ You can compress your string further, ind is ëß! \$\endgroup\$ Mar 19, 2018 at 11:16
  • 1
    \$\begingroup\$ Gs2 has one also. But that language is not used so much these days. \$\endgroup\$
    – recursive
    Mar 19, 2018 at 13:22
  • 1
    \$\begingroup\$ @Simón: The 05AB1E code page \$\endgroup\$
    – Emigna
    Mar 19, 2018 at 20:44
5
\$\begingroup\$

Brachylog, 68 bytes

"Grey","
Red
Orange
Yellow
Green
Blue
Indigo
Violet"ṇṛS," duck"ẉS¬?↰

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Ruby, 84 81 bytes

Thanks to Dom Hastings for -3 bytes.

puts$_=%w[Red Orange Yellow Green Blue Indigo Violet Grey].sample+" duck"until/y/

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you store in $_, you can just have until/y/ at the end for -3! \$\endgroup\$ Mar 19, 2018 at 10:47
5
\$\begingroup\$

Bash + GNU utilities, 72

shuf -e {Red,Orange,Yellow,Green,Blue,Indigo,Violet,Grey}\ Duck|sed /y/q

Try it online.

\$\endgroup\$
5
\$\begingroup\$

PHP, 89 bytes

do echo[Grey,Red,Orange,Yellow,Green,Blue,Indigo,Violet][$i=rand()%8]," Duck
";while($i);

Run with -nr or try it online.

\$\endgroup\$
4
\$\begingroup\$

><>, 107 bytes

x<>" duck"a>
x<^"deR"
x<^"egnarO"
x<^"wolleY"
x<^"neerG"
x<^"eulB"
x<^"ogidnI"
x<^"teloiV"
x"Grey duck"r>o|

Try it online!

\$\endgroup\$
4
\$\begingroup\$

JavaScript, 104 bytes

f=_=>`grey,red,orange,yellow,green,blue,indigo,violet`.split`,`[n=Math.random()*8|0]+` duck
${n?f():``}`

Try it online

\$\endgroup\$
2
  • 1
    \$\begingroup\$ FWIW, I had this variant, but I'm stuck at 104 bytes as well. \$\endgroup\$
    – Arnauld
    Mar 19, 2018 at 10:49
  • \$\begingroup\$ Shame we can't use new Date for the RNG here. \$\endgroup\$
    – Shaggy
    Mar 19, 2018 at 11:03
4
\$\begingroup\$

Octave, 114 112 bytes

do disp([strsplit('Red Orange Yellow Green Blue Indigo Violet'){i=randi(7)},' duck'])until i>6
disp('Grey duck')

Try it online!

There are a bunch of different options that are all between 112 and 118 bytes... Some initialize an index in the start and decrements it by a random number for each loop, and waits until it's 0. Others use printf instead of disp to avoid some brackets and so on.

\$\endgroup\$
2
  • \$\begingroup\$ Little question: Is there a symbol in octave that does the same as & in Excel, because I think it is possible to shorten the code even further then. \$\endgroup\$
    – Michthan
    Mar 20, 2018 at 9:41
  • 1
    \$\begingroup\$ Unfortunately, no... Strings must be concatenated inside square brackets (or using functions such as cat and horzcat. Thanks though :) \$\endgroup\$ Mar 20, 2018 at 12:15
4
\$\begingroup\$

PHP, 133 125 111 108 97 92 bytes

<?for(;$b=[Red,Orange,Yellow,Green,Blue,Indigo,Violet][rand(0,7)];)echo"$b duck
"?>Grey duck

Try it online!

-8 bytes thanks to @Olivier Grégoire

-3 bytes thanks to @manatwork

-11 bytes thanks to @Dom Hastings

\$\endgroup\$
6
  • 2
    \$\begingroup\$ In the color string and the test, change Grey to X, since it's not used. You'll gain 6 bytes. \$\endgroup\$ Mar 19, 2018 at 11:09
  • \$\begingroup\$ @OlivierGrégoire Oh true. Thanks for that :) \$\endgroup\$
    – Ethan
    Mar 19, 2018 at 11:10
  • 1
    \$\begingroup\$ Reverse the for's condition to need no parenthesis: x!=$b=$a[array_rand($a)]. BTW, ?> terminates a statement, no need for ; in front of it. \$\endgroup\$
    – manatwork
    Mar 19, 2018 at 11:30
  • \$\begingroup\$ @manatwork Ohh, nice! I'll update my answer now! \$\endgroup\$
    – Ethan
    Mar 19, 2018 at 11:33
  • 2
    \$\begingroup\$ You can save a few more bytes by not including x at all and using $b=$a.. as the condition and using rand(0,7) instead of array_rand. You can remove the newline between ?> and Grey duck too. Also your TIO link still had full tags, you can add -d short_open_tag=on to flags to get that working! :) \$\endgroup\$ Mar 19, 2018 at 12:07
4
\$\begingroup\$

Python 2, 138 133 120 117 116 bytes

import os
while id:id=ord(os.urandom(1))%8;print"Grey Red Orange Yellow Green Blue Indigo Violet".split()[id],'duck'

Try it online!

Much better with some ideas from @EriktheOutgolfer. Thanks!

-3 more with thanks to @ovs

-1 with thanks to @Rod for a cool new trick learned :-)

\$\endgroup\$
0
4
\$\begingroup\$

Python 3, 130, 128, 127, 126, 125 bytes

from random import*
d,c=1,'Grey Red Orange Yellow Green Blue Indigo Violet'.split()
while d!=c[0]:d=choice(c);print(d,'duck')

-1 by @ElPedro!
-1 by me
-1 by @Bubbler!

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ Can you move Grey to the front and have d!=c[0] to save a byte? \$\endgroup\$
    – ElPedro
    Mar 19, 2018 at 11:41
  • \$\begingroup\$ print(d,'duck') to save a byte. The default delimiter is a space. \$\endgroup\$
    – Bubbler
    Mar 20, 2018 at 0:32
4
\$\begingroup\$

bash, 96 bytes

a=(Grey Red Orange Yellow Green Blue Indigo Violet);for((i=1;i;));{ echo ${a[i=RANDOM%8]} duck;}

Thanks to @DigitalTrauma.

\$\endgroup\$
6
  • \$\begingroup\$ Lots of opportunities to golf this - check out the bash golfing tips \$\endgroup\$ Mar 19, 2018 at 17:56
  • \$\begingroup\$ @DigitalTrauma I was in hurry, but I wanted a pure Bash solution w/no GNU coreutils. I could shrink it from 112 to 105 bytes. \$\endgroup\$ Mar 19, 2018 at 19:53
  • \$\begingroup\$ Yep, I like the pure-bash answers too. Here's another 10 bytes off \$\endgroup\$ Mar 19, 2018 at 20:51
  • \$\begingroup\$ I stripped >0, however, I left ${#a} instead of 8. \$\endgroup\$ Mar 20, 2018 at 7:02
  • \$\begingroup\$ Why do you need to keep ${#a} instead of 8? This is code-golf - there is no need to give your code generalisability to be modified for a different number of colours. All you need to do is meet the spec in the smallest number of bytes. \$\endgroup\$ Mar 20, 2018 at 18:14
3
\$\begingroup\$

AWK, 114 bytes

{srand();for(split("Red9Orange9Yellow9Green9Blue9Indigo9Violet9Grey",A,9);r<8;print A[r]" duck")r=int(8*rand()+1)}

Try it online!

Explanation

{srand();                  # Seed rand to obtain different sequence each run
for(
     split("Red9Orange9Yellow9Green9Blue9Indigo9Violet9Grey",
            A,9);          # Split on 9 to avoid using '"'s
     r<8;
     print A[r]" duck")    # Print the colored ducks
     r=int(8*rand()+1)     # AWK uses 1-indexing not 0-indexing when splitting strings into arrays
}

Note that this requires "some" input. The input can be empty. To avoid the need for input prepend the first line with BEGIN

\$\endgroup\$
3
\$\begingroup\$

PowerShell, 94 bytes

for(;$r-ne'Grey'){$r=-split"Red
Orange
Yellow
Green
Blue
Indigo
Violet
Grey"|Random;"$r Duck"}

Try it online!

Loops until $r is equal to Grey. Inside the loop, -splits the literal string on newlines, chooses a Random one thereof, then prints out the color plus Duck (technically, it's left on the pipeline and pipeline cleanup on the next loop iteration causes a Write-Output to happen). Note that it's theoretically possible for Grey to never be chosen, and the loop to continue infinitely, but this almost never (in the probability sense) will happen.

\$\endgroup\$
3
\$\begingroup\$

R, 101 bytes

cat(paste(c(sample(scan(,""),rexp(1),T),"gray"),"duck\n"))
Red
Orange
Yellow
Green
Blue
Indigo
Violet

Try it online!

Heavily inspired by @user2390246's answer to the related challenge. We need two sources of randomness: Changing the order of the colors and sampling the non-gray duck colors. The sample will take a random sample of random size given by an exponential distribution with rate parameter 1, truncated to an integer. Using an exponential distrubtion unfortunately means that there is a probability of exp(-8) or around 0.0003354 that the sample will be at least 8, so we have to sample with replace=T.

\$\endgroup\$
4
  • \$\begingroup\$ you can replace then \n with an actual newline for 1 \$\endgroup\$
    – MickyT
    Mar 19, 2018 at 20:55
  • \$\begingroup\$ also you can save quite a bit using colors()[c(26,254,498,552,640,652)] in place of the scan(..) etc. should come down to about 83 \$\endgroup\$
    – MickyT
    Mar 19, 2018 at 21:12
  • \$\begingroup\$ @MickyT ......... I don't use R's graphics enough to remember about all the nice graphics builtins like colors()! Those are some nice golfs, which I think you should post as your own answer, since this is the less elegant way of getting the colors. \$\endgroup\$
    – Giuseppe
    Mar 19, 2018 at 21:14
  • \$\begingroup\$ ok then, I'll post it up with a couple of little changes \$\endgroup\$
    – MickyT
    Mar 20, 2018 at 22:30
3
\$\begingroup\$

Retina, 69 68 bytes

Thanks to Leo for saving 1 byte.

.^/y/{K`Red¶Orange¶Yellow¶Green¶Blue¶Indigo¶Violet¶Grey
" duck¶">?G`

Try it online!

Explanation

./y/^{K`Red¶Orange¶Yellow¶Green¶Blue¶Indigo¶Violet¶Grey

. suppresses implicit output at the end of the program (otherwise, we'd get two grey ducks). /y/^{ wraps the entire program in a loop which continues as long as the working string doesn't contain a y. The rest of that line sets the working string to a linefeed-separated list of all the colours.

" duck¶">G?`

We grep a random line from the working string (and therefore a random colour). And we print the result with a trailing duck and a linefeed.

\$\endgroup\$
0
3
\$\begingroup\$

MATL, 68 64 bytes

`'DYCIXMSQ(qm#Q$4{#is,Gh1(=lAjUSId;&'F2Y232hZaYb8YrX)' duck'h7Mq

Try it online!

Explanation

`                         % Do...while
  'DYCI···Id;&'           %   Push this string (to be decompressed by base conversion)
  F                       %   Push false
  2Y2                     %   Push string 'abc...xyz'
  32                      %   Push 32 (ASCII for space)
  h                       %   Concatenate horizontally. Gives 'abc...xyz '
  Za                      %   Base-convert from alphabet of all printable ASCII
                          %   characters except single quote (represented by input
                          %   false) to alphabet 'abc...xyz '. Produces the string
                          %   'grey red ··· violet'
  Yb                      %   Split on space. Gives a cell array of strings
  8Yr                     %   Random integer from 1 to 8, say k
  X)                      %   Get the content of the k-th cell
  ' duck'                 %   Push this string
  h                       %   Concatenate horizontally
  7M                      %   Push k again
  q                       %   Subtract 1
                          % Implicit end. Run a new iteration if top of the stack
                          % is non-zero
                          % Implicit display
\$\endgroup\$
3
\$\begingroup\$

Python 2, 98 bytes

Look ma no imports!

v=0
while 1:x=id(v)%97%8;print"GVIYORGBrinererleodladeuyliln ee egog n  towe"[x::8],"duck";v=1/x,v

(Prints extra spaces between the colours and duck as allowed in the question)

Try it online!

A pretty poor pseudo-random number generator seeded with the object id of 0 (but it seems to fit the spec) which repeatedly yields an integer, x, in [0,7] which is used to slice a list of characters from that index in steps of 8 to get the colour name which is printed along with duck as a tuple, forcing a space in-between. When x becomes zero Grey is printed and the evaluation of the next input to the modulo based random number generator errors attempting to divide by zero (v=1/x,v tries to make a new tuple with the first element 1/x = 1/0)


Same way is 100 in Python 3 with

v=0
while 1:x=id(v)%17%8;print("GVIYORGBrinererleodladeuyliln ee egog n  towe"[x::8],"duck");v=1/x,v
\$\endgroup\$
3
\$\begingroup\$

Java (JDK 10), 287 bytes

Random r=new Random();int i;String c;do{i=r.nextInt(8);switch(i){case 0:c="Red";break;case 1:c="Orange";break;case 2:c="Yellow";break;case 3:c="Green";break;case 4:c="Blue";break;case 5:c="Indigo";break;case 6:c="Violet";break;default:c="Gray";}System.out.println(c+" duck");}while(i!=7)

Try it online!

My very first codegolf! Obviously not competitive, just happy to have learned enough Java (currently in CS200) to be able to participate.

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

Thunno 2, 41 bytes

(’Red ṆỴ »Ẹ Ɓ# Ñɦ ƲỤ ṡʂ Ɓ~’OɼD‘ _*‘+£`Ɓ~Q

Attempt This Online!

Explanation

(                     # While loop
 ’...’                # (condition)  Push compressed string of colours
      O               #              Split it on spaces
       ɼ              #              Pick a random item from this list
        D             #              Duplicate the item
         ‘ _*‘+       #              Append compressed string " duck"
               £      #              Pop and print with a newline
                `Ɓ~   #              Push compressed string "Grey"
                   Q  #              Check for inequality
                      # (body)  Do nothing
\$\endgroup\$
2
\$\begingroup\$

Kotlin, 122 bytes

while({x:Any->println("$x duck");x!="Grey"}("Red,Orange,Yellow,Green,Blue,Indigo,Violet,Grey".split(",").shuffled()[0])){}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Might be helpful to someone out there, to get a random integer value in Kotlin, You can use the following: (0..7).shuffled()[0] Shorter than: (Math.random()*8).toInt() \$\endgroup\$
    – Makotosan
    Mar 20, 2018 at 15:23
2
\$\begingroup\$

MS-SQL, 158 bytes

DECLARE @ VARCHAR(6)a:SELECT @=value FROM STRING_SPLIT('Red,Orange,Yellow,Green,Blue,Indigo,Violet,Grey',',')ORDER BY NEWID()PRINT @+' duck'IF @<>'Grey'GOTO a

Based largely on Razvan's excellent answer, but using the STRING_SPLIT function that is specific to MS-SQL 2016 and later. Also uses a GOTO instead of a WHILE loop.

Formatted:

DECLARE @ VARCHAR(6)
a:
    SELECT @=value FROM 
        STRING_SPLIT('Red,Orange,Yellow,Green,Blue,Indigo,Violet,Grey',',')
        ORDER BY NEWID()
    PRINT @+' duck'
IF @<>'Grey'GOTO a
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.