Zsgn©>‹®L¹˜Kœ0ªε\¹˜0y.;¹gô©O®øO®Å\O®Å/O)˜Ë}à*
00n200 giây).
Có thể có ít hơn 4 byte, nhưng hiện tại có một lỗi trong phần dựng sẵn .;
với danh sách 2D. :
và .:
làm việc như mong đợi, nhưng .;
không làm bất cứ điều gì trong danh sách 2D ngay bây giờ .. do đó, công việc xung quanh ˜
và¹gô
làm phẳng ma trận; sử dụng .;
trong danh sách; và biến nó trở lại thành ma trận một lần nữa.
Dùng thử trực tuyến hoặc xác minh một số trường hợp thử nghiệm . (LƯU Ý: Không bao gồm trường hợp thử nghiệm cuối cùng của mô tả thử thách, vì nó có quá nhiều 0 ..)
Giải trình:
Z # Get the maximum of the (implicit) input-matrix (implicitly flattened)
# (and without popping the matrix)
# i.e. [[8,0,6],[0,5,0],[0,0,2]] → 8
s # Swap to get the input-matrix again
g # Get its length (amount of rows)
# i.e. [[8,0,6],[0,5,0],[0,0,2]] → 3
n # Square it
# i.e. 3 → 9
© # Store it in the register (without popping)
>‹ # Check if the maximum is <= this squared matrix-dimension
# i.e. 8 <= 9 → 1 (truthy)
® # Push the squared matrix-dimension again
L # Create a list in the range [1, squared_matrix_dimension]
# i.e. 9 → [1,2,3,4,5,6,7,8,9]
¹ # Push the input-matrix
˜ # Flatten it
# i.e. [[8,0,6],[0,5,0],[0,0,2]] → [8,0,6,0,5,0,0,0,2]
K # Remove all these numbers from the ranged list
# i.e. [1,2,3,4,5,6,7,8,9] and [8,0,6,0,5,0,0,0,2] → [1,3,4,7,9]
œ # Get all possible permutations of the remaining numbers
# (this part is the main bottleneck of the program;
# the more 0s and too high numbers, the more permutations)
# i.e. [1,3,4,7,9] → [[1,3,4,7,9],[1,3,4,9,7],...,[9,7,4,1,3],[9,7,4,3,1]]
0ª # Add an item 0 to the list (workaround for inputs without any 0s)
# i.e. [[1,3,4,7,9],[1,3,4,9,7],...,[9,7,4,1,3],[9,7,4,3,1]]
# → [[1,3,4,7,9],[1,3,4,9,7],...,[9,7,4,1,3],[9,7,4,3,1],"0"]
ε # Map each permutation `y` to:
\ # Remove the implicit `y` which we don't need yet
¹˜ # Push the flattened input again
0 # Push a 0
y # Push permutation `y`
.; # Replace all 0s with the numbers in the permutation one by one
# i.e. [8,0,6,0,5,0,0,0,2] and [1,3,4,7,9]
# → [8,1,6,3,5,4,7,9,2]
¹g # Push the input-dimension again
ô # And split the flattened list into parts of that size,
# basically transforming it back into a matrix
# i.e. [8,1,6,3,5,4,7,9,2] and 3 → [[8,1,6],[3,5,4],[7,9,2]]
© # Save the matrix with all 0s filled in in the register (without popping)
O # Take the sum of each row
# i.e. [[8,1,6],[3,5,4],[7,9,2]] → [15,12,18]
®øO # Take the sum of each column
# i.e. [[8,1,6],[3,5,4],[7,9,2]] → [18,15,12]
®Å\O # Take the sum of the top-left to bottom-right main diagonal
# i.e. [[8,1,6],[3,5,4],[7,9,2]] → 15
®Å/O # Take the sum of the top-right to bottom-left main diagonal
# i.e. [[8,1,6],[3,5,4],[7,9,2]] → 18
) # Wrap everything on the stack into a list
# → [[15,12,18],[18,15,12],15,18]
˜ # Flatten it
# i.e. [[15,12,18],[18,15,12],15,18] → [15,12,18,18,15,12,15,18]
Ë # Check if all values are equal
# i.e. [15,12,18,18,15,12,15,18] → 0 (falsey)
} # After the map:
# → [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
à # Check if any are truthy by taking the maximum
# → 1 (truthy)
* # And multiply the two checks to verify both are truthy
# i.e. 1 and 1 → 1 (truthy)
# (and output the result implicitly)
Phần ©O®øO®Å\O®Å/O)˜Ë
này cũng được sử dụng trong câu trả lời 05AB1E của tôi cho thử thách Xác minh Magic Magic Square , vì vậy hãy xem câu trả lời đó để được giải thích sâu hơn về phần đó của mã.