# How is BABBAB formed

$[A ...] \to A [...]$

$[B ...] \to B [S [...]]$

$[S A ...] \to B ...$

$[S B ...] \to A [S ...]$

$[S] \to B$

$[ ] \to$

(E.g. $[AAB] \to AABB$)

But, $[????] \to BABBAB$

Bonus puzzle:

Characterize all sequences of A's and B's so that $[?] => sequence$, for some ? sequence of A's and B's

• Hello! Welcome to puzzling SE! Please take the tour (you'll also get a badge, too) to get a better understanding of community expectations. – Prince North Læraðr Mar 8 '18 at 0:25
• Wow is everyone overlooking that clever title ?! Made me chuckle about those decade old memes. Obligatory video and related required viewing. – Amit Naidu Mar 8 '18 at 5:43

The only four-letter sequence [????] that works is:

[BBBB]

This evaluates as

[BBBB] →
B[S[BBB]] →
B[SB[S[BB]]] →
B[SB[SB[S[B]]]] →
B[SB[SB[SB[S[]]]]] →
B[SB[SB[SB[S]]]] →
B[SB[SB[SBB]]] →
B[SB[SBA[SB]]] →
B[SB[SBAA[S]]] →
B[SB[SBAAB]] →
B[SBA[SAAB]] →
B[SBABAB] →
BA[SABAB] →

BABBAB

I found this through directed guessing:

I evaluated the 3 letter sequences [AAB], [BBA] and [BBB], and these gave me a good feel for what might work and what definitely wouldn't. Then I jumped to four letter sequences and took a couple wrong guesses before finding this solution.

Bonus:
I've now written some code to find generable outputs (and was able to confirm that the above solution is unique for four letter inputs). I believe that the rule is:

Treating "A" as 0 and "B" as 1, and evaluating the outputs that can be produced by this ruleset as binary, the possible outputs are all the multiples of 3. (Or, at minimum, they are all multiples of 3, even if not all such multiples can be generated.)

Here's what I've been able to find are the generable outputs:

length of 1: A B
length of 2: AA AB BA BB
length of 3: AAA AAB ABA ABB BAA BAB BBA BBB
length of 4: AAAA AAAB AABA AABB ABAA ABAB ABBA ABBB
BAAA BAAB BABA BABB BBAA BBAB BBBA BBBB
where BOLD are generable outputs and ITALIC are outputs that cannot be generated.

The only generable outputs of length 5 are:
AAAAA AAABB AABBA ABAAB ABBAA ABBBB
BAABA BABAB BBAAA BBABB BBBBA

and for length 6 are:
AAAAAA AAAABB AAABBA AABAAB AABBAA AABBBB
ABAABA ABABAB ABBAAA ABBABB ABBBBA BAAAAB
BAABAA BAABBB BABABA BABBAB BBAAAA BBAABB
BBABBA BBBAAB BBBBAA BBBBBB

I finally noticed that ...

the generable outputs occur at intervals of 3.

• the bonus question is asking for a general rule of [?] where ? can represent any string of As and Bs – Praneetmek Mar 8 '18 at 1:04
• The bonus question asks which sequences its possible to end up with given a start of A's and B's (there is a very simple rule to tell if a sequence is a possible output) – while1fork Mar 8 '18 at 1:11

Not sure if its a typo or not, but currently from the list of rules

[SAABBAB]= BABBAB

• Was thinking about the same – user45835 Mar 8 '18 at 0:35
• I assumed "[????]" was seeking a four letter sequence. If not then yes, this is correct. – Rubio Mar 8 '18 at 0:37
• maybe OP meant [SA....]=B[...] and just typoed? – Praneetmek Mar 8 '18 at 0:38
• @Rubio Either that or, from the way the bonus is phrased, ???? can only contain A's and B's, without any S's. – DqwertyC Mar 8 '18 at 0:40
• Directed guessing. I tried 3 letter sequences [AAB], [BBA] and [BBB] and they gave me a good feel for what might work and what definitely wouldn't. Then jumped to four letter sequences and took a couple wrong guesses before finding that one. – Rubio Mar 8 '18 at 1:11