Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions; // 90/100 in Judge
- 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
- if (regexForWinningTicket.IsMatch(leftSide) && regexForWinningTicket.IsMatch(rightSide))
- {
- CheckSideForWinnerSequences(leftSide, out var maxLeftLengthOfTheSameSequences,
- out var leftWinningSymbols);
- CheckSideForWinnerSequences(rightSide, out var maxRightLengthOfTheSameSequences,
- out var rightWinningSymbol);
- int validTheRepeatingLengthInBothSide = Math.Min(maxRightLengthOfTheSameSequences,
- maxLeftLengthOfTheSameSequences);
- if (rightWinningSymbol == leftWinningSymbols && validTheRepeatingLengthInBothSide >= 6)
- {
- Console.WriteLine(
- validTheRepeatingLengthInBothSide < 10
- ? $"ticket \"{ticket}\" - {validTheRepeatingLengthInBothSide}{rightWinningSymbol}"
- : $"ticket \"{ticket}\" - {validTheRepeatingLengthInBothSide}{rightWinningSymbol} Jackpot!");
- }
- else
- {
- Console.WriteLine($"ticket \"{ticket}\" - no match");
- }
- }
- else
- {
- Console.WriteLine($"ticket \"{ticket}\" - no match");
- }
- }
- }
- }
- private static void CheckSideForWinnerSequences(string side,
- out int maxLengthOfTheSameSequences,
- out char winningSymbol)
- {
- var lengthOfTheSameSequences = 0;
- maxLengthOfTheSameSequences = 0;
- winningSymbol = '\0';
- var lastWinningSymbol = '\0';
- foreach (var ch in side)
- {
- if (ch == '@' || ch == '$' || ch == '#' || ch == '^')
- {
- if (ch != lastWinningSymbol)
- {
- if (lengthOfTheSameSequences > maxLengthOfTheSameSequences)
- {
- maxLengthOfTheSameSequences = lengthOfTheSameSequences; //which is the maxSequences length
- winningSymbol = lastWinningSymbol; //which is the winner symbol
- }
- lastWinningSymbol = ch;
- lengthOfTheSameSequences = 1;
- }
- else
- {
- lengthOfTheSameSequences++;
- }
- }
- else
- {
- if (lengthOfTheSameSequences > maxLengthOfTheSameSequences)
- {
- maxLengthOfTheSameSequences = lengthOfTheSameSequences; //which is the maxSequences length
- winningSymbol = lastWinningSymbol; //which is the winner symbol
- }
- lastWinningSymbol = '\0';
- lengthOfTheSameSequences = 0;
- }
- }
- if (lengthOfTheSameSequences > maxLengthOfTheSameSequences)
- {
- maxLengthOfTheSameSequences = lengthOfTheSameSequences; //which is the maxSequences length
- winningSymbol = lastWinningSymbol; //which is the winner symbol
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment