10
(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 0
s):
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
1
in 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
0
toN
contiguously, meaning that (for example)3
wouldn'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.
Can the output actually be a list of the pieces? Or I/O not be done with strings but with matrices and integers (with
-1
or a space representing an empty space, or absence of an element if possible)? – Erik the Outgolfer – 2019-01-11T19:03:19.653Is it acceptable if the input is 1-based (doesn't contain zeros) and the output uses
0
as filler value? So each piece would be output with the rest of the values in the matrix set to0
– Luis Mendo – 2019-01-11T19:15:01.893Independent of my previous question: no other whitespace is permitted: not even trailing spaces to make all lines equal length? – Luis Mendo – 2019-01-11T19:17:00.787
@EriktheOutgolfer Absence of an element would be OK, since that's outputting just the "piece" itself. Outputting an entire matrix for each piece with
-1
or some other value instead of nothing/whitespace wouldn't be OK, though. – AdmBorkBork – 2019-01-11T19:18:12.727@AdmBorkBork Oh, so should the space (
' '
) be used in that case? – Erik the Outgolfer – 2019-01-11T19:18:49.277@LuisMendo If it's 1-indexed, how would you represent the last example? 1-10? That would mess up the output because the piece wouldn't be the right size? Regarding whitespace - yep, that's correct. No trailing whitespace allowed. – AdmBorkBork – 2019-01-11T19:20:01.403
@EriktheOutgolfer Yep, that would be fine. – AdmBorkBork – 2019-01-11T19:20:26.960
Do we need to support "pieces" that aren't all connected, such as in
010
? – Kamil Drakari – 2019-01-15T01:51:36.620@AdmBorkBork No trailing whitespace to make lines equal length is an issue for array languages where the normal way to print multi-line text is to pad all lines with spaces until equal length, because arrays may not be ragged. Still prohibited for them? – Adám – 2019-01-15T07:25:26.560
Is something else instead of a space allowed in the output, as long as all trailing unnecessary characters are removed? I.e.
111\n01
instead of111\n 1
? – Kevin Cruijssen – 2019-01-15T09:02:06.493@KamilDrakari No, the pieces will always be connected orthogonally or a single character. – AdmBorkBork – 2019-01-15T13:20:08.247
@Adám Yep. Guess that's a bummer for those languages. – AdmBorkBork – 2019-01-15T13:20:37.290
@KevinCruijssen Given how others have answered this question already, I'm going to say "no." – AdmBorkBork – 2019-01-15T13:21:08.557
@AdmBorkBork In that case the Japt answer is invalid, since it outputs
true
andfalse
, instead oftrue
and space. – Kevin Cruijssen – 2019-01-15T13:37:10.833