Пример #1
0
        public void PerfOfPathMap()
        {
            var g = new PathGrid(6);

            var count = 0L;

            var timer = System.Diagnostics.Stopwatch.StartNew();

            foreach (var step in g.Steps())
                count++;

            timer.Stop();

            Console.WriteLine("It took {0}ms to generate all {1} steps", timer.ElapsedMilliseconds, count);
        }
        public MathsStep Solve(out long count_of_routes_tried, long? timeout_ms = null)
        {
            var g = new PathGrid(this.Numbers.Length);

            var steps = this.Numbers.Select(x => new MathsStartStep(x)).ToArray();

            var invalid_division_flag = false;

            count_of_routes_tried = 0L;

            MathsSolution solution = null;

            MathsStep best_step = new MathsStartStep(0);

            System.Diagnostics.Stopwatch watch = null;

            if (timeout_ms != null)
                watch = System.Diagnostics.Stopwatch.StartNew();

            foreach (var step in g.Steps())
            {
                if (step.FrameSize == this.Numbers.Length)
                {
                    solution = new MathsSolution(steps);
                    invalid_division_flag = false;
                    count_of_routes_tried++;
                }
                else if (invalid_division_flag)
                    continue;

                var curr_best = solution.ClosestTo(this.Target);

                if (first_number_is_closer(this.Target, curr_best.Result(), best_step.Result()))
                {
                    best_step = curr_best;

                    if (curr_best.Result() == this.Target)
                        break;
                }

                if (watch != null)
                {
                    if (watch.ElapsedMilliseconds > timeout_ms)
                        break;
                }

                if (Operation.FromInt(step.Operation) == Operation.Division)
                {

                    if (solution.Test(step.Index1, step.Index2, (x, y) => { if (y.Result() == 0 || x.Result() == 0) return true; return x.Result() % y.Result() != 0; }))
                    {
                        invalid_division_flag = true;
                        continue;
                    }
                }

                if (!invalid_division_flag)
                    solution.Add(step.Index1, step.Index2, Operation.FromInt(step.Operation));
            }

            return best_step;
        }