14
1
My kids have an alphabet mat to play with, something like this:
After months with the tiles of the mat randomly placed, I got tired and placed all the tiles of the mat grouped by sections according to their background colors. So, if the letters represent the background color, I got a mat like this:
AABBCDDDE
ABBCCCDEE
ABCCCCDDE
AACCCDDEE
AAAACCCCE
AAAAAACCC
So, for colors A, B, C, D and E there is always a way to connect all the tiles with the same background color either horizontally or vertically in the mat. That's what I call a mat properly grouped by colors. You can see the groups for the previous example in the following tables:
AA
A
A
AA
AAAA
AAAAAA
BB
BB
B
C
CCC
CCCC
CCC
CCCC
CCC
DDD
D
DD
DD
E
EE
E
EE
E
Also, there is only one group for every color, so this would not be valid:
ABA
ABA
Because color A tiles are not grouped in only one group. This also would not be valid because the tiles do not connect either horizontally or vertically:
AB
BA
The challenge
Given a 2-dimensional array of characters in the printable ASCII range (does not need to be a square one as long as the size of both dimensions are equal to or greater than 1), check if the array represents a mat properly grouped by colors (each different character in the array represents a different color). Input may be in any reasonable format as long as it represents a 2-dimensional array of characters (2D char array, array of strings of the same length, and so on), and output must be a pair of truthy and falsey values (0/1, 't'/'f', true/false, whatever as long as something is returned and the return values are consistent across inputs).
This is code-golf, so may the shortest program/function/method/lambda for each language win!
Examples
A truthy
AB
AB truthy
AB
BA falsey
ABCDE truthy
ABCDC falsey
**::dd22
***:d222
*:::::22 truthy
$$$%%%&&
$$%%&&&&
&&$$$%&& falsey
AABBCDDDE
ABBCCCDEE
ABCCCCDDE
AACCCDDEE
AAAACCCCE
AAAAAACCC truthy
AABB
ABBA
AAAA truthy
AAAB
AAAA
AAAA truthy
My mat properly grouped by colors
(I still have to fix those borders...)
Related: 1D version
– xnor – 2017-11-23T16:59:57.1001Out of curiosity, why wouldn't you arrange the mat in alphanumerical order? Nothing to do with the challenge of course, just wondering – caird coinheringaahing – 2017-11-23T17:04:54.217
4@cairdcoinheringaahing because then my particular OCD would not be satisfied. :-) – Charlie – 2017-11-23T17:11:06.917
3Your kids continue to be a source of inspiration for code golf challenges :-) – Luis Mendo – 2017-11-23T17:52:51.500
@LuisMendo yes, the pair of values must be consistent across inputs, and you can choose any two values as long as they are consistent (you may choose 0 for true and 1 for false if you want). – Charlie – 2017-11-23T18:05:09.533
2Why do the colours have to be represented by characters rather than some other input (like integers or even pixels maybe)? – Jonathan Allan – 2017-11-23T22:22:51.637
So we need to be able to support 95 different colours? – Neil – 2017-11-24T01:20:32.500
2speaking of ocd, this challenge won’t be complete without a picture of the mat properly grouped – Jonah – 2017-11-24T02:16:11.873
@JonathanAllan sorry, the tiles have a background color and an alphanumeric character in another color, so I immediately thought about representing color with chars, but you are right, it could be done with numbers as inputs. – Charlie – 2017-11-24T05:52:21.040
Suggested test case:
AABB, ABBA, AAAA / truthy
. The rightmostA
of the 2nd row is connected only with the last row. This pattern doesn't appear in the existing test cases. – Arnauld – 2017-11-24T07:07:18.4131@Jonah I included an image of my mat. :-) – Charlie – 2017-11-24T07:27:09.680
1@Charlie it is bothering me that you didn't take the color of the letters into account... For example bring the yellow tile with the purple 6/9 adjacent to the purple tiles, swap the purple tile with the green "W" with the tile on top of that... So much wasted potential! :P – Luca H – 2017-11-24T09:26:57.603
@LucaH I hadn't noticed that! Now I feel the urge to leave my office and ran home to swap those tiles!! – Charlie – 2017-11-24T09:29:26.573
@Charlie do it after work, I don't want you to lose your job :D – Luca H – 2017-11-24T09:34:38.303
I could swear we've had this before. Or am I (mis)remembering another, completely different challenge involving one of those mats? – Shaggy – 2017-11-25T23:14:03.403
@Shaggy you are probably remembering this other challenge but it has nothing to do with this.
– Charlie – 2017-11-26T08:14:22.857I count 6 background colors in the pix...? – DonielF – 2018-01-02T15:47:08.643