自然日志上的蚂蚁


23

这是一个很好的初学者挑战,也是一个很好的时间杀手。

我只说了-自然-对数,因为标题太短了,这与对数无关。

给定2个变量:

  • 蚂蚁数量n
  • 日志的宽度w

输出日志宽度wn蚁(实施例示出w=3n=6

|             |
| \O/ \O/ \O/ |
| -O- -O- -O- |
| /o\ /o\ /o\ |
|  ^   ^   ^  |
|             |
| \O/ \O/ \O/ |
| -O- -O- -O- |
| /o\ /o\ /o\ |
|  ^   ^   ^  |
|             |

一只蚂蚁看起来像这样:

\O/ # Upper-case O
-O- # Upper-case O
/o\ # Lower-case o
 ^  

一些蚂蚁法则:

  1. 蚂蚁可能不会彼此接触,也不会直接接触日志的边缘,它们更喜欢接触空格。
  2. 每行蚂蚁必须很w宽,并n/w有成排的蚂蚁。
  3. 蚂蚁总是需要一个日志,保证日志宽度大于0。
  4. 蚂蚁也...好吧,需要蚂蚁,蚂蚁的数量大于0,有保证。
  5. 蚂蚁的组织也令人惊讶,它们会从左到右,从上到下填充日志。好像他们在读书。

蚂蚁样本

w = 3,n = 5

|             |
| \O/ \O/ \O/ |
| -O- -O- -O- |
| /o\ /o\ /o\ |
|  ^   ^   ^  |
|             |
| \O/ \O/     |
| -O- -O-     |
| /o\ /o\     |
|  ^   ^      |
|             |

w = 1,n = 1

|     |
| \O/ |
| -O- |
| /o\ |
|  ^  |
|     |

w = 1,n = 3

|     |
| \O/ |
| -O- |
| /o\ |
|  ^  |
|     |
| \O/ |
| -O- |
| /o\ |
|  ^  |
|     |
| \O/ |
| -O- |
| /o\ |
|  ^  |
|     |

这是,字节数最少的人获胜。



@AdmBorkBork我现在记得这一点。我完成了一半,并误读了规范,可能会在潜意识中偷走了该想法的一部分。
Magic Octopus Urn'Apr

这些是ant-y-log。
Arjun

“每行蚂蚁必须宽”。如果w>n应该有正确的前手是额外的空间|或者没有,或者是它给我们吗?
乔纳森·艾伦,

1
-1 Ant-xamples(我讨厌双关语)
caird coinheringaahing

Answers:


4

果冻 44  43 字节

划掉44不是使用44  

⁶ṁ;⁶jЀ“\-/“OOo^ ”;UṖz⁶¤Y
sÇ€⁶;YỴz⁶Zj@€⁾||Y

在线尝试!

怎么样?

⁶ṁ;⁶jЀ“\-/“OOo^ ”;UṖz⁶¤Y - Link 1, make a row of ants: list x (could be integer = length)
⁶                         - literal ' '
 ṁ                        - mould like x (makes a list of that many spaces)
  ;⁶                      - concatenate one more space
                       ¤  - nilad followed by link(s) as a nilad
       “\-/“OOo^ ”        -   literal      ["\-/","OOo^ "] ("..." a list of chars really)
                   U      -   reverse each [" ^oOO","/-\"]
                  ;       -   concatenate  ["\-/","OOo^ "," ^oOO","/-\"]
                    Ṗ     -   pop          ["\-/","OOo^ "," ^oOO"]
                      ⁶   -   literal ' '
                     z    -   transpose & fill ["\O/","-O-","/o\"," ^ ","   "]
    jЀ                   - join left mapped over right
                          -   (join the spaces with each of the ant parts in turn)
                        Y - join with newlines

sÇ€⁶;YỴz⁶Zj@€⁾||Y - Main link: n, w
s                 - split n into chunks of length w (implicitly makes a range of length n)
 Ç€               - call the last link (1) as a monad for €ach
   ⁶;             - a space concatenated with that
     Y            - join with newlines
      Ỵ           - split at newlines (both the ones we just joined with AND the others!)
       z⁶         - transpose & fill with space characters (making the shorter rows,
                  -   including the single space as long as the longest one)
         Z        - transpose it back the right way
             ⁾||  - literal ['|','|']
          j@€     - join with reverse arguments for €ach (put each row between pipes)
                Y - join back up with newlines
                  - implicit print

我已经w<n评论中提出了疑问,因为它有点模棱两可。
如果日志需要是w蚂蚁宽的,而不仅仅是蚂蚁w宽,那么它将花费两个字节:

⁶ṁ;⁶jЀ“\-/“OOo^ ”;UṖz⁶¤Y
+RsÇ€YỴz⁶Zj@€⁾||ṫ5Y

这样做与之前相同,除了在前面加上一个空格来制作第一行空白行之前,它创建了一整行蚂蚁,并砍掉除了尾随的空白行以外的所有行。


5

V70,68个字节

i \O/ 
 -O- 
 /o\ 
  ^  Àä{ò@bf }C GïpòÇÓ/d
HÄÒ çÞ/ÙÒ 
ëI|yê$p

在线尝试!

00000000: 6920 5c4f 2f20 0a20 2d4f 2d20 0a20 2f6f  i \O/ . -O- . /o
00000010: 5c20 0a20 205e 2020 1bc0 e416 7bf2 4062  \ .  ^  ....{.@b
00000020: 6620 167d 4320 1b47 ef70 f2c7 d32f 640a  f .}C .G.p.../d.
00000030: 48c4 d220 e7de 2fd9 d220 0a16 eb49 7c1b  H.. ../.. ...I|.
00000040: 79ea 2470                                y.$p

这以前从未发生过,但是一个已知的错误实际上为我节省了字节!

确切说明发生了什么是很困难的,但是不幸的是,当您尝试按列复制某些内容时,V将在复制之前将一列移到另一列。这就是为什么我最初这样做的原因:

h<C-v>{dÀp

不使用重复运算符。但是,由于我们已经需要移动一行,因此我们只需

hÀä<C-v>{

真好!漏洞利用的潜在提名:P。
Magic Octopus Urn


5

Python 2,144字节

n,w=input()
s=' ';k='|';a=k+s*w*4+s+k;print a
while n>0:
 for i in['\\O/','-O-','/o\\',' ^ ']:print k+s+(i+s)*min(w,n)+s*4*(w-n)+k
 n-=w;print a

在线尝试!


这是无效的,因为它错过了蚂蚁之间的空行(将示例w=1n=3您的版本进行比较)。
FatalMerlin

@FatalMerlin谢谢!固定的:)
数学迷

4

05AB1E,47个字节

"   \O/-O-/o\ ^ "5äðìI.D)IôvyøJ'|ì})˜¬¸«.B„ |«»

在线尝试!

说明

"   \O/-O-/o\ ^ "                                # push the ant-string
                 5ä                              # split into 5 parts
                   ðì                            # prepend a space to each
                     I.D                         # copy input-1 number of times
                        )                        # wrap in a list
                         Iô                      # split into parts each the size of input-2
                           v                     # for each row of ants
                            yø                   # zip, so body parts are on the same row
                              J'|ì               # join to string and prepend a pipe to each
                                  }              # end loop
                                   )˜            # wrap in a flattened list
                                     ¬¸«         # append a copy of the first row (spaces)
                                        .B       # pad rows to equal length with spaces
                                          „ |«   # append " |" to each row
                                              »  # merge on newlines

4

SOGL74 71 74 字节

 |pe4*I@*o |o→L:"╔O¦‘2n;"nΤ↕¬┐α┐PΝld‘*┼eG-’⁵@**┼ |4*┼OL→ALbe÷:?{eA}}be%:?A

