Advertisement
Guest User

Untitled

a guest
Oct 25th, 2017
1,665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace _04_Winning_Ticket
  5. {
  6. public class WinningTicket
  7. {
  8. public static void Main()
  9. {
  10. var tickets = Console.ReadLine().Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).ToArray();
  11.  
  12. foreach (var ticket in tickets)
  13. {
  14. if (ticket.Length != 20)
  15. {
  16. Console.WriteLine("invalid ticket");
  17. }
  18. else
  19. {
  20. var leftSide = new string(ticket.Take(10).ToArray());
  21. var rightSide = new string(ticket.Skip(10).Take(10).ToArray());
  22.  
  23. var leftSeq = SideSeqCount(leftSide);
  24. var rightSeq = SideSeqCount(rightSide);
  25.  
  26. var outputChar = SideCharCount(leftSide);
  27.  
  28. int repeatedSeq = Math.Min(leftSeq, rightSeq);
  29.  
  30. if (repeatedSeq == 10)
  31. {
  32. Console.WriteLine($"ticket \"{ticket}\" - {repeatedSeq}{outputChar} Jackpot!");
  33. }
  34. else if (repeatedSeq >= 6 && repeatedSeq <= 9)
  35. {
  36. Console.WriteLine($"ticket \"{ticket}\" - {repeatedSeq}{outputChar}");
  37. }
  38. else
  39. {
  40. Console.WriteLine($"ticket \"{ticket}\" - no match");
  41. }
  42. }
  43. }
  44. }
  45.  
  46. static char SideCharCount(string side)
  47. {
  48. int cnt = 1;
  49. int bestCnt = int.MinValue;
  50.  
  51. char currChar = char.MinValue;
  52. char bestChar = char.MinValue;
  53.  
  54. for (int i = 0; i < side.Length - 1; i++)
  55. {
  56. char current = side[i];
  57. char next = side[i + 1];
  58.  
  59. if (current == '#' || current == '@' || current == '$' || current == '^')
  60. {
  61. if (current == next)
  62. {
  63. cnt++;
  64. currChar = side[i];
  65. }
  66. else
  67. {
  68. if (cnt > bestCnt)
  69. {
  70. bestCnt = cnt;
  71. bestChar = currChar;
  72. cnt = 1;
  73. }
  74. }
  75. }
  76. }
  77. if (cnt > bestCnt)
  78. {
  79. bestCnt = cnt;
  80. bestChar = currChar;
  81. cnt = 1;
  82. }
  83. return bestChar;
  84. }
  85.  
  86. static int SideSeqCount(string side)
  87. {
  88. int cnt = 1;
  89. int bestCnt = int.MinValue;
  90.  
  91. for (int i = 0; i < side.Length - 1; i++)
  92. {
  93. char current = side[i];
  94. char next = side[i + 1];
  95.  
  96. if (current == '#' || current == '@' || current == '$' || current == '^')
  97. {
  98. if (current == next)
  99. {
  100. cnt++;
  101. }
  102. else
  103. {
  104. if (cnt > bestCnt)
  105. {
  106. bestCnt = cnt;
  107. cnt = 1;
  108. }
  109. }
  110. }
  111. }
  112. if (cnt > bestCnt)
  113. {
  114. bestCnt = cnt;
  115. cnt = 1;
  116. }
  117. return bestCnt;
  118. }
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement