í‚εεygÅ0«NFÁ]€ø`IIø)εεγJ0K}˜D€gøNUε"/\-|"XèªćÙš]€`D€ÅsZQÏεDÅsi¤'.:]{γεÐнÅsig4÷∍]€`
Input as a matrix of characters. Outputs as a list of triples.
Try it online or verify all test cases.
Explanation:
Generate all diagonals and anti-diagonals of the input-matrix:
í # Reverse each row of the (implicit) input-matrix
‚ # Pair it with the (implicit) input-matrix
ε # Map both matrices to:
ε # Map each row to:
yg # Get the length of the current row
Å0« # Append that many 0s at the end of the row
NFÁ # Rotate the row the 0-based index amount of times towards the right
] # Close the rotate-loop and nested maps
€ø # Transpose/zip (swapping rows/columns) for both matrices
` # And push both matrices to the stack
Put them in a list with the input-matrix itself, as well as all columns of the input-matrix:
I # Push the input-matrix
Iø # Push the input-matrix, and transpose/zip (swapping rows/columns)
) # Wrap everything on the stack into a list
Now that we have a list of all diagonals, all anti-diagonals, all rows, and all columns, we're going to extract chunks of the same characters, and transform them into the output triplets:
ε # Map each matrix to:
ε # Map each row to:
γ # Split into chunks of the same elements
J # Join them together to a string
0K # Remove all 0s
}˜ # After the map of the rows: deep-flatten all chunks
€g # Get the length of each chunk
D ø # And pair it with the chunk itself
NU # Store the current index in variable `X`
ε # Map over the chunk and length pairs:
"/\-|"Xè # Get the `X`'th character from the string "/\|-"
ª # And append it to the pair
ć # Extract head; push the length plus character pair and chunk
# separately to the stack
Ù # Uniquify the chunk so a single character remains
š # And prepend it back in front of the length & character pair
] # Close both maps
€` # And then flatten once
Now that we have all our triplets, we only leave the largest ones:
ہs # Get the middle element (the chunk-length) of each
Z # Get the maximum chunk-length (without popping the list itself)
Q # Check for each chunk-length if it's equal to this maximum
D Ï # And only leave the triplets at the truthy indices
And finally we have to fix all .
edge cases, for chunk-lengths of size 1:
ε # Map each triplet:
DÅsi # If the middle element (the chunk-length) is exactly 1:
¤'.: '# Replace the trailing character of the triplet with a "."
] # Close the if-statement and map
{ # Sort the triplets
γ # And group them by the exact same triplets
ε # Map each chunk of the same triplets to:
Ð # Triplicate the chunk of triplets
нÅsi # If the middle element of each triplet is 1:
g # Get the amount of the same triplets in this chunk
4÷ # Integer-divide it by 4
∍ # And shorten the chunk of triplets to that size
] # Close the if-statement and map
€` # Flatten once
# (after which the result is output implicitly)
.
. What's the expected result of a 2x2 square of 31
s and a0
? \$\endgroup\$d
or can we use any 4 characters of our choosing? \$\endgroup\$ab\ncd\nea → [[a,1,.],[b,1,.],[c,1,.],[d,1,.],[e,1,.],[a,1,.]]
(contains two[a,1,.]
, which is a pretty annoying edge case). \$\endgroup\$