编程高尔夫:玩魔方
挑战
按字符数计算的最短代码,将根据用户输入输出积木塔系列。
输入将是一系列数字(正数、负数和零),表示当前立方体塔在其索引之后的高度。高度为 0 表示没有塔并且是间隔的。
立方体塔由堆叠的立方体组成。如果当前索引上的输入数字为正,则立方体上升,如果输入数字为负,则立方体下降。使用以下 4 条线绘制单个立方体:
__ /__ /| | | | |___|/
立方体是 3D - 这意味着当两个塔彼此相邻放置时,它们会相互隐藏,从而生成假透视。
可以假定所有输入都是有效且没有错误的 - 每个数字在一行上用空格分隔,并且至少有一个数字。
测试用例
Input: 2 -3 -2 1 2 -1 Output: __ __ /__ /| /__ /| | | | _| | | |___|/| /__|___|/| | | |__ _| | | |__ |___|/__ /__|___|___|/__ /| | | | | | | | |___|___|/| |___|/ | | | | |___|___|/ | | | |___|/
Input: 1 2 3 4 -2 4 3 2 1 Output: __ __ /__ /| /__ /| _| | | | | |__ /__|___|/| |___|/__ /| _| | | | | | | |__ /__|___|___|/| |___|___|/__ /| _| | | | | | | | | |__ /__|___|___|___|/| |___|___|___|/__ /| | | | | | |_| | | | | | |___|___|___|___|/__|___|___|___|___|/ | | | |___|/| | | | |___|/
Input: 1 3 3 7 0 -2 -2 Output: __ /__ /| | | | |___|/| | | | |___|/| | | | |___|/| __ _| | | /__ /__|___|/| | | | | | |___|___|___|/| _| | | | | /__|___|___|___|/| | | | | | | __ __ |___|___|___|___|/ /__ /__ /| | | | | |___|___|/| | | | | |___|___|/
代码计数包括输入/输出(即完整程序)。
The challenge
The shortest code by character count, that will output playing bricks tower series according to user input.
The input will be a series of numbers (positive, negative and zero) that represents the height of the current cube tower following their index. A height of 0 means no tower and is spaced.
A cube tower is composed of stacked cubes. If the input number on the current index is positive, the cubes go up, if the input number is negative, the cubes go down. A single cube is drawn using the 4 following lines:
__ /__ /| | | | |___|/
Cubes are 3D - this means they hide each other when two towers are placed next to each other, generating fake perspective.
All input can be assumed to be valid and without errors - Each number is separated with a white space on a single line, with at least one number.
Test cases
Input: 2 -3 -2 1 2 -1 Output: __ __ /__ /| /__ /| | | | _| | | |___|/| /__|___|/| | | |__ _| | | |__ |___|/__ /__|___|___|/__ /| | | | | | | | |___|___|/| |___|/ | | | | |___|___|/ | | | |___|/
Input: 1 2 3 4 -2 4 3 2 1 Output: __ __ /__ /| /__ /| _| | | | | |__ /__|___|/| |___|/__ /| _| | | | | | | |__ /__|___|___|/| |___|___|/__ /| _| | | | | | | | | |__ /__|___|___|___|/| |___|___|___|/__ /| | | | | | |_| | | | | | |___|___|___|___|/__|___|___|___|___|/ | | | |___|/| | | | |___|/
Input: 1 3 3 7 0 -2 -2 Output: __ /__ /| | | | |___|/| | | | |___|/| | | | |___|/| __ _| | | /__ /__|___|/| | | | | | |___|___|___|/| _| | | | | /__|___|___|___|/| | | | | | | __ __ |___|___|___|___|/ /__ /__ /| | | | | |___|___|/| | | | | |___|___|/
Code count includes input/output (i.e full program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
Perl 157 个字符
这个条目的灵感来自于 gnibbler 的 Ruby 条目,包括关于在代码中嵌入立方体的部分。还要感谢 Kinopiko 指导我学习
substr
的 4 arg 版本。说明:
第 1 行:选择立方体的去向
第 2-5 行:将立方体放在立方体所在的位置,打印
Perl 157 characters
This entry was inspired by gnibbler's Ruby entry, including the part about embedding the cube in the code. Also thanks to Kinopiko for schooling me on the 4 arg version of
substr
.Explanation:
Line 1: Choose where cubes go
Lines 2-5: Put cubes where cubes go, print
Ruby - 233 个
可变字符串;)没有任意的“99”限制。噗:p
不得不丢失源中立方体的图片
虽然Ruby - 243
删除我放入其中以获得立方体的漂亮图片的额外空间
Ruby - 233
mutable strings ;) No arbitrary "99" limits. pfft :p
Had to lose the picture of the cube in the source though
Ruby - 243
Delete the extra space I put in there to get the nice picture of the cube
Python - 249
第二个 for 循环用 tab 缩进
Python - 393
Python - 249
The second for loop is indented with tab
Python - 393
Ruby,
261 258 250242Run with
ruby -n -a v2.rb
此条目设计得有点过度,因为它可以在任何级别开始和结束堆栈,而不仅仅是在0. 在“竞赛版本”中无法指定这一点,但如果用
c=eval $_;n=99
替换前 4 行,那么它会下降到 203 字节 你可以这样做:<Ruby,
261 258 250242Run with
ruby -n -a v2.rb
This entry is slightly overdesigned in that it can begin and end stacks at any level, not just at 0. There is no way to specify this in the "competition version", but if you replace the first 4 lines with
c=eval $_;n=99
then it drops to 203 bytes and you can do: <Befunge-93(字符太多)
非常未优化。我的第一个 Befunge 程序。 =]
Befunge-93 (too many characters)
Very unoptimized. My first Befunge program. =]
Haskell,349 个字符:
Haskell, 349 characters:
Golfscript - 154
条换行符很重要。如果您未能去掉尾随的换行符,您将在底部打印一个额外的数字!
-
和0
之间有一个 TABGolfscript - 163
Golfscript - 165
Golfscript - 154
Newlines are significant. If you fail to strip the trailing newline you will get an extra number printed at the bottom! There is a TAB between
-
and0
Golfscript - 163
Golfscript - 165
C,287 个字符
(此字符数不包括两个换行符)
这作为命令行程序运行,例如
./cubes 1 2 3 4 5 0 1 3 2 -1 -10
您可以尝试在此处运行它:
://codepad.org/tu4HDqSy" rel="nofollow noreferrer">http://codepad.org/tu4HDqSy (此版本已更改, 对于非高尔夫,是通过
cpp
运行它,然后indent
:C, 287 characters
(this character count excludes two newlines)
This runs as a command line program, like
./cubes 1 2 3 4 5 0 1 3 2 -1 -10
You can try running it here: http://codepad.org/tu4HDqSy (This version is altered because codepad.org doesn't allow command line arguments.)
A handy tip for un-golfing is to run it through
cpp
and thenindent
:Python (2.6),
1092 905 623 501 478 345--> 318 个字符欢迎大家留言!
Python (2.6),
1092 905 623 501 478 345--> 318 charactersAll comments welcome!
VB.NET 9 的 627 字节(不计算新行)
627 bytes of VB.NET 9 (without counting new lines)
313 字节的 Ruby
(有一个换行符不算在内,它之所以存在,是因为否则那里会有很长的一行。)
我使用了制作二维字符数组的明显方法然后一次一个地粘贴立方体,从下到上,从左到右。
313 bytes of Ruby
(There is one newline that doesn't count, it's only there because otherwise there would be a very long line in there.)
I used the obvious method of making a 2D array of characters and pasting in the cubes one at a time, bottom to top and left to right.
Lua,453 个字符
Lua, 453 characters
PHP -
447398我知道它不像其他的那么短,但我对此很满意;)
输入/输出
PHP -
447398I Know it's not as short as the others here, but I'm happy with it ;)
Input / Output