我的骰子矩阵值多少钱?


21

输入项

由3x3子矩阵并排放置的非空二进制矩阵。

任务

您的任务是在3x3子矩阵中识别有效的骰子模式(如下所述)。每个有效模式都值得相应骰子的值。无效的模式值0。

输出量

有效骰子值的总和。

骰子图案

1个00001个000021个00000001个要么001个0001个0031个0001个0001个要么001个01个01个0041个01个0001个01个51个01个01个01个01个61个01个1个01个1个01个要么1个1个1个0001个1个1个

以下矩阵的期望输出是14,因为它包含的骰子563,接着是无效模式(从左至右和从顶部到底部)。

1个01个1个1个1个01个00001个01个1个1个1个1个0000001个001个0001个01个0

规则

  • 矩阵的宽度和高度都保证为3的倍数。
  • 您必须忽略在网格上未正确对齐的子矩阵(请参阅第3个测试用例)。更正式地说,假设索引为0:要考虑的每个子矩阵的左上单元格的坐标形式为3X3ÿ
  • 这是

测试用例

// 0
[ [ 1,0,0 ],
  [ 0,0,1 ],
  [ 1,0,0 ] ]

// 2
[ [ 0,0,1 ],
  [ 0,0,0 ],
  [ 1,0,0 ] ]

// 0 (0 + 0)
[ [ 0,0,1,0,1,0 ],
  [ 0,0,0,1,0,0 ],
  [ 0,0,1,0,1,0 ] ]

// 9 (3 + 3 + 3)
[ [ 1,0,0,0,0,1,1,0,0 ],
  [ 0,1,0,0,1,0,0,1,0 ],
  [ 0,0,1,1,0,0,0,0,1 ] ]

// 6 (6 + 0)
[ [ 1,0,1 ],
  [ 1,0,1 ],
  [ 1,0,1 ],
  [ 1,0,1 ],
  [ 1,0,0 ],
  [ 1,0,1 ] ]

// 14 (5 + 6 + 3 + 0)
[ [ 1,0,1,1,1,1 ],
  [ 0,1,0,0,0,0 ],
  [ 1,0,1,1,1,1 ],
  [ 1,0,0,0,0,0 ],
  [ 0,1,0,0,1,0 ],
  [ 0,0,1,0,1,0 ] ]

// 16 (1 + 2 + 3 + 4 + 0 + 6)
[ [ 0,0,0,1,0,0,1,0,0 ],
  [ 0,1,0,0,0,0,0,1,0 ],
  [ 0,0,0,0,0,1,0,0,1 ],
  [ 1,0,1,1,1,1,1,0,1 ],
  [ 0,0,0,1,0,1,1,0,1 ],
  [ 1,0,1,1,1,1,1,0,1 ] ]

Answers:


5

Python 3中195个 189字节

-6个字节,感谢@Jo King

lambda m:sum({16:1,257:2,68:2,273:3,84:3,325:4,341:5,455:6,365:6}.get(int(''.join(str(e)for c in m[3*i:][:3]for e in c[3*j:][:3]),2),0)for i in range(len(m)//3)for j in range(len(m[0])//3))

在线尝试!(189) 在线尝试!(195)

可读版本:

# 3x3 part matrix to dice, beginning at coordinates 3*i, 3*j
def single_matrix_to_dice(matrix, i, j):
    # Example: matrix = [[0, 0, 0], [0, 1, 0], [0, 0, 0]], i=0, j=0 (result is 1)

    matrix_string = ''.join(
        str(e) for column in matrix[3*i:3*i+3] 
        for entry in column[3*j:3*j+3]
    ) # Slicing the matrix so that only the valid entries remain, here '000010000'

    # Interpreting the matrix string as binary number, here 16
    binary_number = int(matrix_string,2)

    # binary representations of all valid dice rolls
    dct = {16:1,257:2,68:2,273:3,84:3,325:4,341:5,455:6,365:6}

    return dct.get(binary_number, 0)

def f(matrix):
    return sum(
        single_matrix_to_dice(matrix, i, j) for i in range(len(m)//3) 
        for j in range(len(m[0])//3))
    ) # len(m)/3 would generate a float, so len(m)//3 is used

我想知道是否可以通过对矩阵转置进行相同的操作来稍微缩短此时间。这样,您可以删除映射中所有添加6个字节的重复项。只需添加<18字节的转置步骤
Easton Bornemeier 18'Aug


摆脱//3'0'+''.join...
Jonathan Allan

...将其与枚举一起保存另外两个:在这里
Jonathan Allan '18


5

R,134字节

function(m,d=dim(m)/3-1){for(a in 0:d)for(b in 0:d[2])F=F+sum(y<-m[1:3+a*3,1:3+b*3])*sum(y*2^(8:0))%in%utf8ToInt("āDđTŅŕLJŭ");F}

在线尝试!

我注意到我对@Heteira有相同的想法

历史:

  • 171:感谢@JayCe -10个字节!
  • 161:-3个字节,感谢@Giuseppe!
  • 158:节省了-13个字节!
  • 145:-2个字节,感谢@Giuseppe!
  • 143:保存-6个字节!
  • 137:-3个字节,感谢@JayCe!

1
通过压缩数字列表节省5个字节 -链接示例太长而无法发表评论
JayCe '18

1
3个字节的使用dim
JayCe


1
还有一对(2^(8:0))可以删除的括号。
朱塞佩

1
我添加了忘记cat的输出intToUtf8保存3个字节
JayCe

4

Perl 6的113 105 97 94个字节

{sum (|@_[*;^3+3*$_]for ^@_[0]).rotor(9).map:{"@āđŅŕLJ@@DT@@ŭ".ords.first(:2[$_],:k)%7}}

在线尝试!

将矩阵拆分为3x3的子矩阵,将9个1和0转换为以2为底,然后将其索引为该值的整数列表。

说明:

{  #Start anonymous code block
  sum   # Sum of all
     (|@_[*;^3+3*$_]   # Get the n*3 to n*3+3th elements in every sub-list
           for ^@_[0]) # For n in the range 0 to width (divide by 3 to avoid warnings)
     .rotor(9)  # Split this list into groups of 9 (split the dice up)
     .map:{     # And map each die to 
        "@āđŅŕLJ@@DT@@ŭ".ords  # In the list of integers
           .first(      # The first appearance of 
               :2[$_],  # The dice converted from a list of 0s and 1s to base 2
                 :k     # Returning the index
             )%7        # And modulo by 7 to get the alternate versions of 2, 3 and 6
          }
}

4

果冻 29 28 字节

-1感谢Xcoder先生(用于替换ṢṪ

s€3ZẎs3µZU,ƊṀṙ1FḄ“°€⁼-Ḍ?‘i)S

单子链接。

在线尝试!运行测试

怎么样?

s€3ZẎs3µZU,ƊṀṙ1FḄ“°€⁼-Ḍ?‘i)S - Link: list of lists of 1s and 0s
s€3                          - split each into threes
   Z                         - transpose
    Ẏ                        - tighten
     s3                      - split into threes -> the sub-matrices in column-major order
       µ                  )  - for each sub-matrix, say D:
           Ɗ                 -   last three links as a monad:
        Z                    -     transpose D
         U                   -     reverse each -> D rotated a quarter turn clockwise
          ,                  -     pair with D
            Ṁ                -   get the maximum of the two orientations
             ṙ1              -   rotate left by one (to ensure FḄ will yield integers <256 for all non-zero valued D)
               F             -   flatten
                Ḅ            -   convert from binary
                         i   -   first 1-based index in (0 if not found):
                 “°€⁼-Ḍ?‘    -     code-page indices list = [128,12,140,45,173,63]
                           S - sum

例如,当子矩阵为:

[[1,0,1],
 [1,0,1],
 [1,0,1]]

然后ZU,Ɗ产生:

[[[1, 1, 1],
  [0, 0, 0],
  [1, 1, 1]],   ...which has maximum (Ṁ):    ...and after ṙ1:
 [[1, 0, 1],                   [[1, 1, 1],         [[0, 0, 0],
  [1, 0, 1],                    [0, 0, 0],          [1, 1, 1],
  [1, 0, 1]]]                   [1, 1, 1]]          [1, 1, 1]]

...这将会拼[0, 0, 0, 1, 1, 1, 1, 1, 1],其中,从二进制转换,就是63这是在代码页索引列表第六项“°€⁼-Ḍ?‘?能够字节3F果冻的代码页


可能会代替ṢṪ-1。
Xcoder先生18年

...是的(我以为我使用M>。< 保存了)。ŒṪ我想知道可以做些聪明的事吗?
Jonathan Allan


2

视网膜0.8.2,90字节

+`(...)(.+¶)(...)(.+¶)(...)
$1¶$3¶$5¶$2$4
¶

M!`.{9}
G`111000111|(101){3}|(.)0(.0).0\3\2
1

在线尝试!说明:

+`(...)(.+¶)(...)(.+¶)(...)
$1¶$3¶$5¶$2$4

反复删除 3×3 每个块 3×ñ 直到所有行都有3列。

¶

M!`.{9}

将所有块连接在一起,然后分成9列的行。

G`111000111|(101){3}|(.)0(.0).0\3\2

仅保留有效的骰子模式(的两个模式6,然后一个匹配从0到的任何数字5,尽管0当然不会有助于以下计数。)

1

计算有效骰子上的点数。


1

红宝石,151字节

->m{m.each_slice(3).flat_map{|r|r.transpose.each_slice(3).map{|d|" \x10āđŅŕLJ  DT  ŭ".chars.map(&:ord).index(d.flatten.join.to_i 2)&.%7}-[p]}.sum}

在线尝试!

一个lambda接受一个2d整数数组(我猜是字符串)。从Jo King的答案中汲取灵感。我觉得像从输入矩阵中切出骰子要占用很多空间,所以我很可能会失望。幸运的是,处理nils只花了我几个字节。

取消高尔夫:

->m{
  m.each_slice(3).flat_map{|r|             # Split into groups of 3 rows
    r.transpose.each_slice(3).map{|d|      # Split into groups of 3 columns
      " \x10āđŅŕLJ  DT  ŭ".chars.map(&:ord) # [0,16,257,273,325,341,455,0,0,68,84,0,0,365]
        .index(                            # Find in that array
          d.flatten.join.to_i 2            #   the die flattened into a bitstring (nil if not found)
        )&.%7                              # Safe-modulo 7 (leaves nils as nil)
    }-[p]                                  # Remove nils
  }.sum                                    # Add 'em up
}

1

Clojure,197个字节

#(apply +(for[R[range]i(R 0(count %)3)j(R 0(count(% 0))3)](case(apply +(map *(iterate(partial * 2)1)(for[x(R 3)y(R 3)]((%(+ i x))(+ j y)))))16 1 257 2 68 2 273 3 84 3 325 4 3 4 1 5 455 6 365 6 0)))

我应该想出一些更聪明的东西。


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.