男修道士模拟器


73

Ť他从圣Golfus简明的顺序弟兄背诵简短的祷告时,他们看到有人做的传统的交叉的迹象。由于最近在游客中衡量的犯罪率很高,他们在修道院里安装了闭路电视,并雇用您来帮助他们在AI时代保持古老的传统。

您的任务是分析男修道士手指跟踪软件的输出,并告诉您应进行的祈祷次数。输入是一个包含0到4之间的整数的矩阵。1,2,3,4表示手指在连续时间点的位置。0表示非手指。

唯一正确的方法TM过自己是:

.1.
3.4
.2.

(“。”匹配任何数字)。但是,由于摄像机旋转的不确定性以及人群中虔诚的东正教弟兄的存在(One True Way TM的横向方向相反),您也应该计算所有旋转和反射:

.4. .2. .3. .1. .3. .2. .4.
1.2 4.3 2.1 4.3 1.2 3.4 2.1
.3. .1. .4. .2. .4. .1. .3.

一位数字可能是多个十字架的一部分。.pray()通过计算上面3x3子矩阵中有多少个,帮助男修道士确定其AI的次数。编写程序或函数。以任何合理方便的形式输入信息。

遗嘱案例:

// in
[[0,4,2,0],
 [1,3,2,4],
 [2,3,1,0]]

// out
2

// in
[[4,3,3,2,4,4,1,3,2,2],
 [0,3,0,2,1,1,2,3,2,3],
 [0,3,1,3,2,4,3,3,1,1],
 [4,3,2,3,2,4,1,4,2,3],
 [0,4,2,3,4,0,2,3,2,4],
 [2,1,0,0,2,0,0,1,2,4],
 [4,0,3,1,3,2,0,3,2,3],
 [1,4,3,3,1,4,0,1,4,4],
 [0,2,4,3,4,3,1,3,0,4],
 [3,0,1,0,4,0,3,3,3,3]]

// out
3

// in
[[3,2,3,1,0,3,4,2,1,1,1,1,4,0,1,3,1,1,2,1,1,3,0,1,0,1,1,0,0,1,0,3,4,0,1,1,2,3,1,2,4,1,0,2,3,0,2,4,3,2],
 [2,4,1,1,0,3,0,2,4,2,3,2,1,3,0,2,3,2,4,4,4,3,2,1,1,3,2,1,2,3,2,4,0,3,1,4,4,1,1,0,1,1,0,2,2,3,1,2,0,2],
 [3,4,0,0,4,4,0,3,4,4,1,3,2,1,3,2,3,2,2,0,4,0,1,2,3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,4,1,4,4,3,2,0,3,0,3,0,1,1,2,1,3,0,4,4,2,2,2,1,3,4,1,1,1],
 [3,0,1,4,2,0,0,3,1,1,1,4,4,0,2,2,0,4,0,3,1,0,2,2,4,4,4,0,4,4,4,4,4,4,3,0,4,4,4,1,2,4,4,3,0,0,4,0,4,2],
 [2,0,1,2,1,1,3,0,3,1,0,4,3,1,2,1,1,3,0,1,2,4,2,1,2,3,4,2,4,4,2,2,3,4,0,0,1,0,0,4,1,3,3,4,1,2,1,3,3,2],
 [4,0,2,0,3,1,2,1,1,1,1,2,3,0,3,1,0,4,3,0,0,0,2,0,1,4,0,2,1,3,4,2,2,4,2,3,1,2,0,2,0,2,4,0,1,2,3,4,1,3],
 [3,0,2,4,2,0,3,4,3,2,3,4,2,0,4,1,0,4,3,3,1,0,2,2,2,1,3,3,1,1,0,3,3,0,3,2,1,1,0,1,2,2,0,4,4,2,0,1,3,1],
 [0,4,4,4,0,3,0,3,0,2,2,0,1,2,3,3,4,3,0,4,1,2,3,3,0,2,2,3,0,0,0,2,4,2,3,4,2,3,4,0,2,0,1,1,3,4,2,2,4,4],
 [2,1,2,3,4,3,1,2,0,0,0,0,0,0,3,4,3,3,1,2,2,1,3,4,1,2,4,0,1,4,1,0,0,0,2,1,1,1,3,0,0,3,1,1,4,2,1,3,4,1],
 [1,0,3,0,2,1,4,2,3,3,1,1,3,4,4,0,1,2,1,3,0,3,1,1,3,0,2,4,4,4,2,3,1,4,3,4,0,1,4,1,1,1,4,0,0,2,3,4,0,4]]

// out
8

“最矮的人有福了,因为它赢得了投票国的胜利。” -圣戈尔弗斯书13:37

“你不应该使用漏洞,因为漏洞是路西法的邪恶作品。” -给Meta的信13:666

制作十字架的人的轻微可视化


感谢@Tschallacka的可视化。


57
最后+1节经文,他们给了我一个好笑:D
HyperNeutrino '18

6
一位数字可以成为多个十字架的一部分吗?
马丁·恩德

9
亲爱的耶稣,您确实制作了男修道士模拟器。
魔术章鱼缸

1
为什么第一个“ T”链接在一起?
JakeGould

4
@JakeGould以便将此问题链接到“ T”导致的问题。
暴民埃里克(Erik the Outgolfer)'18年

Answers:


19

污垢,20字节

n`.\1./\3.\4/.\2.voO

在线尝试!

规范的非常文字化的实现:

  • n` 使Grime计数产生匹配的输入子矩形的数量。
  • .\1./\3.\4/.\2. 定义3x3正方形:

    .1.
    3.4
    .2.
    

    哪里.可以是任何字符。

  • oO是一个方向修改器,它可使该矩形以任何旋转或反射方式出现。该v用于降低o小号优先,因此,我们不需要在广场周围的括号。

7
对前两个测试更正,对最后一个测试更正。我想只要程序原则上是正确的并且仅受资源限制,就应该可以。
ngn

仅供参考:我刚刚测试了最后一个测试用例,它对我来说运行良好。
3D1T0R '18

16

蜗牛17 16字节

Ao
\1.=\2o=\3b\4

在线尝试!

说明

A使Snails计算整个输入中可能匹配的路径数。o将起始方向设置为任何正交方向(而不是仅向东)。模式本身在第二行:

\1    Match a 1.
.     Match an arbitrary character (the one in the centre).
=\2   Check that the next character ahead is a 2, but don't move there.
o     Turn to any orthogonal direction. In practice, this will only be one
      of the two directions from the centre we haven't checked yet.
=\3   Check that the next character ahead is a 3.
b     Turn around 180 degrees.
\4    Match a 4 in that direction.

7

Haskell中,108个 102 93字节

f((_:t:x):w@((l:q@(_:r:_)):(_:b:z):_))=sum$f((t:x):q:[b:z]):f w:[1|(l*r-1)*(t*b-1)==11]
f _=0

在线尝试!

没有正则表达式。模式匹配

.t.
l.r
.b.

在矩阵的左上角,取一个1if,(l*r-1)*(t*b-1)==11然后递归地移至右边(下拉.l.)和向下(下拉第一行)。如果无法匹配图案(在右侧或底部边框),请使用0。对所有结果求和。

编辑:-9字节感谢@xnor。


我本来建议2^(l*r)+2^(t*b)==4100检查数字,但似乎我们的解决方案都被2,6代替了3,4
xnor

@xnor,但数量仅限于0..4
ousur

3
@衷,谢谢,我错过了。然后,可以进一步优化算术表达式,例如(l*r-1)*(t*b-1)==11
xnor

7

Perl,70个字节

包括+2 0p

在STDIN上将输入矩阵表示为没有空格的数字块:

perl -0pe '$_=map{/.$/m+y/1-4/3421/;/(?=1.{@{-}}(3.4|4.3).{@{-}}2)/sg}($_)x4'
0420
1324
2310
^D

通过旋转数字来旋转十字架


6

视网膜92 83字节

L$v`(?<=((.))*).(.)..*¶(?<-1>.)*(.).(.).*¶(?<-2>.)*.(.)
$6$3$4$5
/../_O`.
1234|3412

在线尝试!说明:

L$v`(?<=((.))*).(.)..*¶(?<-1>.)*(.).(.).*¶(?<-2>.)*.(.)
$6$3$4$5

查找所有重叠的3x3正方形。后向捕捉两次捕获缩进,以便可以在第二行和第三行上进行平衡。假定输入为矩形,因此我们不必检查组平衡。然后捕获底部/顶部中间单元格和左侧/右侧中间单元格。

/../_O`.

将每对单元格排序。

1234|3412

计算剩余的有效模式。


5

果冻,26个字节

Z3Ƥṡ€3ẎµFḊm2ṙ-s2Ṣ€ṢFµ€ċ4R¤

在线尝试!

说明

Z3Ƥṡ€3ẎµFḊm2ṙ-s2Ṣ€ṢFµ€ċ4R¤  Main Link
 3Ƥ                         For each 3 rows
Z                           Zip the rows
   ṡ€3                      Get all arrangements of 3 consecutive columns from these rows. At this step, we have all 3x3 contiguous submatrices
      Ẏ                     Unwrap by one layer; previously grouped by rows, now ungrouped
       µ           µ€       For each 3x3 submatrix
        F                   Flatten it
         Ḋ                  Get rid of the first element
          m2                Take every second element (this only keeps the edges)
            ṙ-              Rotate right 1 element
              s2            Group into pairs; now the top and bottom are grouped and the left and right are grouped
                Ṣ€          Sort each pair
                  Ṣ         Sort the pairs
                   F        Flatten
                      ċ4R¤  Count the number of occurrences of range(4); i.e. [1,2,3,4]

-3个字节,感谢Jonathan Allan(2)和Xcoder先生(3)(合并)


@ngn大声笑点的意思是在D。我的坏,固定的。
HyperNeutrino

使用...Fµ€ċ4R¤而不是保存2个字节...Ḍµ€ċ1234(也请注意,1234可能已替换⁽¡ḋ为保存一个字节)
Jonathan Allan '18

使用Z3Ƥ代替ṡ3Z€⁼J$µ€S代替26个字节Ḍµ€ċ1234
Xcoder先生18年

@JonathanAllan哦,太好了,谢谢
HyperNeutrino

5

Java的8,135个 133 131字节

m->{int r=0,i=0,j;for(;++i<m.length-1;)for(j=1;j<m[i].length-1;)if(~(m[i-1][j]*m[i+1][j])*~(m[i][j-1]*m[i][++j])==39)r++;return r;}

-2字节由于@tehtmi较短式:(l*r-1)*(t*b-1)==11~(l*r)*~(t*b)==39

说明:

在线尝试。

m->{                     // Method with integer-matrix parameter and integer return-type
  int r=0,               //  Result-integer, starting at 0
      i=0,j;             //  Index integers
  for(;++i<m.length-1;)  //  Loop over the rows, skipping the first and last
    for(j=1;j<m[i].length-1;)
                         //   Inner loop over the columns, skipping the first and last
      if(~(m[i-1][j]*m[i+1][j])*~(m[i][j-1]*m[i][++j])==39)
                         //    If (-(bottom*top)-1) * (-(left*right)-1) is exactly 39:
        r++;             //     Increase the result-counter by 1
  return r;}             //  Return the result-counter

1
较短的公式:~(l*r)*~(t*b)==39(Haskell显然没有1个字节~。)
tehtmi

3

外壳,23个字节

#ö§&¦ḣ4SδΛ≈↔Ċ2tΣṁoX3TX3

在线尝试!

说明

#ö§&¦ḣ4SδΛ≈↔Ċ2tΣṁoX3TX3  Implicit input, a list of lists of integers.
                     X3  Length-3 slices.
                ṁ        Map over them and concatenate:
                    T     Transpose,
                 oX3      then get length-3 slices.
                         Now we have a list of 3x3-arrays of the (transposed) input.
#ö                       Return number of 3x3-arrays that satisfy this:
                          Take m = [[0,3,0],[2,4,1],[1,4,0]] as an example.
               Σ          Concatenate: [0,3,0,2,4,1,1,4,0]
              t           Tail: [3,0,2,4,1,1,4,0]
            Ċ2            Take every second element: c = [3,2,1,4]
  §&                      c satisfies both of the following:
    ¦                     1) It contains every element of
     ḣ4                      the range [1,2,3,4].
       Sδ                 2) If you zip c
           ↔                 with its reverse,
         Λ                   then each corresponding pair
          ≈                  has absolute difference at most 1.
                         Implicitly print the result.

3

Dyalog APL30 29 28 27 26字节(SBSC

+/∘,{12 2⍷×∘⌽⍨⊢/4 2⍴⍵}⌺3 3

在线尝试!


很好!⌽∘⊖×⊢可以缩短2个字节,您能猜出怎么做?
ngn

@ngn我真正想要的是一个类似ø的符号;)
H.PWiz

@ngn我有×∘⌽∘⊖⍨
H.PWiz

这是更短的:(⌽×⊖)
ngn

哦,这很聪明,您将自拍(...⍨)向左移动了⊢/另外-1。我没想到。在这种情况下,您不应该相信我。
NGN


2

干净255 ... 162字节

经常在理解中使用模式过滤器是没有好处的,但在这种情况下,确实如此。

import StdEnv,StdLib
? =reverse
@ =transpose
t=tails
$m=sum[1\\f<-[id,@,?,?o@,@o?,@o?o@,?o@o?o@,?o@o?],[a,b,c:_]<-t(f m),[_,1:_]<-t a&[3,_,4:_]<-t b&[_,2:_]<-t c]

在线尝试!

定义函数$,获取[[Int]]并返回Int

它首先生成矩阵的所有对称性m(通过变换f),取其tails具有三行或更多行,并同步检查tails每行的前三列中有多少列与十字图案匹配。

这相当于计算的有序数tails-OF- tails匹配图案[[_,1,_:_],[3,_,4:_],[_,2,_:_]:_]-其又逻辑上相同的,如果,检查矩阵中的每个小区,该小区是交叉的任何旋转的左上角。


2

Python 3中,120个 118字节

lambda x:sum({x[i+1][j]*x[i+1][j+2],x[i][j+1]*x[i+2][j+1]}=={2,12}for i in range(len(x)-2)for j in range(len(x[0])-2))

在线尝试!

使用以下事实:十字架中相反的数字对的乘积必须分别为2和12,并与一组比较以覆盖所有不同的方向。将输入作为2D整数数组。


1
您不必计入f=比分
ngn

2

Japt -x39 38 33字节

ã3 Ëmã3 y x@[XXy]®g1 ë2 ×Ãn e[2C]

在线尝试!

-1个字节感谢@Shaggy。

@ETHproductions可以通过重构数组来获得-5个字节。

开箱及其工作方式

Uã3 mD{Dmã3 y xX{[XXy]mZ{Zg1 ë2 r*1} n e[2C]

       Input: 2D Array of numbers
Uã3    Generate an array of length 3 segments of U
mD{    Map... (D = 2D array of numbers having 3 rows)
Dmã3     Map over each row of D to generate an array of length 3 segments
y        Transpose; make an array of 3x3 subsections
xX{      Map and sum... (x = 3x3 2D array of numbers)
[XXy]      Array of X and X transposed
mZ{        Map...
Zg1 ë2 r*1   Take row 1, take 0th and 2nd elements, reduce with *
}
n          Sort the array
e[2C]      Equals [2,12] element-wise?
           Implicit cast from boolean to number

       Result: 1D array of counts
-x     Sum of the result array

应该还是有更好的方式来测试交...


您可以通过将结尾的相等性检查替换为来保存字节e[2C]
毛茸茸的

你可以摆脱的2ë2
奥利弗
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.