Advertisement
Guest User

Untitled

a guest
Dec 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <vector>
  4. #include <set>
  5. #include <map>
  6. #include <sstream>
  7. #include <string>
  8.  
  9. #include "subprocess.h"
  10. #include "copter.h"
  11. #include <unistd.h>
  12.  
  13. namespace
  14. {
  15. constexpr int MAX_COPTER_COUNT = 20;
  16. constexpr int TURN_LIMIT = 100;
  17.  
  18. auto make_set(const std::vector<CopterState> &copters)
  19. {
  20. int min_x = std::numeric_limits<int>::max();
  21. int min_y = std::numeric_limits<int>::max();
  22. for (const auto &c : copters) {
  23. min_x = std::min(min_x, c.x());
  24. min_y = std::min(min_y, c.y());
  25. }
  26. std::set<std::pair<int, int>> res;
  27. for (const auto &c : copters) {
  28. res.emplace(c.x() - min_x, c.y() - min_y);
  29. }
  30. return res;
  31. }
  32. }
  33.  
  34. using std::cin;
  35. using std::cout;
  36. using std::cerr;
  37. using std::endl;
  38. using std::vector;
  39. using std::set;
  40. using std::map;
  41. using std::pair;
  42. using std::ostringstream;
  43. using std::string;
  44.  
  45. int main(int argc, char *argv[])
  46. {
  47. /*
  48. if (argc != 2) {
  49. cerr << "wrong number of arguments" << endl;
  50. exit(1);
  51. }
  52. string copterfile(argv[1]);
  53. cerr << "copter: " << copterfile << endl;
  54. */
  55.  
  56. // read the copter count
  57. int copter_count = 0;
  58. cin >> copter_count;
  59. if (!cin || copter_count <= 0 || copter_count > MAX_COPTER_COUNT) {
  60. cerr << "invalid copter count" << endl;
  61. exit(1);
  62. }
  63. // read the original state (x, y)
  64. vector<CopterState> copters;
  65. for (int i = 0; i < copter_count; ++i) {
  66. int x = 0, y = 0;
  67. cin >> x >> y;
  68. if (!cin) {
  69. cerr << "failed to read initial copter position" << endl;
  70. exit(1);
  71. }
  72. copters.emplace_back(x, y);
  73. }
  74. // read the target state
  75. vector<CopterState> target;
  76. for (int i = 0; i < copter_count; ++i) {
  77. int x = 0, y = 0;
  78. cin >> x >> y;
  79. if (!cin) {
  80. cerr << "failed to read target copter position" << endl;
  81. exit(1);
  82. }
  83. target.emplace_back(x, y);
  84. }
  85. auto target_set = make_set(target);
  86.  
  87. // FIXME: the turn limit must be settable
  88. int turn_limit = TURN_LIMIT;
  89.  
  90. int current_turn = 0;
  91. int retval = 0;
  92.  
  93. // movements
  94. while (1) {
  95. ++current_turn;
  96. cerr << "Turn: " << current_turn << endl;
  97.  
  98. cerr << "States:" << endl;
  99. for (int i = 0; i < copter_count; ++i) {
  100. cerr << " " << i << ": " << copters[i].x() << " " << copters[i].y() << " ";
  101. copters[i].dump_mem(cerr);
  102. }
  103.  
  104. if (current_turn > turn_limit) {
  105. cerr << "turn limit of " << turn_limit << " exceeded" << endl;
  106. current_turn = -2;
  107. break;
  108. }
  109.  
  110. vector<CopterState> new_copters(copter_count);
  111. int failure_count = 0;
  112. for (int i = 0; i < copter_count; ++i) {
  113. cerr << " Copter: " << i << endl;
  114. const auto &c = copters[i];
  115. // prepare copter input
  116. ostringstream cfg;
  117. cfg << copter_count << endl;
  118. for (const auto &t : target) {
  119. cfg << t.x() << " " << t.y() << endl;
  120. }
  121. c.dump_mem(cfg);
  122. int neighbours = 0;
  123. for (int j = 0; j < copter_count; ++j) {
  124. if (i != j) {
  125. neighbours += c.neighbour_kind(copters[j]) != CopterState::Direction::AWAY;
  126. }
  127. }
  128. cfg << neighbours << endl;
  129. for (int j = 0; j < copter_count; ++j) {
  130. if (i != j) {
  131. const auto &other = copters[j];
  132. if (auto kind = c.neighbour_kind(other); kind != CopterState::Direction::AWAY) {
  133. cfg << int(kind) << " ";
  134. other.dump_mem(cfg);
  135. }
  136. }
  137. }
  138. string copter_input = cfg.str();
  139. //cerr << " Input: <" << copter_input << ">" << endl;
  140.  
  141. /*
  142. for (int j = 0; argv[j]; ++j) {
  143. cerr << "<" << argv[j] << ">" << endl;
  144. }
  145. */
  146. Subprocess prc;
  147. prc.set_cmd(argv + 1);
  148. //prc.set_cmd(copterfile);
  149. prc.set_input(std::move(copter_input));
  150. prc.set_max_cpu_time(1);
  151. bool success = prc.run_and_wait();
  152. if (!success) {
  153. cerr << "copter failed" << endl;
  154. cerr << " Errors: <" << prc.move_error() << ">" << endl;
  155. ++failure_count;
  156. continue;
  157. }
  158. string copter_output = prc.move_output();
  159. cerr << " Output: <" << copter_output << ">" << endl;
  160. cerr << " Errors: <" << prc.move_error() << ">" << endl;
  161. try {
  162. new_copters[i].parse_copter_output(c.x(), c.y(), copter_output);
  163. } catch (const std::exception &ex) {
  164. cerr << ex.what() << endl;
  165. ++failure_count;
  166. continue;
  167. }
  168. cerr << " New position: " << new_copters[i].x() << ", " << new_copters[i].y() << endl;
  169. }
  170.  
  171. if (failure_count > 0) {
  172. cerr << "Turn " << current_turn << " unsuccessful: " << failure_count << " copters crashed" << endl;
  173. current_turn = -1;
  174. break;
  175. }
  176.  
  177. copters.swap(new_copters);
  178. auto copter_set = make_set(copters);
  179. if (copter_set == target_set) {
  180. cerr << "target configuration achieved in " << current_turn << " turns" << endl;
  181. for (int i = 0; i < copter_count; ++i) {
  182. cerr << " " << i << ": " << copters[i].x() << " " << copters[i].y() << " ";
  183. copters[i].dump_mem(cerr);
  184. }
  185. break;
  186. }
  187.  
  188. cerr << "Turn " << current_turn << " ended" << endl;
  189. //sleep(1);
  190. }
  191.  
  192. cout << current_turn << endl;
  193.  
  194. return retval;
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement