public static void startDay2() { int validPasswordsCount = 0; string filePath = @"C:\Users\Sam\Downloads\input2.txt"; string[] day2Array = Day2.readAllLines(filePath); int fileLength = Day2.getFileLength(filePath); int index = 1; Console.WriteLine("Password ({0}) is ({1}) to ({2}) long and we are looking for the character ({3}) and the password is ({4})", index, Day2.getLineMin(day2Array, index), Day2.getLineMax(day2Array, index), Day2.getLineChar(day2Array, index), Day2.getLinePassword(day2Array, index)); for (int i = 0; i < day2Array.Length; i++) { string password = Day2.getLinePassword(day2Array, i); int minOccurence = Day2.getLineMin(day2Array, i); int maxOccurence = Day2.getLineMax(day2Array, i); char lookupChar = Convert.ToChar(Day2.getLineChar(day2Array, i)); int charFreq = password.Count(f => (f == lookupChar)); if (charFreq >= minOccurence && charFreq <= maxOccurence) { validPasswordsCount++; } } Console.WriteLine(validPasswordsCount); //Console.WriteLine(Day2.getLineMax(day2Array, 1)); }
public static void startDay2Part2() { int validPasswordsCount = 0; string filePath = @"C:\Users\Sam\Downloads\input2.txt"; string[] day2Array = Day2.readAllLines(filePath); for (int i = 0; i < day2Array.Length; i++) { string password = Day2.getLinePassword(day2Array, i); int posOfChar = Day2.getLineMin(day2Array, i); int endPosNotChar = Day2.getLineMax(day2Array, i); char lookupChar = Convert.ToChar(Day2.getLineChar(day2Array, i)); char[] charArray = password.ToCharArray(); int charArrayLength = charArray.Length; char space = (char)32; List <char> tempList = charArray.ToList(); tempList.Add(space); charArray = tempList.ToArray(); if (!(charArray[posOfChar - 1] == lookupChar && charArray[endPosNotChar - 1] == lookupChar)) { if (!(charArray[posOfChar - 1] != lookupChar && charArray[endPosNotChar - 1] != lookupChar)) { if (charArray[posOfChar - 1] == lookupChar && charArray[endPosNotChar - 1] != lookupChar || charArray[posOfChar - 1] != lookupChar && charArray[endPosNotChar - 1] == lookupChar) { validPasswordsCount++; } } } } Console.WriteLine(validPasswordsCount); }