病毒与解毒剂代码高尔夫[关闭]


12

有一个矩形2D阵列,其中包含以“ v”表示的病毒,以“ a”表示的antidote1和以“ b”表示的antidote2(除了“ v”,“ a”和“ b”以外没有其他值)。

Antidote1只能杀死水平和垂直方向上的相邻病毒,但是antidote2可以杀死水平,垂直和对角线上的相邻(如果有)病毒。

解毒剂激活后,到底会剩下多少种病毒?

例子:

输入:

vv
vv

输出4

输入:

av
vv

输出1

输入:

vvv
vbv
vvv

输出:0

输入:

bvb
bav
vab
vvv
vvb
vvv
vvv
bva
vav

输出3


2
我感谢您的编辑@Grimy-也很好的挑战:)
pixma140 '19

2
@KevinCruijssen,它不是不规则形状。
Kiara Dan

4
我们可以采用3个(不同的)任意值代替“ v”,“ a”和“ b”吗?
attinat

2
防病毒软件是否可以绕在一起(即,底行中的“ a”会删除顶行中的“ v”)?
布莱恩

2
@Kiara Dan我建议不要使用任何3个不同的值,因为它在挑战中保留了某些特征,并可能在字母的代码点上强制强行编写。
lirtosiast '19

Answers:


8

Python 3,135字节

j=''.join
p='j(s)'+4*'.replace("%s%s","%s%s")'%(*'vbbbbvbbavacvaca',)
f=lambda x:j(eval(2*'(eval(p)for s in zip(*'+'x))))')).count('v')

在线尝试!

-2个字节,感谢Kevin Cruijssen

说明

如果在“ b”旁边找到,则将所有“ v”替换为“ b”。接下来,如果在“ a”旁边找到,则将所有“ v”替换为“ c”。阵列转置版本的第二次迭代清除了所有垂直和对角病毒。最后,它将返回剩余的“ v”数。


作为更具可读性的递归函数(155字节)


3
您可以在之后删除空格y>1else。不错的方法。起初,我不确定这是如何处理对角线的b,但是由于您的替换,这似乎还不错。:)向我+1。
凯文·克鲁伊森

@KevinCruijssen谢谢!如果对角线紧挨着“ b”,则用“ a”代替“ v”来处理。在第二次迭代中,然后删除相邻的“ v”。
吉特(Jitse)

3
对于以下输入,输出应为3,但返回4:bvb bav vab vvv vvb vvv vvv bva vav
Kiara Dan


1
@KevinCruijssen找到了更短的一个,但是您的建议节省了另一个字节!
吉特(Jitse)

5

JavaScript(ES7),108个字节

将输入作为字符矩阵。

f=m=>(g=(y,X,V)=>m.map(r=>r.map((v,x)=>V?v>f&V>'a'>(x-X)**2+y*y-2?r[x]=n--:0:v<f?g(-y,x,v):n++)|y++))(n=0)|n

在线尝试!

与我的原始答案类似,但是V>'a'>(x-X)**2+y*y-2实际执行的时间比使用以下所述的六招少了1个字节。¯\ _(ツ)_ /¯


JavaScript(ES7),109个字节

将输入作为字符矩阵。

f=m=>(g=(y,X,V)=>m.map(r=>r.map((v,x)=>V?v>f&(x-X)**2+y*y<V-8?r[x]=n--:0:v<f?g(-y,x,'0x'+v):n++)|y++))(n=0)|n

在线尝试!

怎么样?

A1=(x1,y1)A2=(x2,y2)

Q(A1,A2)=(x2x1)2+(y2y1)2

考虑整数坐标,它看起来如下:

854585212541145212585458

因此:

  • A1A2Q(A1,A2)<2
  • A1A2Q(A1,A2)<3

238

  • A16810=210
  • B16810=310

已评论

f =                      // named function, because we use it to test if a character
                         // is below or above 'm'
m => (                   // m[] = input matrix
  g = (                  // g is a recursive function taking:
    y,                   //   y = offset between the reference row and the current row
    X,                   //   X = reference column
    V                    //   V = reference value, prefixed with '0x'
  ) =>                   //
    m.map(r =>           // for each row r[] in m[]:
      r.map((v, x) =>    //   for each value v at position x in r[]:
        V ?              //     if V is defined:
          v > f &        //       if v is equal to 'v'
          (x - X) ** 2 + //       and the quadrance between the reference point and
          y * y          //       the current point
          < V - 8 ?      //       is less than the reference value read as hexa minus 8:
            r[x] = n--   //         decrement n and invalidate the current cell
          :              //       else:
            0            //         do nothing
        :                //     else:
          v < f ?        //       if v is either 'a' or 'b':
            g(           //         do a recursive call:
              -y,        //           pass the opposite of y
              x,         //           pass x unchanged
              '0x' + v   //           pass v prefixed with '0x'
            )            //         end of recursive call
          :              //       else:
            n++          //         increment n
      ) | y++            //   end of inner map(); increment y
    )                    // end of outer map()
  )(n = 0)               // initial call to g with y = n = 0
  | n                    // return n

谢谢@Arnauld,你还能解释一下代码吗?
Kiara Dan

3
@KiaraDan完成。
Arnauld

3

05AB1E33 30 29 字节

2F.•s¯}˜?•2ô€Â2ä`.:S¶¡øJ»}'v¢

在线尝试验证更多测试用例

@Jitse的Python 3 Answer的端口,因此请确保对他进行投票!
-1个字节感谢@Jitse

说明:

旧版本的优点是能够压缩/转置字符串列表,其中新版本需要显式的SJ,因为它仅适用于字符列表。但是,通过€Â结合使用较短的压缩字符串,新版本仍要短3个字节。在旧版本中,只会将最后一个值保留在映射内的堆栈上,而在新版本中,它将所有值保留在映射内的堆栈上。

2F                  # Loop 2 times:
  .•s¯}˜?•          #  Push compressed string "vbvabbca"
   2ô               #  Split it into parts of size 2: ["vb","va","bb","ca"]
     €Â             #  Bifurcate (short for duplicate & reverse copy) each:
                    #   ["vb","bv","va","av","bb","bb","ca","ac"]
       2ä           #  Split it into two parts:
                    #   [["vb","bv","va","av"],["bb","bb","ca","ac"]]
         `          #  Push both those lists separated to the stack
          .:        #  Replace all strings once one by one in the (implicit) input-string
            S       #  Then split the entire modified input to a list of characters
             ¶¡     #  Split that list by newlines into sublists of characters
               ø    #  Zip/transpose; swapping rows/columns
                J   #  Join each inner character-list back together to a string again
                 »  #  And join it back together by newlines
}'v¢               '# After the loop: count how many "v" remain

看到这个05AB1E尖矿(部分如何压缩字符串不是字典的一部分吗?理解为什么.•s¯}˜?•"vbvabbca"


你不需要bc=> ba如果你申请bv=> baav=> ac。这样.•6øнãI•(压缩形式的“ bvavbaac”)就足够了,节省了2个字节。
Grimmy

@Grimy对于最后一个测试用例,结果为1而不是3 。
凯文·克鲁伊森

1
@KevinCruijssen两个替换步骤,而不是三个,似乎这样的伎俩毕竟
Jitse

@Jitse谢谢。压缩字符串短了2个字节,但是我现在需要.:(全部替换一次)而不是:(一直替换所有,直到不再存在)。不过仍然为-1。:) 谢谢你让我知道。
凯文·克鲁伊森'19

2

Java的10,211个 209字节

m->{int i=m.length,j,c=0,f,t,I,J;for(;i-->0;)for(j=m[i].length;j-->0;c+=m[i][j]>98?f/9:0)for(f=t=9;t-->0;)try{f-=m[I=i+t/3-1][J=j+t%3-1]==98||Math.abs(I-i+J-j)==1&m[I][J]<98?1:0;}catch(Exception e){}return c;}

修改我的所有单八挑战的答案。
-2个字节感谢@ceilingcat

在线尝试。

说明:

m->{                           // Method with char-matrix parameter & int return-type
  int i=m.length,              //  Amount of rows
      j,                       //  Amount of columns
      c=0,                     //  Virus-counter, starting at 0
      f,                       //  Flag-integer
      t,I,J;                   //  Temp-integers
  for(;i-->0;)                 //  Loop over the rows of the matrix
    for(j=m[i].length;j-->0    //   Inner loop over the columns
        ;c+=                   //     After every iteration: increase the counter by:
            m[i][j]>98         //      If the current cell contains a 'v'
             f/9               //      And the flag is 9:
                               //       Increase the counter by 1
            :0)                //      Else: leave the counter unchanged by adding 0
      for(f=t=9;               //    Reset the flag to 9
          t-->0;)              //    Loop `t` in the range (9, 0]:
         try{f-=               //     Decrease the flag by:
           m[I=i+t/3-1]        //      If `t` is 0, 1, or 2: Look at the previous row
                               //      Else-if `t` is 6, 7, or 8: Look at the next row
                               //      Else (`t` is 3, 4, or 5): Look at the current row
            [J=j+t%3-1]        //      If `t` is 0, 3, or 6: Look at the previous column
                               //      Else-if `t` is 2, 5, or 8: Look at the next column
                               //      Else (`t` is 1, 4, or 7): Look at the current column
            ==98               //      And if this cell contains a 'b'
            ||Math.abs(I-i+J-j)==1
                               //      Or if a vertical/horizontal adjacent cell
              &m[I][J]<98?     //      contains an 'a'
               1               //       Decrease the flag by 1
            :0;                //      Else: leave the flag unchanged by decreasing with 0
         }catch(Exception e){} //     Catch and ignore any ArrayIndexOutOfBoundsExceptions,
                               //     which is shorter than manual checks
  return c;}                   //  And finally return the virus-counter as result

1

木炭,39字节

WS⊞υι≔⪫υ⸿θPθ≔⁰ηFθ«≧⁺›⁼ιv⁺№KMb№KVaηι»⎚Iη

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

WS⊞υι≔⪫υ⸿θPθ

将输入字符串与\r字符连接起来,然后将结果绘制到画布上。

≔⁰η

清除实时病毒数量。

Fθ«

循环输入中的字符。

≧⁺›⁼ιv⁺№KMb№KVaη

如果当前角色是病毒,并且b在任何方向上都没有相邻的s或a与s正交,则增加活病毒的数量。

ι»

重复下一个字符。

⎚Iη

清除画布并打印现场病毒总数。


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.