Beispiel #1
0
        public static int FirstPart(string filename)
        {
            int          sum           = 0;
            const string LINESEPARATOR = "\r\n";
            var          sections      = ReadInputs.ReadWholeFileAsString(ReadInputs.GetFullPath(filename)).Split(LINESEPARATOR + LINESEPARATOR);

            List <char> allYes = new List <char>();

            for (int index = 0; index < sections.Length; index++)
            {
                var section = sections[index];


                // PART I
                // List<char> chars = new List<char>();
                //var cleanSection = section.Replace(LINESEPARATOR, "");
                //for (int i = 0; i < cleanSection.Length; i++)
                //{
                //    var currentChar = Convert.ToChar(cleanSection.Substring(i, 1));
                //    if (!chars.Contains(currentChar)) chars.Add(currentChar);
                //}

                //sum += chars.Count;

                // PART II
                // Console.WriteLine("Section: \r\n{0}", section);
                var         lines   = section.Split(LINESEPARATOR).ToList();
                List <char> answers = new List <char>();

                for (var lineNumber = 0; lineNumber < lines.Count; lineNumber++)
                {
                    string line          = lines[lineNumber];
                    var    personAnswers = line.ToCharArray().Distinct().OrderBy(c => c).ToList();
                    if (lineNumber == 0)
                    {
                        answers = personAnswers;
                    }
                    else
                    {
                        Console.WriteLine("Comparing\r\n{0}\r\n{1}", string.Join(' ', answers), string.Join(' ', personAnswers));

                        for (var k = answers.Count - 1; k >= 0; k--)
                        {
                            if (!personAnswers.Contains(answers[k]))
                            {
                                Console.WriteLine("Removing {0}", answers[k]);
                                answers.RemoveAt(k);
                            }
                        }
                    }
                }

                Console.WriteLine("Answers left: {0}", string.Join(' ', answers));
                //Console.ReadKey();

                sum += answers.Count;
            }

            return(sum);
        }
Beispiel #2
0
        public static int FirstPart(string filename)
        {
            List <Passport> validPassports = new List <Passport>();

            var lines = ReadInputs.ReadWholeFileAsString(filename).Split(LINESEPARATOR + LINESEPARATOR).Select(l => l.Trim()).ToList();
            int count = 0;

            foreach (var line in lines)
            {
                var passport = new Passport(line, LINESEPARATOR);

                if (passport.IsValid())
                {
                    validPassports.Add(passport);
                    count++;
                }
            }

            //var birthYears = validPassports.Select(p => p.BirthYear).ToList();
            //var issueYears = validPassports.Select(p => p.IssueYear).ToList();
            //var expirationYears = validPassports.Select(p => p.ExpirationYear).ToList();
            //var heights = validPassports.Select(p => p.Height).ToList();
            //var haircolors = validPassports.Select(p => p.HairColor).ToList();
            //var eyecolors = validPassports.Select(p => p.EyeColor).ToList();
            //var passportIds = validPassports.Select(p => p.PassportId).ToList();

            return(count);
        }