Advertisement
svephoto

Winning Ticket [C#]

Aug 12th, 2021
1,608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.10 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. namespace WinningTicket
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             string[] ticket = Console.ReadLine().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
  11.  
  12.             string pattern = @"(\@{6,}|\${6,}|\^{6,}|\#{6,})";
  13.             Regex regex = new Regex(pattern);
  14.  
  15.             for (int i = 0; i < ticket.Length; i++)
  16.             {
  17.                 if (ticket[i].Length == 20)
  18.                 {
  19.                     Match firstHalf = regex.Match(ticket[i].Substring(0, 10));
  20.                     Match secondHalf = regex.Match(ticket[i].Substring(10));
  21.                     int minLength = Math.Min(firstHalf.Length, secondHalf.Length);
  22.  
  23.                     if (firstHalf.Success && secondHalf.Success)
  24.                     {
  25.                         string winFirstHalf = firstHalf.Value.Substring(0, minLength);
  26.                         string winSecondHalf = secondHalf.Value.Substring(0, minLength);
  27.  
  28.                         if (winFirstHalf.Equals(winSecondHalf))
  29.                         {
  30.                             if (winFirstHalf.Length == 10)
  31.                             {
  32.                                 Console.WriteLine($"ticket \"{ticket[i]}\" - {minLength}{winFirstHalf.Substring(0, 1)} Jackpot!");
  33.                             }
  34.                             else
  35.                             {
  36.                                 Console.WriteLine($"ticket \"{ticket[i]}\" - {minLength}{winFirstHalf.Substring(0, 1)}");
  37.                             }
  38.                         }
  39.                         else
  40.                         {
  41.                             Console.WriteLine($"ticket \"{ticket[i]}\" - no match");
  42.                         }
  43.                     }
  44.                     else
  45.                     {
  46.                         Console.WriteLine($"ticket \"{ticket[i]}\" - no match");
  47.                     }
  48.                 }
  49.                 else
  50.                 {
  51.                     Console.WriteLine("invalid ticket");
  52.                 }
  53.             }
  54.         }
  55.     }
  56. }
  57.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement