태그 보관물: binary-matrix

binary-matrix

주사위 매트릭스의 가치는 얼마입니까? 0 ,

입력

3×3 하위 행렬로 구성된 비어 있지 않은 이진 행렬은 나란히 놓입니다.

태스크

귀하의 작업은 3×3 하위 매트릭스 중에서 유효한 주사위 패턴 (아래 설명 참조)을 식별하는 것입니다. 각 유효한 패턴은 해당 주사위의 가치가 있습니다. 유효하지 않은 패턴은 0입니다.

산출

유효한 주사위 값의 합입니다.

주사위 패턴

1:(0,0,00,1,00,0,0)2:(1,0,00,0,00,0,1)또는(0,0,10,0,01,0,0):(1,0,00,1,00,0,1)또는(0,0,10,1,01,0,0)4:(1,0,10,0,01,0,1)5:(1,0,10,1,01,0,1)6:(1,0,11,0,11,0,1)또는(1,1,10,0,01,1,1)

다음 행렬의 예상 출력 은 주사위 5 , 63 을 포함하고 유효하지 않은 패턴 (왼쪽에서 오른쪽으로, 위에서 아래로)을 포함하므로 14 입니다.

(1,0,1,1,1,10,1,0,0,0,01,0,1,1,1,11,0,0,0,0,00,1,0,0,1,00,0,1,0,1,0)

규칙

  • 행렬의 너비와 높이는 모두 3의 배수로 보장됩니다.
  • 그리드에 올바르게 정렬되지 않은 하위 행렬을 무시해야합니다 (3 차 테스트 사례 참조). 보다 공식적이고 0- 인덱싱 가정 : 고려할 각 하위 행렬의 왼쪽 위 셀의 좌표는 입니다.
    (엑스,와이)

  • 이것은 입니다.

테스트 사례

// 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 ] ]



답변

파이썬 3 , 195 189 바이트

@Jo King 덕분에 -6 바이트

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


답변

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 : @Giuseppe 덕분에 -3 바이트!
  • 158 : -13 바이트 저장!
  • 145 : @Giuseppe 덕분에 -2 바이트!
  • 143 : -6 바이트 절약!
  • 137 : @JayCe 덕분에 -3 바이트!

답변

Perl 6 , 113105 97 94 바이트

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

온라인으로 사용해보십시오!

행렬을 3×3의 하위 행렬로 분할하고 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
          }
}


답변

젤리 ,  29 28 바이트

Xcoder 씨께 -1 감사합니다 ( 교체 용 ṢṪ).

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젤리의 코드 페이지 )


답변

apt -x , 36 바이트

이제 재미있어지고 있습니다. 더 골프를 칠 수 있다고 확신합니다

ò3 ®®ò3Ãy f@"0ıtŵġdťƍǧ"øºXm¬¬Í+H)d
c

온라인으로 사용해보십시오!


답변

레티 나 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

×

×

¶

M!`.{9}

모든 블록을 결합한 다음 다시 9 열로 나눕니다.

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

(두 개의 패턴 만 유효 주사위 패턴을 유지 6에서 다음, 하나와 일치하는 숫자를 05있지만, 0물론 아래의 수에 포함되지 않습니다.)

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}

온라인으로 사용해보십시오!

int (또는 문자열)의 2 차원 배열을 받아들이는 람다. Jo King의 답변 에서 영감을 얻습니다 . 입력 매트릭스에서 주사위를 자르는 것은 많은 공간을 차지했기 때문에 아웃 골프가 될 수 있습니다. 다행히도 nil을 처리하는 데 몇 바이트의 비용이 들었습니다.

언 골프 드 :

->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
}