Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions;
- namespace TextAndRegex
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] ticketsCollection =
- Console.ReadLine().Split(new string[] { ",", " " }, StringSplitOptions.RemoveEmptyEntries);
- Regex regexForWinningTicket = new Regex(@"([@]{6,10})|([#]{6,10})|([$]{6,10})|([\^]{6,10})");
- foreach (string ticket in ticketsCollection)
- {
- if (ticket.Length != 20)
- {
- Console.WriteLine("invalid ticket");
- }
- else if (ticket.Length == 20) //only 20 is valid ticket length
- {
- string leftSide = ticket.Substring(0, 10); //left side
- string rightSide = ticket.Substring(10, 10); //right side
- MatchCollection matchLeftSide = regexForWinningTicket.Matches(leftSide); // match left side to check is it valid
- MatchCollection matchRightSide = regexForWinningTicket.Matches(rightSide); //match right side to check is it valid
- if (regexForWinningTicket.IsMatch(leftSide) && regexForWinningTicket.IsMatch(rightSide))
- {
- if (matchLeftSide[0].Value.Contains(matchRightSide[0].Value[0]) == false)//if the all left collection contains the first symbol from the right collection==false
- {
- Console.WriteLine($"ticket \"{ticket}\" - no match");
- }
- else if (matchLeftSide[0].Value[0]==matchRightSide[0].Value[0])//if the first symbol from the left collection is equal to the first symbol from the right collection
- {
- char theSameSymbol = matchRightSide[0].Value[0];
- int validTheRepeatingLengthInBothSide = Math.Min(
- matchLeftSide[0].Value.Length,
- matchRightSide[0].Value.Length);
- if ((validTheRepeatingLengthInBothSide >= 6 && validTheRepeatingLengthInBothSide <= 9))
- {
- Console.WriteLine(
- $"ticket \"{ticket}\" - {validTheRepeatingLengthInBothSide}{theSameSymbol}");
- }
- else if ((validTheRepeatingLengthInBothSide == 10))
- {
- Console.WriteLine(
- $"ticket \"{ticket}\" - {validTheRepeatingLengthInBothSide}{theSameSymbol} Jackpot!");
- }
- }
- }
- else if (regexForWinningTicket.IsMatch(leftSide) == false ||
- regexForWinningTicket.IsMatch(rightSide) == false)
- {
- Console.WriteLine($"ticket \"{ticket}\" - no match");
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement