Пример #1
0
        public void ShouldReturnCorrectNumberOfNoncyclicWordsForComplexBranches()
        {
            // arrange
            const int expected = 6;
            string    input    = $"^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$";

            // act
            IRegex result = Parser.Parse(input);

            // assert
            Assert.AreEqual(expected, result.GetNonCyclicWords().Count());
        }
Пример #2
0
        public void ShouldReturnCorrectNumberOfNoncyclicWordsForOneBranch()
        {
            // arrange
            const int expectedNumberOfWords = 2;
            string    input = $"^SN(E|TRE)$";

            // act
            IRegex result = Parser.Parse(input);

            // assert
            Assert.AreEqual(expectedNumberOfWords, result.GetNonCyclicWords().Count());
        }
Пример #3
0
        public void ShouldReturnCorrectNumberOfNoncyclicWordsForComplexBranchesWithOptional()
        {
            // arrange
            const int expected = 5;
            string    input    = $"^ESSWWN(E|NNENN(EESS(WNSE|)SSS|WWWSSSSE(SW|NNNE)))$";

            // act
            IRegex result = Parser.Parse(input);

            // assert
            Assert.AreEqual(expected, result.GetNonCyclicWords().Count());
        }
Пример #4
0
        static void Main(string[] args)
        {
            string filename = args[0];
            string input    = File.ReadAllText(filename);

            IRegex regex = Parser.Parse(input);

            Console.WriteLine($"Part1: {regex.GetLongestNonCyclicWord().Length}");

            HashSet <string> validPaths = new HashSet <string>();
            HashSet <string> allPaths   = regex.GetNonCyclicWords().ToHashSet();

            foreach (var path in allPaths)
            {
                for (int i = 1000; i <= path.Length; i++)
                {
                    validPaths.Add(path.Substring(0, i));
                }
            }
            Console.WriteLine(validPaths.Count());
            Console.WriteLine(validPaths.Select(GetRoomCoordinates).ToHashSet().Count());
        }
Пример #5
0
        private static IEnumerable <string> GetNonCyclicWords(IEnumerable <string> words, IEnumerable <IRegex> remainingParts)
        {
            if (!remainingParts.Any())
            {
                return(words);
            }
            IRegex nextPart = remainingParts.First();

            words = new List <string>(words);

            IEnumerable <string> nextWords = nextPart.GetNonCyclicWords();
            IEnumerable <string> newWords  = CreateCartesianProduct(words, nextWords);
            IEnumerable <string> toReturn  = GetNonCyclicWords(newWords, remainingParts.Skip(1));

            if (nextPart is OptionalBranchRegex)
            {
                var nextOptionalPart = (OptionalBranchRegex)nextPart;
                IEnumerable <string> nextOptionalWords = nextOptionalPart.GetLongestsNotYetCyclicWords();
                IEnumerable <string> newOptionalWords  = CreateCartesianProduct(words, nextOptionalWords);
                toReturn = toReturn.Concat(newOptionalWords);
            }

            return(toReturn);
        }