Quantcast
Viewing all articles
Browse latest Browse all 35

Is my kids' alphabet mat properly grouped by colors?

My kids have an alphabet mat to play with, something like this:

Image may be NSFW.
Clik here to view.
Alphabet mat

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:

AABBCDDDEABBCCCDEEABCCCCDDEAACCCDDEEAAAACCCCEAAAAAACCC

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:

AAAAAAAAAAAAAAAA  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:

ABAABA

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:

ABBA

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    truthyABAB   truthyABBA   falseyABCDE    truthyABCDC    falsey**::dd22***:d222*:::::22    truthy$$$%%%&&$$%%&&&&&&$$$%&&    falseyAABBCDDDEABBCCCDEEABCCCCDDEAACCCDDEEAAAACCCCEAAAAAACCC   truthyAABBABBAAAAA    truthyAAABAAAAAAAA    truthy

My mat properly grouped by colors

Image may be NSFW.
Clik here to view.
My mat properly grouped by colors

(I still have to fix those borders...)


Viewing all articles
Browse latest Browse all 35

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>