VasilKotsev

Exam prep 1 | 04. Winning ticket

Jun 25th, 2018
1,852
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.13 KB | None | 0 0
  1. namespace _04_Winning_ticket
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.     using System.Threading.Tasks;
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             string[] tickets = Console.ReadLine()
  13.                 .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
  14.  
  15.             char[] winningSymbols = new char[] { '@', '^', '$', '#' };
  16.  
  17.             foreach (var ticket in tickets)
  18.             {
  19.                 int winPrice = 0;
  20.                 bool isTicketValid = false;
  21.                 var winChar = ' ';
  22.                 if (ticket.Length == 20)
  23.                 {
  24.                     isTicketValid = true;
  25.                     bool ticketContainsWinningChar = false;
  26.                     var mostCommonChar = GetMostCommonChar(ticket, winningSymbols);
  27.  
  28.                     if (winningSymbols.Contains(mostCommonChar))
  29.                         ticketContainsWinningChar = true;
  30.  
  31.                     if (ticketContainsWinningChar)
  32.                     {
  33.                         int leftCount = 0;
  34.                         int rightCount = 0;
  35.                         int bestLeftCount = 0;
  36.                         int bestRightCount = 0;
  37.                         //Check left char count.
  38.                         for (int i = 0; i < ticket.Length / 2; i++)
  39.                         {
  40.                             var currentChar = ticket[i];
  41.                             if (currentChar == mostCommonChar)
  42.                             {
  43.                                 leftCount++;
  44.                                 if (leftCount > bestLeftCount)
  45.                                 {
  46.                                     bestLeftCount = leftCount;
  47.                                 }
  48.                                 else
  49.                                 {
  50.                                     leftCount = 0;
  51.                                 }
  52.                             }
  53.                         }
  54.                         //Check right char count.
  55.                         for (int i = ticket.Length / 2; i < ticket.Length; i++)
  56.                         {
  57.                             var currentChar = ticket[i];
  58.                             if (currentChar == mostCommonChar)
  59.                             {
  60.                                 rightCount++;
  61.                                 if (rightCount > bestRightCount)
  62.                                 {
  63.                                     bestRightCount = rightCount;
  64.                                 }
  65.                                 else
  66.                                 {
  67.                                     rightCount = 0;
  68.                                 }
  69.                             }
  70.                         }
  71.  
  72.                         // If left && right char count uninterruptedly repeat
  73.                         // for at least 6 times in both sides:
  74.                         // ->win char count is math.min of both sides count
  75.                         if (bestRightCount >= 6 && 6 <= bestLeftCount)
  76.                         {
  77.                             winPrice = Math.Min(bestRightCount, bestLeftCount); //potential problem
  78.                             winChar = mostCommonChar;
  79.                         }
  80.                     }
  81.  
  82.                     if (isTicketValid == true)
  83.                     {
  84.                         bool isJackpotHit = false;
  85.                         if (winPrice == 10)
  86.                         {
  87.                             isJackpotHit = true;
  88.                         }
  89.  
  90.                         if (isJackpotHit == false && winPrice != 0)
  91.                         {
  92.                             Console.WriteLine($"ticket \"{ticket}\" - {winPrice}{winChar}");
  93.                         }
  94.  
  95.                         else if (isJackpotHit == true && winPrice != 0)
  96.                         {
  97.                             Console.WriteLine($"ticket \"{ticket}\" - {winPrice}{winChar} Jackpot!");
  98.                         }
  99.  
  100.                         else
  101.                         {
  102.                             Console.WriteLine($"ticket \"{ticket}\" - no match");
  103.                         }
  104.                     }
  105.                 }
  106.  
  107.                 else
  108.                 {
  109.                     Console.WriteLine("invalid ticket");
  110.                 }
  111.             }
  112.         }
  113.  
  114.         static char GetMostCommonChar(string ticket, char[] winningSymbols)
  115.         {  
  116.             int currentCount = 0;
  117.             int bestCount = 0;
  118.             char winningChar = '/'; //placeholder.
  119.  
  120.             for (int i = 0; i < ticket.Length; i++)
  121.             {
  122.                 var currentChar = ticket[i];
  123.                 if (winningSymbols.Contains(currentChar))
  124.                 {
  125.                     currentCount++;
  126.                     if (currentCount > bestCount)
  127.                     {
  128.                         bestCount = currentCount;
  129.                         winningChar = currentChar;
  130.                     }
  131.                 }
  132.  
  133.                 else
  134.                 {
  135.                     currentCount = 0;
  136.                 }
  137.             }
  138.  
  139.             return winningChar;
  140.         }
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment