Advertisement
Prohause

Kamino Factory

Mar 8th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem02
  6. {
  7. public class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var dnaLengh = int.Parse(Console.ReadLine());
  12. var allDna = new List<Dna>();
  13.  
  14. string input;
  15. var counter = 1;
  16.  
  17. while (!(input = Console.ReadLine()).Equals("Clone them!"))
  18. {
  19. var tokens = input.Split(new[] { '!' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToList();
  20. if (tokens.Count != dnaLengh)
  21. {
  22. continue;
  23. }
  24.  
  25. if (tokens.All(p => p != 1))
  26. {
  27.  
  28. var currentDna1 = new Dna
  29. {
  30. DnaSequence = new List<int>(tokens),
  31. LongestSequence = new KeyValuePair<int, int>(0,-1),
  32. Sum = tokens.Sum(),
  33. InputIndex = counter
  34.  
  35. };
  36. allDna.Add(currentDna1);
  37. counter++;
  38. continue;
  39. }
  40.  
  41. var currentDna = new Dna
  42. {
  43. DnaSequence = new List<int>(tokens),
  44. LongestSequence = CalculateLongestSequence(tokens),
  45. Sum = tokens.Sum(),
  46. InputIndex = counter
  47. };
  48.  
  49. if (currentDna.LongestSequence.Value >= 0)
  50. {
  51. allDna.Add(currentDna);
  52. }
  53.  
  54. counter++;
  55. }
  56.  
  57. allDna = allDna.OrderByDescending(p => p.LongestSequence.Key).ThenBy(p => p.LongestSequence.Value)
  58. .ThenByDescending(p => p.Sum).ToList();
  59.  
  60. if (allDna.Any())
  61. {
  62. var output = allDna.First();
  63. Console.WriteLine($"Best DNA sample {output.InputIndex} with sum: {output.Sum}.{Environment.NewLine}{string.Join(" ", output.DnaSequence)}");
  64.  
  65. }
  66.  
  67. }
  68.  
  69. private static KeyValuePair<int, int> CalculateLongestSequence(List<int> tokens)
  70. {
  71. var size = 0;
  72. var currentSize = 0;
  73. var startIndex = -1;
  74.  
  75. for (int i = 0; i < tokens.Count; i++)
  76. {
  77. if (tokens[i] == 1)
  78. {
  79. currentSize++;
  80. }
  81. else
  82. {
  83. if (currentSize > size)
  84. {
  85. size = currentSize;
  86. startIndex = i - currentSize;
  87.  
  88. }
  89.  
  90. currentSize = 0;
  91. }
  92. }
  93.  
  94. if (currentSize>size)
  95. {
  96. size = currentSize;
  97. startIndex = tokens.Count - size;
  98. }
  99. return new KeyValuePair<int, int>(size, startIndex);
  100. }
  101. }
  102.  
  103. public class Dna
  104.  
  105. {
  106. public List<int> DnaSequence { get; set; }
  107. public KeyValuePair<int, int> LongestSequence { get; set; }
  108. public int Sum { get; set; }
  109. public int InputIndex { get; set; }
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement