Beispiel #1
0
 static void flush(Turing turing, StringBuilder sb, int start, int length)
 {
     for (int i = start; i < start + length;)
     {
         int depth = Math.Min(start + length - i, Depth);
         if (PhraseString.Length > 200)
         {
             depth = Math.Min(depth, 4);
         }
         //
         State     s = turing.GetState();
         Traversal t = best(s, i, depth);
         //
         string[] ps        = t.Steps.Split(',');
         int[]    positions = new int[ps.Length];
         for (int x = 0; x < depth; x++)
         {
             positions[x] = int.Parse(ps[x]);
         }
         //
         string program = print(s, i, positions);
         sb.Append(program);
         turing.Execute(program);
         i += depth;
     }
 }
Beispiel #2
0
        public static string WhatIs(string program)
        {
            Turing turing = new Turing();

            turing.Execute(program);
            return(turing.GetOutput());
        }
Beispiel #3
0
        public static string TestState(string program)
        {
            Turing turing = new Turing();

            turing.Execute(program);
            return(turing.GetMemory());
        }
Beispiel #4
0
        static string post(string program)
        {
            Turing        turing = new Turing();
            StringBuilder sb     = new StringBuilder();

            for (int i = 0; i < program.Length; i++)
            {
                Pattern pattern = detectPattern(program, i);
                if (pattern.Occurrences > 0)
                {
                    string loop = replaceProgram(turing, pattern);
                    sb.Append(loop);
                    turing.Execute(loop);
                    int length = pattern.Base.Length * pattern.Occurrences;
                    i += length - 1;
                    continue;
                }
                if (program[i] == '[')
                {
                    int startIndex = i;
                    int stack      = 1;
                    while (stack != 0)
                    {
                        char b = program[++i];
                        if (b == '[')
                        {
                            stack++;
                        }
                        else if (b == ']')
                        {
                            stack--;
                        }
                    }
                    string block = program.Substring(startIndex, i - startIndex + 1);
                    sb.Append(block);
                    turing.Execute(block);
                    continue;
                }
                sb.Append(program[i]);
                turing.Execute("" + program[i]);
            }
            string output = turing.GetOutput();

            return(sb.ToString());
        }
Beispiel #5
0
        static string preset(Turing turing, string text)
        {
            string program = "";
            int    length  = text.Length;

            for (int i = 0; i < length; i++)
            {
                //
                int    current   = turing.Current();
                int    target    = (text[i] == ' ') ? 0 : text[i] - 'A' + 1;
                int    alphaD    = AlphaDs[current, target];
                int    alphaDAbs = AlphaDsAbs[current, target];
                bool   reset     = AlphaRs[current, target];
                string fragment  = (reset ? "[-]" : "") + new string(alphaD > 0 ? '+' : '-', reset ? alphaDAbs - 3 : alphaDAbs);
                if (i != length - 1)
                {
                    fragment += ">";
                }
                turing.Execute(fragment);
                //
                program += fragment;
            }
            return(program);
        }
Beispiel #6
0
        static string pre(Turing turing)
        {
            string program = "";
            //
            Dictionary <char, int>      cFrequencies = PhraseString.ToCharArray().Where(c => c != ' ').GroupBy(c => c).ToDictionary(c => c.Key, c => c.Count());
            List <Tuple <char, int> >   chars        = cFrequencies.ToList().OrderByDescending(c => c.Value).Select(c => new Tuple <char, int>(c.Key, c.Value)).ToList();
            Dictionary <string, int>    wFrequencies = PhraseString.Split(' ').GroupBy(w => w).ToDictionary(w => w.Key, w => w.Count());
            List <Tuple <string, int> > words        = wFrequencies.ToList().OrderByDescending(w => w.Value).Select(w => new Tuple <string, int>(w.Key, w.Value)).ToList();;
            List <string> shortWords = wFrequencies.ToList().OrderBy(w => w.Key.Length).Select(w => w.Key).ToList();

            //Random 4 chars
            if (PhraseString.Length > 20 && chars.Count() == 3)
            {
                string seed = "" + chars[1].Item1 + chars[0].Item1 + chars[2].Item1;
                PatternDebug += "Random 4 " + seed + " ";
                program      += preset(turing, "" + seed);
                return(program);
            }
            //Two words alternating
            if (shortWords.Count() <= 6 && shortWords.Count() >= 4)
            {
                string w1 = shortWords[0]; string w2 = shortWords[1];
                bool   isGood    = true;
                string remainder = PhraseString;
                while (remainder.Length > 0)
                {
                    if (remainder.StartsWith(" "))
                    {
                        remainder = remainder.Substring(1);
                        continue;
                    }
                    else if (remainder.StartsWith(w1))
                    {
                        remainder = remainder.Substring(w1.Length);
                        continue;
                    }
                    else if (remainder.StartsWith(w2))
                    {
                        remainder = remainder.Substring(w2.Length);
                        continue;
                    }
                    isGood = false;
                    break;
                }
                if (isGood)
                {
                    PatternDebug += "Two Words ";
                    //Set all to M
                    program += "-[<+>++]<[<-]>";
                    turing.Execute(program);
                    //Preset two words and spacer
                    program += preset(turing, w1 + " " + w2 + " ");
                    program += "<[<]";
                    //
                    remainder = PhraseString;
                    while (remainder.Length > 0)
                    {
                        if (remainder.StartsWith(" "))
                        {
                            remainder = remainder.Substring(1);
                            program  += ".";
                        }
                        else if (remainder.StartsWith(w1))
                        {
                            remainder = remainder.Substring(w1.Length);
                            program  += "<[<]>[.>]";
                        }
                        else if (remainder.StartsWith(w2))
                        {
                            remainder = remainder.Substring(w2.Length);
                            program  += ">[.>]<[<]";
                        }
                    }
                    return("x" + program);
                }
            }
            //Freq analysis
            if (PhraseString.Length > 300)
            {
                //
                program += "+++++[<<<+>+++>+>-]"; //EOE
                turing.Execute(program);
                string phrase = words[0].Item1;
                program += preset(turing, phrase); //executed
                //
                string fragment = "";              // "<[<]";
                turing.Execute(fragment);
                //
                PatternDebug += "Frequency Analysis " + words[0].Item1 + " ";
                return(program + fragment);
            }
            //First letter is O
            if (PhraseString.Length > 2 && PhraseString[0] == 'O' && PhraseString[1] != ' ')
            {
                PatternDebug += "First Letter O ";
                program       = "+++[<+>--]";
            }
            //Middle Spell
            int dev0 = 0;
            int devM = 0;

            for (int i = 0; i < PhraseInt.Length; i++)
            {
                dev0 += Math.Min(27 - PhraseInt[i], PhraseInt[i]);
                devM += Math.Abs(PhraseInt[i] - 13);
            }
            double avg0 = 1.0 * dev0 / PhraseInt.Length;
            double avgM = 1.0 * devM / PhraseInt.Length;

            if ((avgM + 0.1) < avg0)
            {
                Pattern pattern = detectPattern(PhraseString, 0);
                if (pattern.Occurrences <= 0)
                {
                    PatternDebug += "Middle Spell ";
                    program       = "+[<<+>+>++]";
                }
            }
            //
            turing.Execute(program);
            return(program);
        }
Beispiel #7
0
        static string run(Turing turing)
        {
            //
            StringBuilder sb       = new StringBuilder();
            int           residual = 0;

            for (int i = 0; i < PhraseString.Length;)
            {
                Pattern pattern = detectPattern(PhraseString, i);
                if (pattern.Occurrences > 1 && pattern.Base.Length > 4)
                {
                    //
                    flush(turing, sb, i - residual, residual);
                    residual = 0;
                    //
                    PatternDebug += pattern.Occurrences.ToString() + "x(" + pattern.Base + ") ";
                    //
                    if (pattern.Base.Length == 10)
                    {
                        //GAAVOOOLLU
                        string token = "";
                        char   temp  = ' ';
                        foreach (char c in pattern.Base)
                        {
                            if (c != temp)
                            {
                                temp   = c;
                                token += c;
                            }
                        }
                        sb.Append(preset(turing, "" + token + " ABCDEFGHI"[3]));
                        //
                        string walk = ""; temp = ' ';
                        foreach (char c in pattern.Base)
                        {
                            if (c != temp)
                            {
                                walk += ">."; temp = c;
                            }
                            else
                            {
                                walk += ".";
                            }
                        }
                        string f = "[[<]>.>..>.>...>..>.>+++]";
                        f = "[[<]" + walk + ">+++]";
                        sb.Append(f);
                        turing.Execute(f);
                        string o = turing.GetOutput();
                        i += 10 * pattern.Occurrences;
                        continue;
                    }
                    //_WORD_E
                    int setCounter = pattern.Occurrences;
                    int loops      = 0;
                    if (setCounter > 26)
                    {
                        loops      = setCounter / 26;
                        setCounter = setCounter % 26;
                    }
                    string dialCounter = "-";
                    if (Loops.ContainsKey(setCounter))
                    {
                        string[] specs = Loops[pattern.Occurrences].Split(',');
                        setCounter = int.Parse(specs[0]);
                        int d = int.Parse(specs[1]);
                        dialCounter = new string(d > 0 ? '+' : '-', Math.Abs(d));
                    }
                    //Set counter is always positive and at most 9
                    char counter = " ABCDEFGHI"[setCounter];
                    sb.Append(preset(turing, "" + pattern.Base.Trim() + " " + counter));
                    //
                    string fragment = "";
                    if (pattern.Base.StartsWith(" "))
                    {
                        fragment = "[<<[<].>[.>]>@]".Replace("@", dialCounter);
                    }
                    else if (pattern.Base.EndsWith(" "))
                    {
                        fragment = "[<<[<]>[.>].>@]".Replace("@", dialCounter);
                    }
                    else
                    {
                        fragment = "[<<[<]>[.>]>@]".Replace("@", dialCounter);
                        if (hasSpace(pattern.Base))
                        {
                            fragment = "[<<[<]<[<]>[.>].>[.>]>@]".Replace("@", dialCounter);
                            if (pattern.Base.Length == 7 && pattern.Base[5] == ' ')
                            {
                                fragment = "[<<<<<<<<[.>].>.>>@]".Replace("@", dialCounter);
                            }
                        }
                    }
                    sb.Append(fragment);
                    turing.Execute(fragment);
                    //
                    while (loops > 0)
                    {
                        bool isHere = true;
                        if (pattern.Base.StartsWith(" "))
                        {
                            fragment = "-[<<[<].>[.>]>-]";
                        }
                        else if (pattern.Base.EndsWith(" "))
                        {
                            fragment = "-[<<[<]>[.>].>-]";
                        }
                        else
                        {
                            fragment = "-[<<[<]>[.>]>-]";
                        }
                        sb.Append(fragment);
                        turing.Execute(fragment);
                        loops--;
                    }
                    //
                    i += pattern.Base.Length * pattern.Occurrences;
                    continue;
                }
                residual++;
                i++;
            }
            flush(turing, sb, PhraseString.Length - residual, residual);
            return(sb.ToString());
        }
Beispiel #8
0
        static void Main(string[] args)
        {
            Start();
            System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();
            string result = "";
            int    total  = 0;
            bool   ok     = true;

            for (int i = 0; i < TestCases.Length; i++)
            {
                //
                stopWatch.Reset();
                stopWatch.Start();
                //
                string program = Bilbo.Optimize(TestCases[i]);
                //
                stopWatch.Stop();
                //
                string item = "";
                item += TestCases[i] + NewLine + program + NewLine;
                //
                Turing turing = new Turing();
                turing.Execute(program);
                string output  = turing.GetOutput();
                bool   correct = output.Equals(TestCases[i]);
                if (correct)
                {
                    //
                    item += new Turing().ExecuteLetters(program) + NewLine;
                    item += program.Length + " ";
                    if (program.Length > BestCases[i].Length)
                    {
                        item += "***Suboptimal*** ";
                    }
                    if (program.Length < BestCases[i].Length)
                    {
                        item += "***New Best Case*** ";
                    }
                    total += program.Length;
                }
                else
                {
                    item += "PROBLEM ";
                    ok    = false;
                }
                //
                Turing t2 = new Turing();
                t2.Execute(BestCases[i]);
                if (!t2.GetOutput().Equals(TestCases[i]))
                {
                    throw new Exception("Best case is wrong.");
                }
                //
                long time = stopWatch.ElapsedMilliseconds;
                item += time + "ms -- " + Bilbo.PatternDebug;
                //
                item   += NewLine + NewLine;
                result += item;
            }

            logResult(result, total, ok);
        }