Exemplo n.º 1
0
        public void TestTryRoutine(string layout, string main, string a, string b, string c)
        {
            var layoutRows = layout.Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
            var robot      = new SimulationRobot(layoutRows);
            var routine    = new MovementRoutine(main, a, b, c);

            Assert.IsTrue(routine.TryMainRoutine(robot));
        }
Exemplo n.º 2
0
        public static MovementRoutine FindValidMovementRoutine(IEnumerable <string> path, List <string>[] previousPatterns, int[] patternsUsed)
        {
            const int maxCharacters        = MovementRoutine.characterLimit;
            const int maxPatternsInRoutine = MovementRoutine.maxPatternsInRoutine;

            if (!path.Any())
            {
                var abc         = new string[] { "A", "B", "C" };
                var mainRoutine = patternsUsed.Select(p => abc[p]);
                var moveRoutine = new MovementRoutine(mainRoutine, previousPatterns[0], previousPatterns[1], previousPatterns[2]);
                if (moveRoutine.IsWithinCharacterLimits)
                {
                    return(moveRoutine);
                }
                else
                {
                    return(null);
                }
            }
            if (previousPatterns.Length >= 3)
            {
                return(null);
            }

            var patternsWithNew = new List <string> [previousPatterns.Length + 1];

            for (int patternId = 0; patternId < previousPatterns.Length; patternId++)
            {
                patternsWithNew[patternId] = previousPatterns[patternId];
            }
            var newPattern = new List <string>();

            patternsWithNew[previousPatterns.Length] = newPattern;

            var  pathAfterNewPattern     = path;
            int  patternLengthSansCommas = 0;
            bool patternIsValid          = true;
            var  pathEnumerator          = path.GetEnumerator();

            while (patternIsValid && pathEnumerator.MoveNext())
            {
                patternLengthSansCommas += pathEnumerator.Current.Length;
                if ((patternLengthSansCommas + newPattern.Count) <= maxCharacters)
                {
                    newPattern.Add(pathEnumerator.Current);
                    pathAfterNewPattern = pathAfterNewPattern.Skip(1);
                    var branches = TryFittingPathWithPatterns(pathAfterNewPattern, patternsWithNew, maxPatternsInRoutine - patternsUsed.Length);
                    foreach (var(remainingPath, patternsUsedInBranch) in branches)
                    {
                        var updatedPatternsUsed = patternsUsed.Concat(new int[] { previousPatterns.Length }).Concat(patternsUsedInBranch).ToArray();
                        var result = FindValidMovementRoutine(remainingPath, patternsWithNew, updatedPatternsUsed);
                        if (result != null)
                        {
                            return(result);
                        }
                    }
                }
                else
                {
                    patternIsValid = false;
                }
            }
            return(null);
        }