第一部分:输出空日志部分的函数

             →L  define function L
 |p              output in a newline "|"
   e4*           multiply width by 4
      I          increace that
       @*        get that many spaces
         o       append [to current line] that
           |o    append "|"

第二部分有一个压缩字符串"nΤ↕¬┐α┐PΝld‘,即ant。解压缩为\-/ OOo^/-\ 。那是蚂蚁(右边有间距),但是像从上到下然后到右边一样

159d
26ae
37bf
48cg

之所以这样存储,是因为该函数会附加这样的字符串(这是因为您可以将字符串乘以在其中添加多个实例)。该部分本身:函数,该函数要求在堆栈上显示一个数字以表示要绘制多少只蚂蚁。

                                    Example input: width 3, on stack 2
:                                   duplicate the input                             [2,2]
 "╔O¦‘                              push "| | | | "                                 [2,2,"| | | | "]
      2n                            split into chunks of two                        [2,2,["| ","| ","| ","| "]]
        ;                           put one of the input copies ontop of the stack  [2,["| ","| ","| ","| "], 2]
         "...‘*                     multiply that many ants                         [2,["| ","| ","| ","| "], "\\-/ OOo^/-\\     \\-/ OOo^/-\\     "]
               ┼                    add horizontally the ants                       ["| \O/ \O/ \O/ ",
                                                                                     "| -O- -O- -O- ",
                                                                                     "| /o\ /o\ /o\ ",
                                                                                     "|  ^   ^   ^  "]
                e                   get the width                                   [["| \\O/ \\O/ \\O/ ", "| -O- -O- -O- ", "| /o\\ /o\\ /o\\ ", "|  ^   ^   ^  "], 3]
                 G-                 subtract input from it                          [["| \\O/ \\O/ \\O/ ", "| -O- -O- -O- ", "| /o\\ /o\\ /o\\ ", "|  ^   ^   ^  "], 1]
                   ’⁵@*             push 16 spaces                                  [["| \\O/ \\O/ \\O/ ", "| -O- -O- -O- ", "| /o\\ /o\\ /o\\ ", "|  ^   ^   ^  "], 1, "                "]
                       *            multiply [the 16 spaces and empty place count]  [["| \\O/ \\O/ \\O/ ", "| -O- -O- -O- ", "| /o\\ /o\\ /o\\ ", "|  ^   ^   ^  "], "                "]
                        ┼           add that horizontally                           [["| \\O/ \\O/ \\O/     ", "| -O- -O- -O-     ", "| /o\\ /o\\ /o\\     ", "|  ^   ^   ^      "]]
                          |4*┼      add 4 vertical bars to the array                [["| \\O/ \\O/ \\O/     |", "| -O- -O- -O-     |", "| /o\\ /o\\ /o\\     |", "|  ^   ^   ^      |"]]
                              O     output the array                                []
                               L    call the empty line function                    []
                                →A  define as A

