simonradev

WinningTicketFirstTry

Feb 9th, 2017
394
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8.  
  9. namespace WinningTicket
  10. {
  11.     public class WinningTicket
  12.     {
  13.         public static void Main()
  14.         {
  15.             string[] allTickets = Console.ReadLine().Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
  16.  
  17.             Regex winningTicket = new Regex(@"([$]{6,}|[@]{6,}|[\^]{6,}|[#]{6,})");
  18.             Regex jackpotTicket = new Regex(@"([$]{20}|[@]{20}|[\^]{20}|[#]{20})");
  19.  
  20.             string[] result = new string[allTickets.Length];
  21.             for (int currTicket = 0; currTicket < allTickets.Length; currTicket++)
  22.             {
  23.                 string ticket = allTickets[currTicket];
  24.  
  25.                 if (ticket.Length != 20)
  26.                 {
  27.                     result[currTicket] = "invalid ticket";
  28.  
  29.                     continue;
  30.                 }
  31.  
  32.                 if (jackpotTicket.IsMatch(ticket))
  33.                 {
  34.                     result[currTicket] = $"ticket \"{ticket}\" - 10{ticket[0]} Jackpot!";
  35.  
  36.                     continue;
  37.                 }
  38.  
  39.                 string leftHalf = ticket.Substring(0, 10);
  40.                 string rightHalf = ticket.Substring(10, 10);
  41.  
  42.                 if (!winningTicket.IsMatch(leftHalf))
  43.                 {
  44.                     result[currTicket] = $"ticket \"{ticket}\" - no match";
  45.  
  46.                     continue;
  47.                 }
  48.  
  49.                 Match leftMatch = winningTicket.Match(leftHalf);
  50.                 Match rightMatch = winningTicket.Match(rightHalf);
  51.  
  52.                 leftHalf = leftMatch.Groups[1].ToString();
  53.                 rightHalf = rightMatch.Groups[1].ToString();
  54.  
  55.                 if (leftHalf == rightHalf)
  56.                 {
  57.                     result[currTicket] = $"ticket \"{ticket}\" - {leftHalf.Length}{leftHalf[0]}";
  58.                 }
  59.             }
  60.  
  61.             for (int currTicket = 0; currTicket < result.Length; currTicket++)
  62.             {
  63.                 Console.WriteLine(result[currTicket]);
  64.             }
  65.         }
  66.     }
  67. }
Add Comment
Please, Sign In to add comment