Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _04_Winning_ticket
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- static void Main()
- {
- string[] tickets = Console.ReadLine()
- .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
- char[] winningSymbols = new char[] { '@', '^', '$', '#' };
- foreach (var ticket in tickets)
- {
- int winPrice = 0;
- bool isTicketValid = false;
- var winChar = ' ';
- if (ticket.Length == 20)
- {
- isTicketValid = true;
- bool ticketContainsWinningChar = false;
- var mostCommonChar = GetMostCommonChar(ticket, winningSymbols);
- if (winningSymbols.Contains(mostCommonChar))
- ticketContainsWinningChar = true;
- if (ticketContainsWinningChar)
- {
- int leftCount = 0;
- int rightCount = 0;
- int bestLeftCount = 0;
- int bestRightCount = 0;
- //Check left char count.
- for (int i = 0; i < ticket.Length / 2; i++)
- {
- var currentChar = ticket[i];
- if (currentChar == mostCommonChar)
- {
- leftCount++;
- if (leftCount > bestLeftCount)
- {
- bestLeftCount = leftCount;
- }
- else
- {
- leftCount = 0;
- }
- }
- }
- //Check right char count.
- for (int i = ticket.Length / 2; i < ticket.Length; i++)
- {
- var currentChar = ticket[i];
- if (currentChar == mostCommonChar)
- {
- rightCount++;
- if (rightCount > bestRightCount)
- {
- bestRightCount = rightCount;
- }
- else
- {
- rightCount = 0;
- }
- }
- }
- // If left && right char count uninterruptedly repeat
- // for at least 6 times in both sides:
- // ->win char count is math.min of both sides count
- if (bestRightCount >= 6 && 6 <= bestLeftCount)
- {
- winPrice = Math.Min(bestRightCount, bestLeftCount); //potential problem
- winChar = mostCommonChar;
- }
- }
- if (isTicketValid == true)
- {
- bool isJackpotHit = false;
- if (winPrice == 10)
- {
- isJackpotHit = true;
- }
- if (isJackpotHit == false && winPrice != 0)
- {
- Console.WriteLine($"ticket \"{ticket}\" - {winPrice}{winChar}");
- }
- else if (isJackpotHit == true && winPrice != 0)
- {
- Console.WriteLine($"ticket \"{ticket}\" - {winPrice}{winChar} Jackpot!");
- }
- else
- {
- Console.WriteLine($"ticket \"{ticket}\" - no match");
- }
- }
- }
- else
- {
- Console.WriteLine("invalid ticket");
- }
- }
- }
- static char GetMostCommonChar(string ticket, char[] winningSymbols)
- {
- int currentCount = 0;
- int bestCount = 0;
- char winningChar = '/'; //placeholder.
- for (int i = 0; i < ticket.Length; i++)
- {
- var currentChar = ticket[i];
- if (winningSymbols.Contains(currentChar))
- {
- currentCount++;
- if (currentCount > bestCount)
- {
- bestCount = currentCount;
- winningChar = currentChar;
- }
- }
- else
- {
- currentCount = 0;
- }
- }
- return winningChar;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment