首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >蓝鸭,红鸭,灰鸭

蓝鸭,红鸭,灰鸭
EN

Code Golf用户
提问于 2018-03-19 09:36:01
回答 23查看 7.9K关注 0票数 49

所以,上周我发布了一个挑战来玩鸭子,鸭子,鹅。这导致许多明尼苏达州人评论他们的地区“灰鸭”变异

规则如下:

使用此颜色列表:

代码语言:javascript
复制
Red
Orange
Yellow
Green
Blue
Indigo
Violet
Gray

编写一个程序来遵循以下规则:

  1. 选择这些颜色中的一种,并将其添加到单词“鸭子”中,并将结果打印到新行。
  2. 如果颜色不是“灰色”,重复第一步。
  3. 如果颜色是“灰色”,就结束你的程序。

必须遵守的规则:

  • 程序不应始终打印相同的行数。
  • 它可以从“灰鸭”开始,但不应该一直这样做。
  • 每一行都应该有一个鸭子,没有空行输出。
  • 在颜色和鸭子之间应该至少有一个空格。
  • 重要输出前后的空白并不重要。
  • 输出的情况并不重要。
  • 颜色可以重复。
  • 输出不一定每次都包含所有颜色,但必须有可能您的代码将输出每一种颜色。
  • 不能包含上述数组之外的颜色。
  • 在你的回答中,灰色或灰色都是可以接受的。
  • 颜色不应始终保持相同的顺序。
  • 以最短的程序为目标。最小字节数获胜。

示例输出:

代码语言:javascript
复制
Green duck
Orange duck
Yellow duck
Indigo duck
Yellow duck 
Gray duck

感谢迈克·希尔第一次提醒我注意这种变化。

EN

回答 23

Code Golf用户

发布于 2018-03-19 10:09:52

Perl 5,79字节

代码语言:javascript
复制
say$_=(Grey,Red,Orange,Yellow,Green,Blue,Indigo,Violet)[rand 8]." duck"until/y/

在网上试试!

票数 12
EN

Code Golf用户

发布于 2018-03-19 19:52:39

出租车,1995年字节

代码语言:javascript
复制
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".

在网上试试!

我认为值得注意的是,47%的代码只是从1到8之间选择一个随机整数。

而且,出租车是如此冗长,所以在每种颜色之后硬编码duck\n都要短得多,而不是稍后将其连接起来。

以下是未镀金版本:

代码语言:javascript
复制
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".
票数 11
EN

Code Golf用户

发布于 2018-03-19 10:21:42

Java (OpenJDK 9),133个字节

代码语言:javascript
复制
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");}

在网上试试!

解释

代码语言:javascript
复制
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.
}
票数 8
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/159567

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档