Exemplo n.º 1
0
        public static State[] ParseRule(string rule, BusyBeaver beaver)
        {
            var ruleParts = rule.Split(" ");

            var stateCount = ruleParts.Length / 2;

            if (ruleParts.Length % 2 != 0)
            {
                throw new InvalidOperationException("We need an even number of rules");
            }

            var states = new State[stateCount];

            for (var n = 0; n < stateCount; n++)
            {
                states[n] = new State(n.ToString());
            }

            var home = 'H' - 'A';

            for (var n = 0; n < stateCount; n++)
            {
                Rule Parse(string ruleToken)
                {
                    var setValue  = ruleToken[0];
                    var direction = ruleToken[1];
                    var newState  = ruleToken[2] - 'A';

                    return(new Rule(
                               setValue == '1' ? TapeValue.Set : TapeValue.NotSet,
                               direction == 'L' ? Direction.Left : Direction.Right,
                               newState >= 0 && newState != home ? states[newState] : null));
                }

                states[n].NotSetRule = Parse(ruleParts[n * 2]);
                states[n].SetRule    = Parse(ruleParts[n * 2 + 1]);
            }

            beaver.CurrentState = states[0];

            return(states);
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            var beaver = new BusyBeaverEngine.BusyBeaver();
            //var states = BeaverRuleParser.ParseRule("1L0 1RA", beaver);
            //var states = BeaverRuleParser.ParseRule("1RB 1LB 1LA 1R0", beaver);
            //var states = BeaverRuleParser.ParseRule("1RB 1RH 1LB 0RC 1LC 1LA", beaver);
            //var states = BeaverRuleParser.ParseRule("1RB 1LB 1LA 0LC 1RH 1LD 1RD 0RA", beaver);
            var states = BeaverRuleParser.ParseRule("1RB 1LC 1RC 1RB 1RD 0LE 1LA 1LD 1RH 0LA", beaver);

            //var states = BeaverRuleParser.ParseRule("1RB 1LC 0LA 0LD 1LA 1RH 1LB 1RE 0RD 0RB", beaver);
            //var states = BeaverRuleParser.ParseRule("1RB 0LC 1RC 1RD 1LA 0RB 0RE 1RH 1LC 1RA", beaver);


            foreach (var state in states)
            {
                Console.WriteLine(state.ToString());
            }

            var count     = 0;
            var stopWatch = Stopwatch.StartNew();

            while (!beaver.IsFinished)
            {
                //beaver.PrintState();
                beaver.DoRound();
                count++;

                if (count == Int32.MaxValue)
                {
                    break;
                }
            }

            stopWatch.Stop();

            beaver.PrintState();
            Console.WriteLine(beaver.Tape.CountOnes() + " ones established (" + beaver.Tape.FirstOne() + " to " +
                              beaver.Tape.LastOne() + ")");
            Console.WriteLine(count + " rounds");
            Console.WriteLine(stopWatch.Elapsed + " time elapsed");
        }