fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using std::cout;
  6. using std::vector;
  7.  
  8. enum wires { invalid, white, red, black, orange, green };
  9.  
  10. struct s0; struct s1; struct s2; struct s3; struct s4; struct s5; struct sf; struct ss;
  11.  
  12. struct ctx {
  13.  
  14. ctx(vector<wires> const & v) : defused(false), input(v) {
  15. it = input.begin();
  16. run<s0>();
  17. }
  18.  
  19. wires next() {
  20. if ( it == input.end() )
  21. return invalid;
  22. return *it++;
  23. }
  24.  
  25. template<typename T>
  26. void run() {
  27. T().run(*this);
  28. }
  29.  
  30.  
  31. bool defused;
  32. vector<wires> input;
  33. vector<wires>::const_iterator it;
  34. };
  35.  
  36. struct defuse_check {
  37. defuse_check(vector<vector<wires>> const & v) {
  38. for(auto&&a:v)
  39. for(auto&&w:a)
  40. input.emplace_back(w);
  41. }
  42.  
  43. bool defusable() {
  44. std::sort(input.begin(), input.end());
  45. while ( std::next_permutation(input.begin(), input.end()) ) {
  46. if ( ctx(input).defused ) return true;
  47. }
  48. return false;
  49. }
  50.  
  51. vector<wires> input;
  52. };
  53.  
  54. struct s0 {
  55. void run(ctx& c) {
  56. switch(c.next()) {
  57. case white: c.run<s1>(); break;
  58. case red : c.run<s2>(); break;
  59. default: c.run<sf>();
  60. }
  61. }
  62. };
  63. struct s1 {
  64. void run(ctx& c) {
  65. switch(c.next()) {
  66. case white : c.run<s2>(); break;
  67. case orange: c.run<s3>(); break;
  68. default: c.run<sf>();
  69. }
  70. }
  71. };
  72. struct s2 {
  73. void run(ctx& c) {
  74. switch(c.next()) {
  75. case red : c.run<s0>(); break;
  76. case black: c.run<s3>(); break;
  77. default: c.run<sf>();
  78. }
  79. }
  80. };
  81. struct s3 {
  82. void run(ctx& c) {
  83. switch(c.next()) {
  84. case black : c.run<s3>(); break;
  85. case orange: c.run<s4>(); break;
  86. case green : c.run<s5>(); break;
  87. default: c.run<sf>();
  88. }
  89. }
  90. };
  91. struct s4 {
  92. void run(ctx& c) {
  93. if ( c.next() == green ) c.run<ss>();
  94. else c.run<sf>();
  95. }
  96. };
  97. struct s5 {
  98. void run(ctx& c) {
  99. if ( c.next() == orange ) c.run<ss>();
  100. else c.run<sf>();
  101. }
  102. };
  103. struct sf { void run(ctx& c) { c.defused = false; } };
  104. struct ss { void run(ctx& c) { c.defused = true ; } };
  105.  
  106. template<typename T>
  107. void print_defused(T && c) {
  108. cout << (c.defused ? "defused" : "boom") << "\n";
  109. }
  110.  
  111. template<typename T>
  112. void print_defusable(T && c) {
  113. cout << (c.defusable() ? "defusable" : "not defusable") << "\n";
  114. }
  115.  
  116. int main(int argc, char* argv[]) {
  117. // challenges
  118. print_defused(ctx( {white, white, red, red, red, white, white, black, green, orange} ));
  119. print_defused(ctx( {white, black, black, black, black, green, orange} ));
  120. print_defused(ctx( {black, green, green} ));
  121. print_defused(ctx( {red, red, white, orange, black, green} ));
  122.  
  123. // bonus 1
  124. print_defusable(defuse_check({{4, white}, {3, red}, {4, black}, {1, green}, {1, orange}}));
  125. print_defusable(defuse_check({{4, white}, {3, red}, {4, black}, {1, orange}}));
  126.  
  127. // bonus 1 challenge
  128. print_defusable(defuse_check({{3, white}, {1, red}, {48, black}, {1, green}, {2, orange}}));
  129. print_defusable(defuse_check({{3, white}, {1, red}, {48, black}, {1, green}, {1, orange}}));
  130. }
Success #stdin #stdout 0.74s 3484KB
stdin
Standard input is empty
stdout
defused
boom
boom
boom
defusable
not defusable
defusable
defusable