Example #1
0
        private static IEnumerable <Tile> Parse(ReadOnlyMemory <string> input)
        {
            foreach (var tileInput in input.Split(string.Empty))
            {
                var id = int.Parse(tileInput.Span[0].AsSpan().TrimStart("Tile ").TrimEnd(":"));

                yield return(new Tile(id, tileInput.Slice(1).Span));
            }
        }
Example #2
0
        private static IEnumerable <ISet <char> > AnyoneAnswered(ReadOnlyMemory <string> input)
        {
            foreach (var group in input.Split(string.Empty))
            {
                var correctAnswers = new HashSet <char>();

                foreach (var line in group)
                {
                    correctAnswers.UnionWith(line);
                }

                yield return(correctAnswers);
            }
        }
Example #3
0
        private static IEnumerable <ISet <char> > EveryoneAnswered(ReadOnlyMemory <string> input)
        {
            foreach (var group in input.Split(string.Empty))
            {
                HashSet <char> groupAnswers = null;

                foreach (var line in group)
                {
                    groupAnswers ??= new HashSet <char>(line);
                    groupAnswers.IntersectWith(line);
                }

                yield return(groupAnswers);
            }
        }
Example #4
0
        private static IEnumerable <IDictionary <string, string> > ParsePassports(ReadOnlyMemory <string> input)
        {
            foreach (var group in input.Split(string.Empty))
            {
                var passport = new Dictionary <string, string>();

                foreach (var line in group)
                {
                    foreach (Match match in KeyValuePattern.Matches(line))
                    {
                        if (match.TryCapture(out var key, out var value))
                        {
                            passport.Add(key, value);
                        }
                    }
                }

                yield return(passport);
            }
        }