和主要功能:

L                  call the empty line drawing function
 be÷               push floor(b/e) (the amount of full lines)
    :?{eA}}        that many times call A with the full width on the stack
           be%     push b%e (the leftovers)
              :?   if truthy (aka !=0)
                A   call A with for the leftovers

2

Perl 5,159个字节

($w,$n)=@ARGV;
print
$_%$w?"":"| ",
['   \O/-O-/o\\ ^ '=~/.../g]->[($_<5*$w*int$n/$w||$_%$w<$n%$w?$_/$w:0)%5],
($_+1)%$w?" ":" |\n"
for 0..$w*(6+5*int(($n-1)/$w))-1

在线尝试!

Perl 5,152个字节

另一个基于Python解决方案的解决方案:

($w,$n)=@ARGV;
$b=' 'x($w*4+1);$j=$a="|\n|";
map$j.=' '."$_ "x($w<$n?$w:$n).'    'x($w-$n).$a,'\\O/','-O-','/o\\',' ^ 'and$n-=$w
while$n>0;
print"|$b$j$b|"

在线尝试!


0

Mathematica 210字节

StringRiffle[If[#2==c||#2==1,"|",If[r-#1<6&&#2>2+4 (a+w-h w),Table[" ",5,4],Characters@"    \\O/ -O- /o\\  ^  "~Partition~4][[1+Mod[#1-1,5],1+Mod[#2+1,4]]]]&~Array~{r=5(h=⌈(a=#)/(w=#2)⌉)+1,c=4w+3},"\n",""]&

认为我需要制作基于Mathematica的高尔夫语言。


0

Python 2,166字节

w,n=input()
print'\n'.join(['|'+' '*w*4+' |']+[' '.join(['|']+[p]*r+['   ']*(w-r)+['|'])for r in[w]*(n/w)+[[],[n%w]][n%w>0] for p in['\O/','-O-','/o\\',' ^ ','   ']])

0

木炭,43字节

NθF⪪× Nθ«←P↓⁶M⊗⊕⊗θ→P↓⁶⸿E⪪\O/-O-/o\ ^ ³⭆ι⁺μκ

在线尝试!链接是详细版本的代码。说明:

Nθ

输入w

F⪪× Nθ«

输入n,然后创建一串n空格并将其分成若干行w(除了最后一块可能较小)。循环遍历这些行。

←P↓⁶M⊗⊕⊗θ→P↓⁶⸿

打印日志部分的两面。

  \O/-O-/o\ ^           Literal string
 ⪪            ³         Split into (4) pieces of length 3
E                       Map over each piece
               ⭆ι       Map over each row space
                 ⁺μκ    Appending the piece
                        Implicitly print the results on separate lines
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.