simonradev

WinningTicketSecondTry

Feb 9th, 2017
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 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.                 MatchCollection allMatches = winningTicket.Matches(ticket);
  40.  
  41.                 List<string> matches = new List<string>();
  42.                 foreach (Match currMatch in allMatches)
  43.                 {
  44.                     matches.Add(currMatch.Groups[1].ToString());
  45.                 }
  46.  
  47.                 bool thereIsMatch = false;
  48.                 for (int currMatch = 0; currMatch < matches.Count; currMatch++)
  49.                 {
  50.                     for (int currCheck = currMatch + 1; currCheck < matches.Count; currCheck++)
  51.                     {
  52.                         if (matches[currMatch] == matches[currCheck])
  53.                         {
  54.                             thereIsMatch = true;
  55.                             break;
  56.                         }
  57.                     }
  58.  
  59.                     if (thereIsMatch)
  60.                     {
  61.                         result[currTicket] = $"ticket \"{ticket}\" - {matches[currMatch].Length}{matches[currMatch][0]}";
  62.                         break;
  63.                     }
  64.                 }
  65.  
  66.                 if (!thereIsMatch)
  67.                 {
  68.                     result[currTicket] = $"ticket \"{ticket}\" - no match";
  69.                 }
  70.             }
  71.  
  72.             for (int currTicket = 0; currTicket < result.Length; currTicket++)
  73.             {
  74.                 Console.WriteLine(result[currTicket]);
  75.             }
  76.         }
  77.     }
  78. }
Add Comment
Please, Sign In to add comment