生日快乐,我们来吃蛋糕吧!


12

这是我朋友的生日,因为他是一名程序员和ASCII艺术爱好者,所以我想我可以给他做一些ASCII蛋糕!

可悲的是,我一直忘了他的年龄,所以我想为我的ASCII烤箱准备一个程序,用指定数量的蜡烛烤蛋糕,所以如果我错了他,我就不必自己再做一次年龄。

ASCII烤箱仅具有有限的内存和存储容量,因此应使用尽可能少的字节


你的任务:

编写一个程序,将一个生日蛋糕输出到控制台,其中蜡烛的数量与输入指定的数量相同。

蛋糕要求是:

  • 它必须具有由水平-和垂直|线以及顶点组成的边界+
  • 至少5个字符(包括蛋糕边框|
  • 高至少5个字符(包括蛋糕边框-
  • 蛋糕边框和第一个烛台(不是火焰)之间必须有一个空白字符,除非该空间中有火焰,否则每边都必须有一个空白字符。火焰或蜡烛基不应与蛋糕边界重叠。
  • 蛋糕的最大宽度为9个字符,因此每行最多5个蜡烛。
  • 由于我们不希望蛋糕是二维的,因此蛋糕必须高2行才能使其具有一定的体积。添加另一个边框底部和连接顶点与上面摆着的,再次使用从上面的ASCII字符(-|+)。

蜡烛的要求是:

  • 由底座|和火焰组成*,火焰堆叠在底座上。
  • 除对角线外,蜡烛可能不会彼此直接相邻。
  • 蜡烛从左到右放置,然后从上到下放置,每行最多5条蜡烛。
    (注意:如果上一行有5支蜡烛,则下一行也不能有5支蜡烛,因为它们将相邻。)

补充说明:

  • 蛋糕的宽度取决于第一行中蜡烛的数量,但宽度必须至少为5个字符,最大为9个字符。
  • 蜡烛从最上一行开始填充,从左到右。如果一行已满,则下一行应从第一行下面的行开始。

输入:

您可以接受自己喜欢的(合理的)格式的数字。

对于这个挑战,即使我不承认这个年龄的人,您也可以假设该数字在0到2 31之间(不包括0)。

输出:

您可以返回一个字符串,也可以将结果蛋糕直接写入输出控制台。

规则:

  • 禁止出现标准漏洞
  • 这是,因此以任何语言表示的最短代码(以字节为单位)将获胜。

例子:

输入: 8

+-----------+
| * * * * * |
| |*|*|*| | |
|  | | |    |
|           |
+-----------+
|           |
+-----------+

输入: 2

+-----+
| * * |
| | | |
|     |
+-----+
|     |
+-----+

输入: 12

+-----------+
| * * * * * |
| |*|*|*|*| |
| *|*|*| |  |
| | | |     |
|           |
+-----------+
|           |
+-----------+

祝好运!


输入可以有多个有效的解决方案吗?
Officialaimm

1
@officialaimm由于存在蜡烛订购和蛋糕尺寸的规范,因此应该不可能。
伊恩·

2
没用的事实:如果您每隔一秒钟而不是每年庆祝一次生日,那么2 ^ 31〜= 68岁。但这每天都会产生很多蛋糕,一段时间后可能会变得乏味。
Arnauld

1
@Arnauld但由于当时它不是你的生日:( ......除非你正在庆祝的一般事实,你出生的时候99%。
伊恩·H.

3
@IanH。假设您正在庆祝自己的开始时间戳。:-)
Arnauld

Answers:


10

木炭76 71 70 66 46字节

NθF=+B⁺³⌊⟦χ⁺θθ⟧÷⁺℅ι⁺θθ⁹↘↘Fθ«↑|*¶¶¿‹⁶﹪⁺ιι⁹«M⁹←↓

在线尝试!链接是详细版本的代码。编辑:感谢@ASCII_Only,节省了1个字节。通过发现绘制蜡烛的巧妙方法节省了20个字节。说明:

NθF=+B⁺³⌊⟦χ⁺θθ⟧÷⁺℅ι⁺θθ⁹

计算整个蛋糕的大小(包括额外的体积)和蛋糕的顶部,以便可以绘制它们。((== ASCII 61)=(+= ASCII 43)+ 9 * 2用于额外的音量。)

↘↘Fθ«

将光标移到5根蜡烛的第一行。循环通过每个蜡烛。

↑|*¶¶

打印一支蜡烛,然后向右移动两个字符以表示下一支蜡烛。

¿‹⁶﹪⁺ιι⁹«

但是,在(零索引)第4、8、13、17、22等蜡烛(位于行末)之后,

M⁹←↓

将光标移到下一行的第一个蜡烛上。这适用于奇数行和偶数行!


1
您的解决方案在输入数小于6的地方添加了(不需要的)额外行::)
Ian H.

@IanH。抱歉,出于某些原因,我认为这是最低高度。解决这个问题实际上为我节省了4个字节!
尼尔

仍然在TIO上显示错误:/
Ian H.

1
@IanH。在线尝试!给我的输出与您的示例相同...
Neil

1
@Neil记住您在一开始就不需要:P(感谢您找到另一个高尔夫球手(不太好)的情况)
仅使用ASCII格式,

3

果冻,67 字节

s9s€5Ẏa;⁶;⁶z⁶Z
ç”|ṙ-ż"ç”*$U⁸RḤ’¤¦Ẏ€j@€“| “|”Zj@€⁾--Z”+®¦€0,1©¦;ṫ¥-Y

一个带数字并返回字符列表的单子链接,或打印输出的完整程序。

在线尝试!

怎么样?

s9s€5Ẏa;⁶;⁶z⁶Z - Link 1, make some candle parts & topping: number, age; character, part
s9             - split (implicit range(age)) into chunks of 9 (or remainder)
  s€5          - split each chunk of 9 into chunks of 5 (a 5 and a 4 or remainder)
     Ẏ         - tighten (to a list of lists of length 5, 4, 5, 4, ..., remainder)
      a        - logical and with the part character (either | or * here)
       ;⁶      - concatenate a space (we all still want topping when no candles)
         ;⁶    - ...and another (we also want some extra topping below the last row)
           z⁶  - transpose with filler space (fill the top with topping!)
             Z - transpose (put it back around the right way again chef)

ç”|ṙ-ż"ç”*$U⁸RḤ’¤¦Ẏ€j@€“| “|”Zj@€⁾--Z”+®¦€0,1©¦;ṫ¥-Y - Main link: number, age
ç”|                                                  - call last link (1) as a dyad with '|'
   ṙ-                                                - rotate left by -1
          $                                          - last two links as a monad:
       ç”*                                           -   call (1) as a dyad with '*'
      "                                              - zip with the dyadic operation:
     ż                                               -   zip (interleave each)
                 ¦                                   - sparse application:
           U                                         - ...of: upend (reverse each)
                ¤                                    - ...to indexes: nilad+links as a nilad:
            ⁸                                        -   chain's left argument, age
             R                                       -   range
              Ḥ                                      -   double (vectorises)
               ’                                     -   increment
                  Ẏ€                                 - tighten €ach (from '|*' or '*|' pairs)
                       “| “|”                        - literal ["| ", "|"]
                    j@€                              - join (swap arguments) for €ach (add a little extra topping to the left, and add piping to the sides)
                             Z                       - transpose
                                 ⁾--                 - literal "--"
                              j@€                    - join (swap arguments) for €ach (add piping to the top and bottom edges)
                                    Z                - transpose (time to invest in a potters wheel!)
                                              ¦      - sparse application:
                                          0,1        - ...to indexes: [0,1] (both ends)
                                             ©       -   (copy that to the register)
                                         €           - ...of: for each:
                                        ¦            -   sparse application:
                                     ”+              -   ...of: '+' character
                                       ®             -   ...to indexes: recall from register (both ends)
                                                  -  - literal -1
                                                 ¥   - last two links as a dyad
                                                ṫ    -   tail from index (gets last two rows)
                                               ;     -   concatenate (repeats them underneath)
                                                   Y - join with newlines
                                                     - as a full program: implicit print

1
击败木炭的圣物+1
仅ASCII码,

@仅ASCII抱歉,我发现保存了4个字节……
Neil

@Neil很好,这也很好,木炭毕竟应该擅长ASCII艺术(胜于果冻):P
仅ASCII的算法,

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.