(Randomly inspired by https://codegolf.meta.stackexchange.com/a/17272/42963)
Given a rectangular matrix of digits (i.e., 0 - 9), output the "pieces" of the matrix as if the digits are connected together forming a single piece, in ascending order by the digits. The pieces are guaranteed to connect only orthongonally -- no piece will connect diagonally. There will only ever be a maximum of 10 pieces (i.e., a 3 piece won't appear twice in the same matrix).
For example, given the matrix
0 1 1 1 0 0 1 2 3 3 2 2 the following are the pieces, and an example output:
0 0 0 1 1 1 1 2 2 2 3 3 Spacing is important to keep the shape of the pieces, but the pieces do not necessarily need interior spacing. The pieces themselves should somehow be made distinct in a consistent manner (e.g., a newline between pieces, making sure each is a different character, etc.). Additionally, extraneous whitespace (for example, trailing newlines or leading columns) are not allowed. For example, the following would also be valid:
0 00 111 1 2 22 33 or
# ## ### # # ## ## But the following would not be (note the trailing spaces behind the 0s):
0 0 0 Rotations or reflections are also not allowed. For example, outputting
1 111 for the above matrix is also invalid.
The matrix pieces may have holes, or be only a single element:
0 0 0 1 0 2 0 1 0 0 0 3 Or, the piece may be the whole matrix:
0 0 0 0 0 0 Here's a larger, more complicated test case:
1 1 1 1 1 2 2 3 4 4 4 2 2 2 5 5 4 4 2 0 0 5 6 6 6 6 7 7 5 6 8 8 6 6 7 9 6 6 6 7 7 7 And an example output:
00 11111 22 222 2 3 444 44 55 5 5 6666 6 66 666 77 7 777 88 9 Rules and I/O
- Input and output can be given by any convenient method.
- You can print it to STDOUT or return it as a function result.
- Either a full program or a function are acceptable.
- Leading whitespace to keep the shape (e.g., the "T" shape of the
1in the example) is required, consistent whitespace to make the pieces distinct, and a single trailing newline at the end is allowed, but no other whitespace is permitted. - You can safely assume that the pieces are numbered
0toNcontiguously, meaning that (for example)3wouldn't be skipped in a six-piece matrix. - Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
-1or a space representing an empty space, or absence of an element if possible)? \$\endgroup\$0as filler value? So each piece would be output with the rest of the values in the matrix set to0\$\endgroup\$-1or some other value instead of nothing/whitespace wouldn't be OK, though. \$\endgroup\$' ') be used in that case? \$\endgroup\$