fork(4) download
  1. // Test.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include <iostream>
  5. #include <vector>
  6. #include <algorithm>
  7. using namespace std;
  8.  
  9. enum TroopType
  10. {
  11. TYPE_INVALID,
  12. TYPE_BARBARIN,
  13. TYPE_ARCHER,
  14. TYPE_GIANT,
  15. TYPE_GOBLIN,
  16. TYPE_WALLBREAKER,
  17. TYPE_BALLOON,
  18. TYPE_WIZARD,
  19. TYPE_HEALER,
  20. TYPE_DRAGON,
  21. TYPE_PEKKA,
  22. };
  23.  
  24. int GetTroopCapacity(TroopType t)
  25. {
  26. switch (t)
  27. {
  28. case TYPE_BARBARIN: return 1; break;
  29. case TYPE_ARCHER: return 1; break;
  30. case TYPE_GIANT: return 5; break;
  31. case TYPE_GOBLIN: return 1; break;
  32. case TYPE_WALLBREAKER: return 2; break;
  33. case TYPE_BALLOON: return 5; break;
  34. case TYPE_WIZARD: return 4; break;
  35. case TYPE_HEALER: return 14; break;
  36. case TYPE_DRAGON: return 20; break;
  37. case TYPE_PEKKA: return 25; break;
  38. }
  39.  
  40. return -1;
  41. };
  42.  
  43. int GetTroopBuildTimeSec(TroopType t)
  44. {
  45. switch (t)
  46. {
  47. case TYPE_BARBARIN: return 20; break;
  48. case TYPE_ARCHER: return 25; break;
  49. case TYPE_GIANT: return 120; break;
  50. case TYPE_GOBLIN: return 30; break;
  51. case TYPE_WALLBREAKER: return 120; break;
  52. case TYPE_BALLOON: return 480; break;
  53. case TYPE_WIZARD: return 480; break;
  54. case TYPE_HEALER: return 900; break;
  55. case TYPE_DRAGON: return 1800; break;
  56. case TYPE_PEKKA: return 2700; break;
  57. }
  58.  
  59. return -1;
  60. };
  61.  
  62. string GetTroopName(TroopType t)
  63. {
  64. switch (t)
  65. {
  66. case TYPE_BARBARIN: return "Barbarian"; break;
  67. case TYPE_ARCHER: return "Archer"; break;
  68. case TYPE_GIANT: return "Giant"; break;
  69. case TYPE_GOBLIN: return "Healer"; break;
  70. case TYPE_WALLBREAKER: return "Wall Breaker"; break;
  71. case TYPE_BALLOON: return "Balloon"; break;
  72. case TYPE_WIZARD: return "Wizard"; break;
  73. case TYPE_HEALER: return "Healer"; break;
  74. case TYPE_DRAGON: return "Dragon"; break;
  75. case TYPE_PEKKA: return "Pekka"; break;
  76. }
  77.  
  78. return "";
  79. };
  80.  
  81. struct Troop
  82. {
  83. TroopType type;
  84. int quantity;
  85.  
  86. Troop(TroopType t, int qty)
  87. {
  88. type = t;
  89. quantity = qty;
  90. }
  91.  
  92. Troop()
  93. {
  94. type = TYPE_INVALID;
  95. quantity = 0;
  96. }
  97. };
  98.  
  99. struct Barracks
  100. {
  101. int currentQueueTimeSec;
  102. int currentCapacity;
  103. int totalCapacity;
  104. vector<Troop> queuedTroops;
  105.  
  106. Barracks(int capacity)
  107. {
  108. currentQueueTimeSec = 0;
  109. currentCapacity = 0;
  110. totalCapacity = capacity;
  111. }
  112.  
  113. Barracks()
  114. {
  115. currentQueueTimeSec = 0;
  116. currentCapacity = 0;
  117. totalCapacity = 0;
  118. }
  119.  
  120. int AvailableSpace()
  121. {
  122. return (totalCapacity - currentCapacity);
  123. }
  124.  
  125. void Queue(TroopType t)
  126. {
  127. if (AvailableSpace() >= GetTroopCapacity(t))
  128. {
  129. bool exists = false;
  130. for (int i = 0; i < queuedTroops.size(); i++)
  131. {
  132. if (t == queuedTroops[i].type)
  133. {
  134. queuedTroops[i].quantity++;
  135. exists = true;
  136. }
  137. }
  138.  
  139. if (!exists)
  140. {
  141. queuedTroops.push_back(Troop(t, 1));
  142. }
  143.  
  144. currentCapacity += GetTroopCapacity(t);
  145. currentQueueTimeSec += GetTroopBuildTimeSec(t);
  146. }
  147. }
  148. };
  149.  
  150. vector<Barracks> ActiveBarracks;
  151.  
  152. vector<Troop> ArmyCompList;
  153.  
  154. //Tweak the variables here to see the example.
  155. void ExampleSetup()
  156. {
  157. //Establish the number of barracks and their capacities
  158. ActiveBarracks.push_back(Barracks(75));
  159. ActiveBarracks.push_back(Barracks(75));
  160. ActiveBarracks.push_back(Barracks(75));
  161. ActiveBarracks.push_back(Barracks(75));
  162.  
  163. ArmyCompList.push_back(Troop(TYPE_ARCHER, 20));
  164. ArmyCompList.push_back(Troop(TYPE_HEALER, 3));
  165. ArmyCompList.push_back(Troop(TYPE_GIANT , 14));
  166. ArmyCompList.push_back(Troop(TYPE_WIZARD, 23));
  167. ArmyCompList.push_back(Troop(TYPE_WALLBREAKER, 8));
  168. ArmyCompList.push_back(Troop(TYPE_DRAGON, 1));
  169.  
  170. //ArmyCompList.push_back(Troop(TYPE_PEKKA, 2));
  171. //ArmyCompList.push_back(Troop(TYPE_DRAGON, 1));
  172. //ArmyCompList.push_back(Troop(TYPE_BALLOON, 5));
  173. //ArmyCompList.push_back(Troop(TYPE_GIANT, 5));
  174. //ArmyCompList.push_back(Troop(TYPE_WIZARD, 5));
  175. //ArmyCompList.push_back(Troop(TYPE_WALLBREAKER, 8));
  176. //ArmyCompList.push_back(Troop(TYPE_ARCHER, 20));
  177. }
  178.  
  179. //Checks if the test about to be performed is valid. you can not queue up more troops than what you have the capacity for
  180. bool ValidSetup()
  181. {
  182. int totalCapacity = 0;
  183. for (int i = 0; i < ActiveBarracks.size(); i++)
  184. {
  185. totalCapacity += ActiveBarracks[i].totalCapacity;
  186. }
  187.  
  188. int totalQueue = 0;
  189. for (int i = 0; i < ArmyCompList.size(); i++)
  190. {
  191. totalQueue += ArmyCompList[i].quantity * GetTroopCapacity(ArmyCompList[i].type);
  192. }
  193.  
  194. return totalQueue <= totalCapacity;
  195. }
  196.  
  197. int main()
  198. {
  199. //setup the example run
  200. ExampleSetup();
  201.  
  202. //Check conditions of test
  203. if (!ValidSetup())
  204. {
  205. printf("ERROR: You've queued up more than possible!\n");
  206. return 1;
  207. }
  208.  
  209. //Sort based off of time. Longest to shortest. Uses the STL sort algorithm with the lambda function to compare build times.
  210. std::sort(ArmyCompList.begin(), ArmyCompList.end(), [](const Troop &lhs, const Troop &rhs)
  211. {
  212. return GetTroopBuildTimeSec(lhs.type) > GetTroopBuildTimeSec(rhs.type);
  213. });
  214.  
  215. //perform the queing algorithm
  216. while (ArmyCompList.size() > 0)
  217. {
  218. //Get the first troop!
  219. int curTroopCapacity = GetTroopCapacity(ArmyCompList[0].type);
  220.  
  221. //Loop through the barracks, find the lowest capacity barrack right now
  222. int barrackIdx = -1; //save off the index for use later
  223. int leastTime = 99999; //start the time off with a very high number for the test
  224. for (int i = 0; i < ActiveBarracks.size(); i++)
  225. {
  226. int time = ActiveBarracks[i].currentQueueTimeSec;
  227. int space = ActiveBarracks[i].AvailableSpace();
  228.  
  229. //Check if this barrack has the smallest current build time AND that it has the capacity to queue the troop in question
  230. if (time < leastTime && space >= curTroopCapacity)
  231. {
  232. barrackIdx = i;
  233. leastTime = time;
  234. }
  235. }
  236.  
  237. //Something went wrong if this was hit... it should not have been hit assuming the ValidSetup() function call was performed correctly
  238. if (barrackIdx == -1)
  239. {
  240. printf("Error occured, whups!\n");
  241. return 0;
  242. }
  243.  
  244. //Queue 1 troop into the best barrack
  245. ActiveBarracks[barrackIdx].Queue(ArmyCompList[0].type);
  246. ArmyCompList[0].quantity--;
  247.  
  248. //if the remaining quantity is 0, then remove it from the composition list
  249. if (ArmyCompList[0].quantity <= 0)
  250. {
  251. ArmyCompList.erase(ArmyCompList.begin());
  252. }
  253. }
  254.  
  255.  
  256. //Print the results to the console
  257. for (int i = 0; i < ActiveBarracks.size(); i++)
  258. {
  259. printf("-----------\n");
  260. printf("Barrack #%d - Build Time: %d, Capacity: %d\n", i, ActiveBarracks[i].currentQueueTimeSec, ActiveBarracks[i].currentCapacity);
  261. for (int j = 0; j < ActiveBarracks[i].queuedTroops.size(); j++)
  262. {
  263. string name = GetTroopName(ActiveBarracks[i].queuedTroops[j].type);
  264. int qty = ActiveBarracks[i].queuedTroops[j].quantity;
  265. printf("\tTroop %s, qty %d\n", name.c_str(), qty);
  266. }
  267. printf("\n");
  268. }
  269.  
  270. //Exit successfully
  271. return 0;
  272. }
Success #stdin #stdout 0s 3236KB
stdin
Standard input is empty
stdout
-----------
Barrack #0 - Build Time: 4660, Capacity: 53
	Troop Dragon, qty 1
	Troop Wizard, qty 5
	Troop Giant, qty 1
	Troop Wall Breaker, qty 2
	Troop Archer, qty 4

-----------
Barrack #1 - Build Time: 4670, Capacity: 69
	Troop Healer, qty 1
	Troop Wizard, qty 6
	Troop Giant, qty 5
	Troop Wall Breaker, qty 2
	Troop Archer, qty 2

-----------
Barrack #2 - Build Time: 4675, Capacity: 69
	Troop Healer, qty 1
	Troop Wizard, qty 6
	Troop Giant, qty 4
	Troop Wall Breaker, qty 2
	Troop Archer, qty 7

-----------
Barrack #3 - Build Time: 4675, Capacity: 69
	Troop Healer, qty 1
	Troop Wizard, qty 6
	Troop Giant, qty 4
	Troop Wall Breaker, qty 2
	Troop Archer, qty 7