Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace Day_04
- {
- class Program
- {
- static void Main(string[] args)
- {
- //Part1();
- //Part2();
- Test();
- }
- static bool MeetsPasswordCriteria(int possiblePassword)
- {
- /*
- Password rules:
- - It is a six-digit number.
- - The value is within the range given in your puzzle input.
- - Two adjacent digits are the same (like 22 in 122345).
- - Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
- */
- // Check it is a six-digit number.
- if(possiblePassword.ToString().Length != 6)
- {
- return false;
- }
- // Check for two adjacent digits are the same (like 22 in 122345).
- if(!Regex.IsMatch(possiblePassword.ToString(),"(\\d)\\1"))
- {
- return false;
- }
- // Check that, going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
- int previousDigit = 0;
- foreach(char digitChar in possiblePassword.ToString())
- {
- int currentDigit = (int)Char.GetNumericValue(digitChar);
- if(currentDigit < previousDigit)
- {
- return false;
- }
- else
- {
- previousDigit = currentDigit;
- }
- }
- return true;
- }
- static bool MeetsUpdatedPasswordCriteria(int possiblePassword)
- {
- /*
- Password rules:
- - It is a six-digit number.
- - The value is within the range given in your puzzle input.
- - Two adjacent digits are the same (like 22 in 122345).
- + The two adjacent matching digits are not part of a larger group of matching digits.
- - Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
- */
- // Check it is a six-digit number.
- if(possiblePassword.ToString().Length != 6)
- {
- return false;
- }
- // Check for two adjacent digits are the same (like 22 in 122345) but not part of a larger group of matching digits (like 222 in 122245).
- // int? previousDigit = null;
- // int digitRepeatCount = 0;
- // foreach(char digitChar in possiblePassword.ToString())
- // {
- // int currentDigit = (int)Char.GetNumericValue(digitChar);
- // if(currentDigit == previousDigit)
- // {
- // digitRepeatCount += 1;
- // }
- // else
- // {
- // if(!(digitRepeatCount%2 == 0))
- // {
- // // If not an even number of repeated digits
- // return false;
- // }
- // previousDigit = currentDigit;
- // }
- // }
- Regex pairMatch = new Regex(@"(\d)\1");
- Regex quadMatch = new Regex(@"(\d)\1\1\1");
- Regex hexMatch = new Regex(@"(\d)\1\1\1\1\1");
- Regex trioMatch = new Regex(@"(\d)\1\1");
- Regex quinMatch = new Regex(@"(\d)\1\1\1\1");
- Match goodPairMatch = pairMatch.Match(possiblePassword.ToString());
- Match goodQuadMatch = quadMatch.Match(possiblePassword.ToString());
- Match goodHexMatch = hexMatch.Match(possiblePassword.ToString());
- Match badTrioMatch = trioMatch.Match(possiblePassword.ToString());
- Match badQuinMatch = quinMatch.Match(possiblePassword.ToString());
- if (goodPairMatch.Success)
- {
- // if(badTrioMatch.Success || badQuinMatch.Success)
- // {
- // Console.WriteLine($"{goodMatch.Groups[1].Value} -> {possiblePassword} [{goodMatchCount}] {badTrioMatch.Success}");
- // }
- // Need to check there are no triple or quintuple digit matches that aren't actually part of two or four double digit matches
- if((!goodHexMatch.Success && badQuinMatch.Success) || (!goodQuadMatch.Success && badTrioMatch.Success))
- {
- if((!goodQuadMatch.Success && badTrioMatch.Success))
- {
- // If there is a trio match and no quad match, we need to check whether there is still a valid duo match of a different number to the trio match
- // e.g. 111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
- MatchCollection pairMatches = Regex.Matches(possiblePassword.ToString(), @"(\d)\1");
- string badVal = badTrioMatch.Value;
- bool foundGoodVal = false;
- foreach (Match match in pairMatches)
- {
- foreach (Capture capture in match.Captures)
- {
- //Console.WriteLine($"Capture value is {capture.Value}");
- //Console.WriteLine($"badVal is {badVal}");
- if(!badVal.Contains(capture.Value))
- {
- foundGoodVal = true;
- }
- }
- }
- if(!foundGoodVal)
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- }
- }
- else
- {
- return false;
- }
- // Check that, going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
- int previousDigit = 0;
- foreach(char digitChar in possiblePassword.ToString())
- {
- int currentDigit = (int)Char.GetNumericValue(digitChar);
- if(currentDigit < previousDigit)
- {
- return false;
- }
- else
- {
- previousDigit = currentDigit;
- }
- }
- return true;
- }
- static void Test()
- {
- // Console.WriteLine(MeetsUpdatedPasswordCriteria(123456) == false); // false
- // Console.WriteLine(MeetsUpdatedPasswordCriteria(113456) == true); // true
- // Console.WriteLine(MeetsUpdatedPasswordCriteria(111456) == false); // false
- // Console.WriteLine(MeetsUpdatedPasswordCriteria(111156) == true); // true
- // Console.WriteLine(MeetsUpdatedPasswordCriteria(111116) == false); // false
- // Console.WriteLine(MeetsUpdatedPasswordCriteria(111111) == true); // true
- // Console.WriteLine(MeetsUpdatedPasswordCriteria(111122) == true); // true
- // Console.WriteLine(MeetsUpdatedPasswordCriteria(111233) == true); // true
- /*
- 112233 meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
- 123444 no longer meets the criteria (the repeated 44 is part of a larger group of 444).
- 111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
- */
- Console.WriteLine(MeetsUpdatedPasswordCriteria(112233) == true); // true
- Console.WriteLine(MeetsUpdatedPasswordCriteria(123444) == false); // false
- Console.WriteLine(MeetsUpdatedPasswordCriteria(111122) == true); // true
- }
- static void Part1()
- {
- // There is only one line so get it
- var input = File.ReadAllLines("input.txt")[0];
- // Get the range boundaries
- int lowerBound = System.Convert.ToInt32(input.Split('-')[0]);
- int higherBound = System.Convert.ToInt32(input.Split('-')[1]);
- List<int> possiblePasswords = new List<int>(){};
- // For each possible password, check if it meets the criteria and if it does, add it to a list of possible passwords
- for (int possiblePassword = lowerBound; possiblePassword < higherBound+1; possiblePassword++)
- {
- if(MeetsPasswordCriteria(possiblePassword))
- {
- possiblePasswords.Add(possiblePassword);
- }
- }
- // Get the length of the list of possible passwords
- int possiblePasswordCount = possiblePasswords.Count();
- Console.WriteLine($"Part 1: The number of different passwords within the range given in my puzzle input meeting the criteria is {possiblePasswordCount}");
- }
- static void Part2()
- {
- // There is only one line so get it
- var input = File.ReadAllLines("input.txt")[0];
- // Get the range boundaries
- int lowerBound = System.Convert.ToInt32(input.Split('-')[0]);
- int higherBound = System.Convert.ToInt32(input.Split('-')[1]);
- List<int> possiblePasswords = new List<int>(){};
- // For each possible password, check if it meets the criteria and if it does, add it to a list of possible passwords
- for (int possiblePassword = lowerBound; possiblePassword < higherBound+1; possiblePassword++)
- {
- if(MeetsUpdatedPasswordCriteria(possiblePassword))
- {
- possiblePasswords.Add(possiblePassword);
- }
- }
- // Get the length of the list of possible passwords
- int possiblePasswordCount = possiblePasswords.Count();
- Console.WriteLine($"Part 2: The number of different passwords within the range given in my puzzle input meeting the updated criteria is {possiblePasswordCount}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement