Advertisement
Guest User

Untitled

a guest
Feb 20th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Hashcode
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. StringBuilder sb = new StringBuilder();
  13. var booksIds = new List<int>();
  14.  
  15. //First id of the library second list is the number of books and the time for shiping
  16. var librariesInformation = new Dictionary<int, List<int>>();
  17.  
  18. var librariesBooks = new Dictionary<int, List<int>>();
  19.  
  20. var librariesScanTimeLeft = new Dictionary<int, int>();
  21. var librariesResults = new Dictionary<int, List<int>>();
  22.  
  23. string text = System.IO.File.ReadAllText(@"C:\Users\Ilia\source\repos\Hashcode\b_read_on.txt");
  24.  
  25. string[] inputs = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  26.  
  27. string[] firstLine = inputs[0].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  28.  
  29. int numberOfBooks = int.Parse(firstLine[0]);
  30. int numberOfLibraries = int.Parse(firstLine[1]);
  31. int daysToScan = int.Parse(firstLine[2]);
  32.  
  33. string[] secondLine = inputs[1].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  34.  
  35. foreach (var item in secondLine)
  36. {
  37. booksIds.Add(int.Parse(item));
  38. }
  39.  
  40. for (int i = 2; i < inputs.Length; i++)
  41. {
  42. int currentLibraryId = i - 2;
  43.  
  44. string[] libraryInfo = inputs[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToArray();
  45.  
  46.  
  47.  
  48. if (i % 2 == 0)
  49. {
  50. if (currentLibraryId > 1)
  51. {
  52. currentLibraryId -= 1;
  53. }
  54.  
  55. int numberOfBooksForCurrentLibrary = int.Parse(libraryInfo[0]);
  56. int sighupProcesDuration = int.Parse(libraryInfo[1]);
  57. int canShipNumberOfBooks = int.Parse(libraryInfo[2]);
  58.  
  59. librariesInformation.Add(currentLibraryId, new List<int>());
  60.  
  61. librariesInformation[currentLibraryId].Add(numberOfBooksForCurrentLibrary);
  62. librariesInformation[currentLibraryId].Add(sighupProcesDuration);
  63. librariesInformation[currentLibraryId].Add(canShipNumberOfBooks);
  64.  
  65. }
  66. else
  67. {
  68. if (currentLibraryId > 1)
  69. {
  70. currentLibraryId -= 1;
  71. }
  72.  
  73. librariesBooks.Add(currentLibraryId - 1, new List<int>());
  74.  
  75. foreach (var item in libraryInfo)
  76. {
  77. int index = int.Parse(item);
  78. if (librariesBooks[currentLibraryId - 1].Any(x => x == index))
  79. {
  80. continue;
  81. }
  82. librariesBooks[currentLibraryId - 1].Add(index);
  83. }
  84. }
  85.  
  86.  
  87. }
  88.  
  89. librariesInformation = librariesInformation.OrderByDescending(x => x.Value[1]).ToDictionary(k => k.Key, k => k.Value);
  90.  
  91.  
  92. int tempDaysToScan = daysToScan -= librariesInformation[librariesInformation.FirstOrDefault().Key][1];
  93.  
  94. librariesScanTimeLeft.Add(librariesInformation.FirstOrDefault().Key, tempDaysToScan);
  95.  
  96.  
  97. foreach (var item in librariesInformation)
  98. {
  99. if (tempDaysToScan - item.Value[1] < 0)
  100. {
  101. break;
  102. }
  103.  
  104. if (librariesScanTimeLeft.ContainsKey(item.Key))
  105. {
  106. continue;
  107. }
  108.  
  109. tempDaysToScan -= item.Value[1];
  110. librariesScanTimeLeft.Add(item.Key, tempDaysToScan);
  111. }
  112.  
  113. foreach (var item in librariesScanTimeLeft)
  114. {
  115. int howManyBooksCanBeScan = item.Value * librariesInformation[item.Key][2];
  116.  
  117. librariesResults.Add(item.Key, new List<int>());
  118.  
  119. foreach (var book in librariesBooks[item.Key])
  120. {
  121. if (howManyBooksCanBeScan <= 0)
  122. {
  123. break;
  124. }
  125. librariesResults[item.Key].Add(book);
  126. }
  127. }
  128.  
  129.  
  130. sb.Append(librariesResults.Count + "\r\n");
  131.  
  132.  
  133. foreach (var item in librariesResults)
  134. {
  135. sb.Append(item.Key + " " + item.Value.Count + "\r\n");
  136.  
  137. foreach (var book in item.Value)
  138. {
  139. sb.Append(book + " ");
  140. }
  141.  
  142. sb.Append("\r\n");
  143. }
  144.  
  145. //foreach (var key in librariesInformation)
  146. //{
  147.  
  148. // Console.WriteLine("Library id: " + key.Key);
  149. // foreach (var item in key.Value)
  150. // {
  151. // Console.Write(item);
  152. // }
  153. // Console.WriteLine();
  154. // Console.WriteLine("Books: ");
  155. // foreach (var value in librariesBooks[key.Key])
  156. // {
  157.  
  158. // Console.Write(value);
  159.  
  160. // }
  161.  
  162. // Console.WriteLine();
  163. //}
  164.  
  165.  
  166.  
  167. System.IO.File.WriteAllText(@"C:\Users\Ilia\source\repos\Hashcode\Hashcode\ResultText.txt", sb.ToString());
  168.  
  169.  
  170. }
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement