代码高尔夫:蜂巢
挑战
根据字符数计算的最短代码将根据用户输入生成蜂巢。
蜂巢被定义为由用户输入的大小为两个大于零的正数的六边形网格(无需验证输入)。第一个数字 (W
) 代表蜂箱的宽度 - 或者 - 每行有多少个六边形。第二个数字 (H
) 代表蜂箱的高度 - 或者 - 每列有多少个六边形。
单个六边形由三个 ASCII 字符组成:_
、/
和 \
,以及三行:
__
/ \
\__/
六边形相互补充:第一列第一个蜂箱将处于“低”位置,第二个蜂箱将处于“高”位置 - 以相同的图案交替和重复,形成 W 六边形。这将重复 H 次以形成总共 WxH 的六边形。
测试用例:
Input:
1 1
Output:
__
/ \
\__/
Input:
4 2
Output:
__ __
__/ \__/ \
/ \__/ \__/
\__/ \__/ \
/ \__/ \__/
\__/ \__/
Input:
2 5
Output:
__
__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/
Input:
11 3
Output:
__ __ __ __ __
__/ \__/ \__/ \__/ \__/ \__
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
代码计数包括输入/输出(即完整程序)。
The challenge
The shortest code by character count that will generate a beehive from user input.
A beehive is defined a a grid of hexagons in a size inputted by the user as two positive numbers greater than zero (no need to validate input). The first number (W
) represents the width of the beehive - or - how many hexagons are on each row. The second number (H
) represents the height of the beehive - or - how many hexagons are on each column.
A Single hexagon is made from three ASCII characters: _
, /
and \
, and three lines:
__
/ \
\__/
Hexagons complete each other: the first column of the beehive will be 'low', and the second will be high - alternating and repeating in the same pattern forming W hexagons. This will be repeated H times to form a total of WxH hexagons.
Test cases:
Input:
1 1
Output:
__
/ \
\__/
Input:
4 2
Output:
__ __
__/ \__/ \
/ \__/ \__/
\__/ \__/ \
/ \__/ \__/
\__/ \__/
Input:
2 5
Output:
__
__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/ \
/ \__/
\__/
Input:
11 3
Output:
__ __ __ __ __
__/ \__/ \__/ \__/ \__/ \__
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
/ \__/ \__/ \__/ \__/ \__/ \
\__/ \__/ \__/ \__/ \__/ \__/
Code count includes input/output (i.e full program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
Perl,99 个字符
上次编辑:保存了一个字符,将
-($W%2)
替换为0-$W%2
(感谢 A. Rex)解释:
对于宽度 W 和高度 H,输出为 2+2 * H 行长和 3 * W+1 个字符宽,输出中间有很多重复。
为了方便起见,我们让
$W
为 3 * W + 1,即输出的字符宽度。最上面一行由模式
" __"
组成,重复 W/2 ==$W/6
次。偶数行由重复模式
"\__/ "
组成,并被截断为$W
字符。第二行输出是一种特殊情况,其中第二行的第一个字符应该是空格而不是\
。奇数行由重复模式
"/ \__"
组成,并被截断为$W
字符。我们构造一个字符串:
" " 。 “__/\”x 99
。请注意,该字符串的开头是第二行所需的输出。该行从位置 3 开始是奇数行的所需输出,从位置 6 开始是偶数行的所需输出。map
调用的 LIST 参数以 0 开头,后跟 H 次重复的 (3,6)。map
调用创建一个从适当位置开始的子字符串列表,长度为$W
= 3 * W + 1 个字符。在打印结果之前还需要进行一项调整。如果 W 是奇数,则第二行 (
$P[0]
) 上有一个额外的字符需要被砍掉
。如果 W 是偶数,则底行 ($P[-1]
) 上有一个额外的字符需要砍掉。Perl, 99 characters
Last edit: Saved one character replacing
-($W%2)
with0-$W%2
(thanks A. Rex)Explanation:
For width W and height H, the output is 2+2 * H lines long and 3 * W+1 characters wide, with a lot of repetition in the middle of the output.
For convenience, we let
$W
be 3 * W + 1, the width of the output in characters.The top line consists of the pattern
" __"
, repeated W/2 ==$W/6
times.The even numbered lines consist of the repeating pattern
"\__/ "
, truncated to$W
characters. The second line of output is a special case, where the first character of the second line should be a space instead of a\
.The odd numbered lines consist of the repeating pattern
"/ \__"
, truncated to$W
characters.We construct a string:
" " . "__/ \" x 99
. Note that the beginning of this string is the desired output for the second line. This line starting at position 3 is the desired output for the odd lines, and starting at position 6 for the even numbered lines.The LIST argument to the
map
call begins with 0 and is followed by H repetitions of (3,6). Themap
call creates a list of the substrings that begin at the appropriate positions and are$W
= 3 * W + 1 characters long.There is one more adjustment to make before printing the results. If W is odd, then there is an extra character on the second line (
$P[0]
) that needs to bechop
ped off. If W is even, then there is an extra character on the bottom line ($P[-1]
) to chop.Python 2.6 - 144 个字符,包括换行符
如果允许输入以逗号分隔,我可以节省大约 20 个字符。
从命令行获取输入的版本多了 4 个字节:
Python 2.6 - 144 characters including newlines
I can save about 20 more characters if the inputs are allowed to be comma separated.
The version that takes input from the command line is 4 more bytes:
C89(136 个字符)
C89 (136 characters)
Perl,160 个字符
根本不涉及任何聪明之处:只需用字符填充数组,然后剔除那些看起来丑陋的字符。
strager 的杰作移植到 Perl 后只有 137 个字符,但所有功劳都应归于他。
Perl, 160 characters
No cleverness involved at all: just fill the array with characters, then weed out the ones that look ugly.
strager's masterpiece is only 137 characters when ported to Perl, but all credit should go to him.
J, 143 个字符
在处理可变长度字符串和面向控制台的情况下,使用 J 感觉非常尴尬以其他语言假定的用户交互。不过,我想这还不错......
再次窃取想法(一旦你找到一种以数组结构的方式看待问题的方法,J 就更容易使用),这里是mobrule 的杰作移植于 124(糟糕,它比原来的还要长):
J, 143 characters
Using J feels very awkward when dealing with variable-length strings and the sort of console-oriented user interaction that is assumed in other languages. Still, I guess this is not too bad...
Stealing ideas once more (J is much easier to work with once you find a way of looking at the problem in an array-structured way), here's mobrule's masterpiece ported in 124 (ick, it's longer than the original):
C#,216 个字符
较少混淆:
我使用了以下方法:
如果你观察足够大的蜂窝,这种模式就会很明显。一半逻辑仅用于解决第一行、第二行末尾和最后一个单元中的异常。
C#, 216 characters
Less obfuscated:
I used the following method:
The pattern is evident if you look at a large enough honeycomb. Half the logic is there only to address exceptions in the first row, the end of the second row, and the last cell.
Ruby,164
strager 的 Ruby 杰作...
又名
Ruby, 164
strager's masterpiece in Ruby...
aka
NewLisp:257个字符
我确信这不是最佳解决方案:
(silent(define(iv)(println)(set v(int(read-line))))(i'w)(i'h )(set't(+(* 3 w)1))(set'l " __/ \\__/ ")(define(pse(b 0))(println(slice(append(dup" "b)( dup(s 6 l)w))0 e)))(p 0 t)(p 4(- t(% w 2))1)(dotimes(n(- h 1))(p 6 t)(p 9 t))(p 6 t)(p 9(- t(%(+ w 1)2))))
更少混淆:
我确信我可以以不同的方式编写循环并保存两行和一个例如,几个字符,但已经晚了......
NewLisp: 257 chars
I'm sure this is not an optimal solution:
(silent(define(i v)(println)(set v(int(read-line))))(i'w)(i'h)(set't(+(* 3 w)1))(set'l " __/ \\__/ ")(define(p s e(b 0))(println(slice(append(dup" "b)(dup(s 6 l)w))0 e)))(p 0 t)(p 4(- t(% w 2))1)(dotimes(n(- h 1))(p 6 t)(p 9 t))(p 6 t)(p 9(- t(%(+ w 1)2))))
Less obfuscated:
I'm sure I could write the loop differently and save two lines and a few characters, for instance, but it's late...
Golfscript,88 个字符
基于 mobrule 的解决方案。为了让它比那个小,需要做很多工作!换行只是为了清晰起见。
解释:
Here is my original entry at 118 characters:
入场晚了,但它是第二小的! (我只是用这些来学习 Golfscript)。换行是为了清晰起见。
Golfscript, 88 characters
Based on the mobrule's solution. It was a lot of work to get it smaller than that one! Newlines are just for clarity.
Explanation:
Here is my original entry at 118 characters:
Late entry, but it's 2nd smallest! (I'm just using these to learn Golfscript). Newlines are for clarity.
C89 - 261 个必要字符
所有空格都可以删除。我的解决方案使用板的旋转......
C89 - 261 necessary chars
All white spaces can be removed. My solution uses rotation of the board...
F#,303 个字符
编辑
现在终于发布了一些其他答案,我不介意分享一个不太模糊的版本:
F#, 303 chars
EDIT
Now that there are finally some other answers posted, I don't mind sharing a less-obfuscated version:
C# 377 chars
不想让等待“有趣”C# 答案的人失望。
不幸的是,它不是 250 行...;)
C# 377 chars
Didn't want to disappoint anyone waiting for the "funny" C# answer.
Unfortunately, it's not 250 lines though...;)
Groovy,#375 字符
相同的逻辑 & @markt 在 C# 中实现的代码,但为 Groovy 更改了一些地方:)
Groovy, #375 chars
Same logic & code that @markt implemented in c#, but have changed few places for Groovy :)
Lua,227 个字符
208 个字符,当从命令行读取宽度和高度时。
Lua, 227 characters
208 characters, when width and height are read from command line.