Ejemplo n.º 1
0
        public static string WhatIs(string program)
        {
            Turing turing = new Turing();

            turing.Execute(program);
            return(turing.GetOutput());
        }
Ejemplo n.º 2
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());
        }
Ejemplo n.º 3
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());
        }
Ejemplo n.º 4
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);
        }