Aborigenius

WinningTicketRegex

Aug 29th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.39 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading.Tasks;
  7.  
  8. namespace WinningTicket
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.  
  15.             string[] tickets = Regex.Split(Console.ReadLine(), @"\s*,\s*").Select(t => t.Trim()).Where(t => t.Length > 0)
  16.                 .ToArray();
  17.  
  18.             foreach (var ticket in tickets)
  19.             {
  20.                 if (ticket.Length != 20)
  21.                 {
  22.                     Console.WriteLine("invalid ticket");
  23.                 }
  24.                 else
  25.                 {
  26.                     string ticketLeftSide = ticket.Substring(0, 10);
  27.                     string ticketRightSide = ticket.Substring(10);
  28.  
  29. //                    Console.WriteLine($"TicketLeft: {ticketLeftSide}");
  30.            //         Console.WriteLine($"Ticket Right: {ticketRightSide}");
  31.  
  32.                     var leftSeq = FindMaxSeq(ticketLeftSide);
  33.                     var rightSeq = FindMaxSeq(ticketRightSide);
  34.  
  35.                   Console.WriteLine($"LeftSeq {leftSeq}");
  36.                    Console.WriteLine($"RightSeq {rightSeq}");
  37.  
  38.                     //if left/right match is 6 or more and first symbol[0] matches(left and right)
  39.                     if (leftSeq.Length >= 6 && rightSeq.Length >= 6
  40.                         && leftSeq[0] == rightSeq[0])
  41.                     {
  42.                         var count = Math.Min(leftSeq.Length, rightSeq.Length);
  43.                         Console.Write($"ticket \"{ticket}\" - {count}{leftSeq[0]}");
  44.                         if (count == 10)
  45.                         {
  46.                             Console.Write(" Jackpot!");
  47.  
  48.                         }
  49.                         Console.WriteLine();
  50.                     }
  51.                     else
  52.                     {
  53.                         Console.WriteLine($"ticket \"{ticket}\" - no match");
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.  
  59.         static string FindMaxSeq(string s)
  60.         {
  61.             string matched = String.Empty;
  62.               Regex pattern = new Regex(@"(?<match>#|$|^|@){6,10}");
  63.               Match m = pattern.Match(s);
  64.               if (m.Success)
  65.               {
  66.                   matched = m.Value;
  67.               }
  68.             return matched;
  69.         }
  70.     }
  71. }
  72. //'@', '#', '$' and '^' [#|$|^|@]{6,10}
Add Comment
Please, Sign In to add comment