Advertisement
Guest User

Untitled

a guest
Nov 21st, 2021
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.38 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace _01._Winning_Ticket
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             StringBuilder sb = new StringBuilder();
  14.             string winningSymbolsRegex = @"(\${6,10}|\@{6,10}|\#{6,10}|\^{6,10})";
  15.             List<string> input = Console.ReadLine().Trim().Split(new[] { ',', ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries).ToList();
  16.             for (int i = 0; i < input.Count; i++)
  17.             {
  18.                 string currentTicket = input[i];
  19.  
  20.                 if (currentTicket.Length != 20)
  21.                 {
  22.                     sb.AppendLine("invalid ticket");
  23.                     continue;
  24.                 }
  25.  
  26.                 else if (!Regex.IsMatch(currentTicket, winningSymbolsRegex))
  27.                 {
  28.                     sb.AppendLine($"ticket \"{currentTicket}\" - no match");
  29.                 }
  30.  
  31.                 else if (Regex.IsMatch(currentTicket, winningSymbolsRegex))
  32.                 {
  33.                     Match match = Regex.Match(currentTicket, winningSymbolsRegex);
  34.                     string leftHalf = string.Join("", currentTicket.Take(10));
  35.                     string rightHalf = string.Join("", currentTicket.Skip(10).Take(10));
  36.                     leftHalf = Regex.Match(leftHalf, winningSymbolsRegex).Value;
  37.                     rightHalf = Regex.Match(rightHalf, winningSymbolsRegex).Value;
  38.                     int leftSymbolCount = leftHalf.Count(x => x == match.Value[0]);
  39.                     int rightSymbolCount = rightHalf.Count(x => x == match.Value[0]);
  40.                     int min = Math.Min(leftSymbolCount, rightSymbolCount);
  41.  
  42.                     if (min == 10)
  43.                     {
  44.                         sb.AppendLine($"ticket \"{currentTicket}\" - {min}{match.Value[0]} Jackpot!");
  45.                     }
  46.                     else if (leftSymbolCount == 0 || rightSymbolCount == 0)
  47.                     {
  48.                         sb.AppendLine($"ticket \"{currentTicket}\" - no match");
  49.                     }
  50.                     else
  51.                     {
  52.                         sb.AppendLine($"ticket \"{currentTicket}\" - {min}{match.Value[0]}");
  53.                     }
  54.                 }
  55.             }
  56.             Console.WriteLine(sb.ToString());
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement