fork download
  1. #include "stdio.h"
  2. #include "stdbool.h"
  3.  
  4. #define CIRCLES_COUNT 4
  5. #define CIRCLE_CAPACITY 12
  6.  
  7. static inline void rotate_right(int c[CIRCLE_CAPACITY]) {
  8. int last = c[CIRCLE_CAPACITY-1];
  9. for (int i = CIRCLE_CAPACITY-1; i > 0; i--)
  10. c[i] = c[i-1];
  11. c[0] = last;
  12. }
  13.  
  14. void print_circle(const int c[CIRCLE_CAPACITY]) {
  15. for (int i = 0; i < CIRCLE_CAPACITY; i++)
  16. printf("%d ", c[i]);
  17. printf("\n");
  18. }
  19.  
  20. void print_circles(const int cc[CIRCLES_COUNT][CIRCLE_CAPACITY]) {
  21. for (int row = 0; row < CIRCLES_COUNT; row++) {
  22. print_circle(cc[row]);
  23. }
  24. }
  25.  
  26. bool all_equal(const int cc[CIRCLES_COUNT][CIRCLE_CAPACITY]) {
  27. bool first_column = true;
  28. int prev_sum;
  29. for (int col = 0; col < CIRCLE_CAPACITY; col++) {
  30. int sum = 0;
  31. for (int i = 0; i < CIRCLES_COUNT; i++) {
  32. sum += cc[i][col];
  33. }
  34.  
  35. if (!first_column && sum != prev_sum)
  36. return false;
  37. prev_sum = sum;
  38.  
  39. first_column = false;
  40. }
  41. return true;
  42. }
  43.  
  44. void main() {
  45. int circles[CIRCLES_COUNT][CIRCLE_CAPACITY] = {
  46. {3, 9, 6, 4, 3, 7, 5, 2, 4, 8, 3, 6},
  47. {8, 4, 7, 5, 8, 2, 9, 5, 5, 8, 4, 6},
  48. {6, 5, 8, 1, 6, 6, 7, 1, 3, 7, 1, 9},
  49. {9, 2, 4, 6, 8, 4, 3, 8, 5, 2, 3, 7}
  50. };
  51.  
  52. int max_rotations = CIRCLE_CAPACITY;
  53. for (int i = 1; i < CIRCLES_COUNT; i++)
  54. max_rotations *= CIRCLE_CAPACITY;
  55.  
  56. for (int rot = 0; ; rot++) {
  57. if (rot == max_rotations) {
  58. printf("No solutions\n");
  59. break;
  60. }
  61.  
  62. if (!(all_equal(circles))) {
  63. rotate_right(circles[(rot / CIRCLE_CAPACITY) % CIRCLES_COUNT]);
  64. continue;
  65. }
  66.  
  67. print_circles(circles);
  68. break;
  69. }
  70. }
Runtime error #stdin #stdout 0s 2168KB
stdin
Standard input is empty
stdout
No solutions