Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.52 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Day_04
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //Part1();
  14.             //Part2();
  15.             Test();
  16.         }
  17.  
  18.         static bool MeetsPasswordCriteria(int possiblePassword)
  19.         {
  20.             /*
  21.             Password rules:
  22.             - It is a six-digit number.
  23.             - The value is within the range given in your puzzle input.
  24.             - Two adjacent digits are the same (like 22 in 122345).
  25.             - Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
  26.             */
  27.  
  28.             // Check it is a six-digit number.
  29.             if(possiblePassword.ToString().Length != 6)
  30.             {
  31.                 return false;
  32.             }
  33.  
  34.             // Check for two adjacent digits are the same (like 22 in 122345).
  35.             if(!Regex.IsMatch(possiblePassword.ToString(),"(\\d)\\1"))
  36.             {
  37.                 return false;
  38.             }
  39.  
  40.             // Check that, going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
  41.             int previousDigit = 0;
  42.             foreach(char digitChar in possiblePassword.ToString())
  43.             {
  44.                 int currentDigit = (int)Char.GetNumericValue(digitChar);
  45.                 if(currentDigit < previousDigit)
  46.                 {
  47.                     return false;
  48.                 }
  49.                 else
  50.                 {
  51.                     previousDigit = currentDigit;
  52.                 }
  53.             }
  54.  
  55.             return true;
  56.         }
  57.  
  58.         static bool MeetsUpdatedPasswordCriteria(int possiblePassword)
  59.         {
  60.             /*
  61.             Password rules:
  62.             - It is a six-digit number.
  63.             - The value is within the range given in your puzzle input.
  64.             - Two adjacent digits are the same (like 22 in 122345).
  65.             + The two adjacent matching digits are not part of a larger group of matching digits.
  66.             - Going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
  67.             */
  68.  
  69.             // Check it is a six-digit number.
  70.             if(possiblePassword.ToString().Length != 6)
  71.             {
  72.                 return false;
  73.             }
  74.  
  75.             // 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).
  76.             //  int? previousDigit = null;
  77.             //  int digitRepeatCount = 0;
  78.             // foreach(char digitChar in possiblePassword.ToString())
  79.             // {
  80.             //     int currentDigit = (int)Char.GetNumericValue(digitChar);
  81.             //     if(currentDigit == previousDigit)
  82.             //     {
  83.             //         digitRepeatCount += 1;
  84.             //     }
  85.             //     else
  86.             //     {
  87.             //         if(!(digitRepeatCount%2 == 0))
  88.             //         {
  89.             //             // If not an even number of repeated digits
  90.             //             return false;
  91.             //         }
  92.             //         previousDigit = currentDigit;
  93.             //     }
  94.             // }
  95.            
  96.  
  97.  
  98.  
  99.             Regex pairMatch = new Regex(@"(\d)\1");
  100.             Regex quadMatch = new Regex(@"(\d)\1\1\1");
  101.             Regex hexMatch = new Regex(@"(\d)\1\1\1\1\1");
  102.             Regex trioMatch = new Regex(@"(\d)\1\1");
  103.             Regex quinMatch = new Regex(@"(\d)\1\1\1\1");
  104.             Match goodPairMatch = pairMatch.Match(possiblePassword.ToString());
  105.             Match goodQuadMatch = quadMatch.Match(possiblePassword.ToString());
  106.             Match goodHexMatch = hexMatch.Match(possiblePassword.ToString());
  107.             Match badTrioMatch = trioMatch.Match(possiblePassword.ToString());
  108.             Match badQuinMatch = quinMatch.Match(possiblePassword.ToString());
  109.             if (goodPairMatch.Success)
  110.             {
  111.                 // if(badTrioMatch.Success || badQuinMatch.Success)
  112.                 // {
  113.                 //     Console.WriteLine($"{goodMatch.Groups[1].Value} -> {possiblePassword} [{goodMatchCount}] {badTrioMatch.Success}");
  114.                 // }
  115.  
  116.                 // Need to check there are no triple or quintuple digit matches that aren't actually part of two or four double digit matches
  117.                 if((!goodHexMatch.Success && badQuinMatch.Success) || (!goodQuadMatch.Success && badTrioMatch.Success))
  118.                 {
  119.                     if((!goodQuadMatch.Success && badTrioMatch.Success))
  120.                     {
  121.                         // 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
  122.                         // e.g. 111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
  123.                        
  124.                         MatchCollection pairMatches = Regex.Matches(possiblePassword.ToString(), @"(\d)\1");
  125.                         string badVal = badTrioMatch.Value;
  126.                         bool foundGoodVal = false;
  127.  
  128.  
  129.                         foreach (Match match in pairMatches)
  130.                         {
  131.                             foreach (Capture capture in match.Captures)
  132.                             {
  133.                                 //Console.WriteLine($"Capture value is {capture.Value}");
  134.                                 //Console.WriteLine($"badVal is {badVal}");
  135.                                 if(!badVal.Contains(capture.Value))
  136.                                 {
  137.                                     foundGoodVal = true;
  138.                                 }
  139.                             }
  140.                         }
  141.                         if(!foundGoodVal)
  142.                         {
  143.                             return false;
  144.                         }
  145.                     }
  146.                     else
  147.                     {
  148.                         return false;
  149.                     }
  150.                 }
  151.             }
  152.             else
  153.             {
  154.                 return false;
  155.             }
  156.  
  157.             // Check that, going from left to right, the digits never decrease; they only ever increase or stay the same (like 111123 or 135679).
  158.             int previousDigit = 0;
  159.             foreach(char digitChar in possiblePassword.ToString())
  160.             {
  161.                 int currentDigit = (int)Char.GetNumericValue(digitChar);
  162.                 if(currentDigit < previousDigit)
  163.                 {
  164.                     return false;
  165.                 }
  166.                 else
  167.                 {
  168.                     previousDigit = currentDigit;
  169.                 }
  170.             }
  171.            
  172.             return true;
  173.         }
  174.  
  175.         static void Test()
  176.         {
  177.             // Console.WriteLine(MeetsUpdatedPasswordCriteria(123456) == false); // false
  178.             // Console.WriteLine(MeetsUpdatedPasswordCriteria(113456) == true); // true
  179.             // Console.WriteLine(MeetsUpdatedPasswordCriteria(111456) == false); // false
  180.             // Console.WriteLine(MeetsUpdatedPasswordCriteria(111156) == true); // true
  181.             // Console.WriteLine(MeetsUpdatedPasswordCriteria(111116) == false); // false
  182.             // Console.WriteLine(MeetsUpdatedPasswordCriteria(111111) == true); // true
  183.             // Console.WriteLine(MeetsUpdatedPasswordCriteria(111122) == true); // true
  184.             // Console.WriteLine(MeetsUpdatedPasswordCriteria(111233) == true); // true
  185.  
  186.  
  187.             /*
  188.                 112233 meets these criteria because the digits never decrease and all repeated digits are exactly two digits long.
  189.                 123444 no longer meets the criteria (the repeated 44 is part of a larger group of 444).
  190.                 111122 meets the criteria (even though 1 is repeated more than twice, it still contains a double 22).
  191.             */
  192.             Console.WriteLine(MeetsUpdatedPasswordCriteria(112233) == true); // true
  193.             Console.WriteLine(MeetsUpdatedPasswordCriteria(123444) == false); // false
  194.             Console.WriteLine(MeetsUpdatedPasswordCriteria(111122) == true); // true
  195.  
  196.         }
  197.  
  198.         static void Part1()
  199.         {
  200.             // There is only one line so get it
  201.             var input = File.ReadAllLines("input.txt")[0];
  202.             // Get the range boundaries
  203.             int lowerBound = System.Convert.ToInt32(input.Split('-')[0]);
  204.             int higherBound = System.Convert.ToInt32(input.Split('-')[1]);
  205.  
  206.             List<int> possiblePasswords = new List<int>(){};
  207.  
  208.             // For each possible password, check if it meets the criteria and if it does, add it to a list of possible passwords
  209.             for (int possiblePassword = lowerBound; possiblePassword < higherBound+1; possiblePassword++)
  210.             {
  211.                 if(MeetsPasswordCriteria(possiblePassword))
  212.                 {
  213.                     possiblePasswords.Add(possiblePassword);
  214.                 }
  215.             }
  216.  
  217.             // Get the length of the list of possible passwords
  218.             int possiblePasswordCount = possiblePasswords.Count();
  219.  
  220.             Console.WriteLine($"Part 1: The number of different passwords within the range given in my puzzle input meeting the criteria is {possiblePasswordCount}");
  221.         }
  222.  
  223.         static void Part2()
  224.         {
  225.            // There is only one line so get it
  226.             var input = File.ReadAllLines("input.txt")[0];
  227.             // Get the range boundaries
  228.             int lowerBound = System.Convert.ToInt32(input.Split('-')[0]);
  229.             int higherBound = System.Convert.ToInt32(input.Split('-')[1]);
  230.  
  231.             List<int> possiblePasswords = new List<int>(){};
  232.  
  233.             // For each possible password, check if it meets the criteria and if it does, add it to a list of possible passwords
  234.             for (int possiblePassword = lowerBound; possiblePassword < higherBound+1; possiblePassword++)
  235.             {
  236.                 if(MeetsUpdatedPasswordCriteria(possiblePassword))
  237.                 {
  238.                     possiblePasswords.Add(possiblePassword);
  239.                 }
  240.             }
  241.  
  242.             // Get the length of the list of possible passwords
  243.             int possiblePasswordCount = possiblePasswords.Count();
  244.  
  245.             Console.WriteLine($"Part 2: The number of different passwords within the range given in my puzzle input meeting the updated criteria is {possiblePasswordCount}");
  246.         }
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement