Advertisement
Guest User

Untitled

a guest
Nov 26th, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4.  
  5. namespace TextAndRegex
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             string[] ticketsCollection =
  12.                 Console.ReadLine().Split(new string[] {",", " "}, StringSplitOptions.RemoveEmptyEntries);
  13.             Regex regexForWinningTicket = new Regex(@"([@]{6,10})|([#]{6,10})|([$]{6,10})|([\^]{6,10})");
  14.  
  15.             foreach (string ticket in ticketsCollection)
  16.             {
  17.                 if (ticket.Length != 20)
  18.                 {
  19.                     Console.WriteLine("invalid ticket");
  20.                 }
  21.  
  22.                 else if (ticket.Length == 20) //only 20 is valid ticket length
  23.                 {
  24.                     string leftSide = ticket.Substring(0, 10); //left side
  25.                     string rightSide = ticket.Substring(10, 10); //right side
  26.                     MatchCollection
  27.                         matchLeftSide = regexForWinningTicket.Matches(leftSide); // match left side to check is it valid
  28.                     MatchCollection
  29.                         matchRightSide =
  30.                             regexForWinningTicket.Matches(rightSide); //match right side to check is it valid
  31.  
  32.                     if (regexForWinningTicket.IsMatch(leftSide) && regexForWinningTicket.IsMatch(rightSide))
  33.                     {
  34.                         if (matchLeftSide[0].Value.Contains(matchRightSide[0].Value[0]) == false)
  35.                         {
  36.                             Console.WriteLine($"ticket \"{ticket}\" - no match");
  37.                         }
  38.  
  39.                         else if (matchLeftSide[0].Value.Contains(matchRightSide[0].Value[0]))
  40.                         {
  41.                             char theSameSymbol = matchRightSide[0].Value[0];
  42.                             int validTheRepeatingLengthInBothSide = Math.Min(
  43.                                 matchLeftSide[0].Value.Length,
  44.                                 matchRightSide[0].Value.Length);
  45.                             if ((validTheRepeatingLengthInBothSide >= 6 && validTheRepeatingLengthInBothSide <= 9))
  46.                             {
  47.                                 Console.WriteLine(
  48.                                     $"ticket \"{ticket}\" - {validTheRepeatingLengthInBothSide}{theSameSymbol}");
  49.                             }
  50.  
  51.                             else if ((validTheRepeatingLengthInBothSide == 10))
  52.                             {
  53.                                 Console.WriteLine(
  54.                                     $"ticket \"{ticket}\" - {validTheRepeatingLengthInBothSide}{theSameSymbol} Jackpot!");
  55.                             }
  56.                         }
  57.                     }
  58.  
  59.                     else if (regexForWinningTicket.IsMatch(leftSide) == false ||
  60.                              regexForWinningTicket.IsMatch(rightSide) == false)
  61.                     {
  62.                         Console.WriteLine($"ticket \"{ticket}\" - no match");
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement