Build a MU puzzle solver

16

1

The MU puzzle is a puzzle in which you find out whether you can turn MI into MU given the following operations:

  1. If your string ends in I, you may add a U to the end. (e.g. MI -> MIU)

  2. If your string begins with M, you may append a copy of the part after M to the string.
    (e.g. MII -> MIIII)

  3. If your string contains three consecutive I's, you may change them into a U.
    (e.g. MIII -> MU)

  4. If your string contains two consecutive U's, you may delete them. (e.g. MUUU -> MU).

Your task is to build a program that determines whether this is doable for any start and finish strings.

Your program will take two strings as input. Each string will consist of the following:

  • one M.

  • a string of up to twenty-nine I's and U's.

Your program will then return true (or your programming language's representation thereof/YPLRT) if the second string is reachable from the first string, and false (or YPLRT) if it is not.

Example inputs and outputs:

MI  MII
true

MI  MU
false

MIIIIU  MI
true

The shortest code in any language to do this wins.

Joe Z.

Posted 2014-07-12T03:03:42.917

Reputation: 30 589

Since both start and end strings can be up to 30 characters, is there a limit on the length of a required intermediate string? For example, could a starting string of length 30 require being doubled a couple times right away to reach the ending string (thus reaching length 120)? – mbomb007 – 2015-04-01T21:50:02.280

No, there shouldn't be a limit on that. – Joe Z. – 2015-04-01T22:49:59.463

8I'm currently reading Gödel, Escher, Bach and thought about doing an "18-hole golf course"based on its chapters afterwards. Guess I've got to find a new "hole 1" now. ;) – Martin Ender – 2014-07-12T06:38:56.527

This is just a graph reachability question whose essence has been asked plenty of times before. – Peter Taylor – 2014-07-12T08:57:09.180

1@PeterTaylor I think there's a good chance this won't be solved by an explicit search of the reachability graph. The MIU rules have a lot of structure, and I wouldn't be surprised if there's a direct algorithm to test for reachability without searching for intermediate nodes. For example, the nodes reachable from MI are exactly the M(I|U)* where the number of I isn't a multiple of 3. And such a direct check surely makes for shorter code. Also, I don't know of an a-priori bound on the lengths of strings required for intermediate steps, so direct search might be simply impractical. – xnor – 2014-07-12T16:02:46.063

@xnor, on the other hand it's an instance of a Turing-complete system, so explicit search of the reachability graph may be the only approach that works in the general case. – Peter Taylor – 2014-07-12T16:50:50.163

@PeterTaylor Oh, it's Turing complete? I don't remember that from GEB. Can you please point me to a proof? – xnor – 2014-07-12T16:54:51.127

@xnor, I said "an instance of a Turing-complete system". Specifically, it's a Post canonical system.

– Peter Taylor – 2014-07-12T17:02:50.063

@PeterTaylor Oh, I see. I guess then the question remains whether there's a problem-specific method. I haven't found one but I hope someone will. – xnor – 2014-07-12T17:06:54.863

@PeterTaylor I understand what you mean about the reachability graph, but as xnor said, I'm looking to see whether there are any solutions that are shorter that do something else. – Joe Z. – 2014-07-12T17:11:17.073

1I've thought about this problem for a while and haven't gotten close to a solution that isn't brute-force. If nobody bites, I suggest posting an easier version of the question, perhaps to give a derivation starting from MI of a given reachable string. – xnor – 2014-07-15T13:09:07.793

"The puzzle's solution is no. It is impossible to change the string MI into MU by repeatedly applying the given rules." So the program returns false whatever the input? – Pharap – 2014-07-25T15:46:12.367

No, the program's input isn't just for MI to MU. I could be from MIIIIUIUIUIUUIUIUUIUU to MUIUUUIUIUUUIIIIIUIUI, for instance. – Joe Z. – 2014-07-25T16:19:56.473

1What should the output be if IM is supplied or MUMMI? – Beta Decay – 2014-09-10T17:41:40.803

You can assume that input will be valid. – Joe Z. – 2014-09-10T21:41:09.320

Answers

7

SWI Prolog, 183 characters

m(A,A).
m([i],[i,u]).
m([i,i,i|T],B):-m([u|T],B).
m([u,u|T],B):-m(T,B).
n([m|A],[m|B]):-(m(A,B);append(A,A,X),m(X,B)).
n(A,B):-m(A,B).
s(A,B):-atom_chars(A,X),atom_chars(B,Y),n(X,Y).

How about some Prolog, (since nobody has answered in 6 months). To run, just use "s(mi,mu)." The code breaks up atoms into chars, then searches for the solution.

swstephe

Posted 2014-07-12T03:03:42.917

Reputation: 649

2This wrongfully returns false on s(mi,miiii), and in general anything requiring more than one application of rule 2 to prove. – algorithmshark – 2017-12-08T00:11:29.273