This challenge is NinjaBearMonkey's prize for winning my Block Building Bot Flocks! challenge with the Black Knight submission. Congratulations NinjaBearMonkey!
The challenge here is fairly simple, but has a variety of possible approaches. The story goes that in the world of Isometric Illusions, there are 6 different types of creatures:
- Ninjas, abbreviated
N
- Bears, abbreviated
B
- Monkeys, abbreviated
M
- NinjaBears, abbreviated
NB
- BearMonkeys, abbreviated
BM
- NinjaBearMonkeys, abbreviated
NBM
(NinjaBearMonkey is, of course, the last, most powerful type.)
Your task is to take a census of these creatures when they are lined up side-by-side, i.e. when their abbreviation strings are concatenated. The caveat is that you need to make sure not to over-count the parts of some creatures as separate creatures that happen to look similar. The creatures will line up such that:
- Any instance of
NBM
is 1 NinjaBearMonkey and 0 other creatures. - Any instance of
NB
not followed byM
is 1 NinjaBear and 0 other creatures. - Any instance of
BM
not preceded byN
is 1 BearMonkey and 0 other creatures. - Otherwise, instances of
N
,B
, andM
are single Ninjas, Bears, and Monkeys respectively.
The line is read from left to right.
So, for example, in the line of creatures NBMMBNBNBM
, there are 0 Ninjas, 1 Bear, 1 Monkey, 1 NinjaBear, 0 BearMonkeys, and 2 NinjaBearMonkeys.
Challenge
Write a program or function that takes in a string of the characters N
, B
, and M
, and prints or returns how many of each of the 6 types of creatures are present in it.
The output should have the form
#N #B #M #NB #BM #NBM
with the respective creature count replacing each #
sign. All 6 counts must be shown, separated by spaces, even when they are 0. However, they may be in any order (e.g. #NBM
could come first).
Also:
- The input string will only contain the characters
N
,B
, andM
. - If the empty string is input, then all the counts are 0.
- The output may optionally contain a single leading and/or trailing space, and/or a single trailing newline.
The shortest submission in bytes wins.
Examples
Input: NB
Output: 0N 0B 0M 1NB 0BM 0NBM
Input: NBM
Output: 0N 0B 0M 0NB 0BM 1NBM
Input: NBMMBNBNBM
(example from above)
Output: 0N 1B 1M 1NB 0BM 2NBM
Input: MBNNBBMNBM
Output: 1N 1B 1M 1NB 1BM 1NBM
Input: NNNMNBMMBMMBMMMNBMNNMNNNBNNNBNBBNBNMMNBBNBMMBBMBMBBBNNMBMBMMNNNNNMMBMMBM
Output: 17N 6B 14M 5NB 8BM 3NBM
NBMNBM
would be perfectly valid input. Reading it from left to right there are clearly 2 NinjaBearMonkeys. \$\endgroup\$