_PoY

<<Crystal>> Underground Switches

Nov 15th, 2020 (edited)
1,468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.35 KB | None | 0 0
  1. // 3 2 1 is optimal, this program proves it.
  2.  
  3. #include <stdio.h>
  4.  
  5. int MASK_4  = 1 << 4;
  6. int MASK_5  = 1 << 5;
  7. int MASK_6  = 1 << 6;
  8. int MASK_7  = 1 << 7;
  9. int MASK_8  = 1 << 8;
  10. int MASK_9  = 1 << 9;
  11. int MASK_10 = 1 << 10;
  12. int MASK_11 = 1 << 11;
  13. int MASK_12 = 1 << 12;
  14. int MASK_13 = 1 << 13;
  15. int MASK_14 = 1 << 14;
  16.  
  17. void printBits(size_t const size, void const * const ptr)
  18. {
  19.     unsigned char *b = (unsigned char*) ptr;
  20.     unsigned char byte;
  21.     int i, j;
  22.    
  23.     for (i = size-1; i >= 0; i--) {
  24.         for (j = 7; j >= 0; j--) {
  25.             byte = (b[i] >> j) & 1;
  26.             printf("%u", byte);
  27.         }
  28.     }
  29.     puts("");
  30. }
  31.  
  32. int isDoorsMatching(int doors, int mask){
  33.    return (doors&mask) == mask;
  34. }
  35.  
  36. int isDoorsInPaths(int doors, int* paths, int size){
  37.     int isIn = 1;
  38.    
  39.     for(int i = 0; i < size; i++){
  40.         if(isDoorsMatching(doors, paths[i]))
  41.             return isIn;
  42.     }
  43.    
  44.     return !isIn;
  45. }
  46.  
  47. int updateCombination(int oldCombination, int score){
  48.     int clearAll = 0b000000000000000;
  49.    
  50.     int newCombination = oldCombination;
  51.     switch(score){
  52.         case 0:
  53.             newCombination &= clearAll;
  54.             break;
  55.         case 1:
  56.             newCombination |= (MASK_4|MASK_10|MASK_13);
  57.             newCombination &= ((~(MASK_9|MASK_11|MASK_12|MASK_14))&0xFFFF);
  58.             break;
  59.         case 2:
  60.             newCombination |= (MASK_5|MASK_11|MASK_12);
  61.             newCombination &= ((~(MASK_8|MASK_10|MASK_13|MASK_14))&0xFFFF);
  62.             break;
  63.         case 3:
  64.             newCombination |= (MASK_6|MASK_10|MASK_13);
  65.             newCombination &= ((~(MASK_7|MASK_11|MASK_12|MASK_14))&0xFFFF);
  66.             break;
  67.         case 4:
  68.             newCombination |= (MASK_7|MASK_11|MASK_12);
  69.             newCombination &= ((~(MASK_6|MASK_10|MASK_13|MASK_14))&0xFFFF);
  70.             break;
  71.         case 5:
  72.             newCombination |= (MASK_8|MASK_10|MASK_13);
  73.             newCombination &= ((~(MASK_5|MASK_11|MASK_12|MASK_14))&0xFFFF);
  74.             break;
  75.         default: //6
  76.             newCombination |= (MASK_9|MASK_11|MASK_12|MASK_14);
  77.             newCombination &= ((~(MASK_4|MASK_10|MASK_13))&0xFFFF);
  78.             break;
  79.     }
  80.    
  81.     return newCombination;
  82. }
  83.  
  84. int calculateCombination(int* doors, int doorsNb){
  85.     int isZeroEncountered = 0;
  86.     int combination = 0;
  87.     int score = 0;
  88.     int switches[] = {0, 0, 0, 0}; // index 0 is usused
  89.    
  90.     for(int i = 0; i < doorsNb; i++){
  91.         int door = doors[i];
  92.        
  93.         if(isZeroEncountered && door > 0)
  94.             return 0;
  95.            
  96.         if(door == 0){
  97.             isZeroEncountered = 1;
  98.             continue;
  99.         }
  100.        
  101.         int switchh = switches[door];
  102.         //printf("b %d %d %d\n", door, switchh, score);
  103.         if(switchh == 0){
  104.             switches[door] = 1;
  105.             score += door;
  106.         } else {
  107.             switches[door] = 0;
  108.             score -= door;
  109.         }
  110.        
  111.         //printf("a %d %d %d\n", door, switches[door], score);
  112.         combination = updateCombination(combination, score);
  113.     }
  114.    
  115.     return combination;
  116. }
  117.  
  118.  
  119. int go()
  120. {
  121.     /* 6    5    4
  122.     /*   11   10
  123.     /* 7    8    9
  124.     /*   13   12   14
  125.     */
  126.    
  127.     int PATHS[] = {
  128.         MASK_4 |MASK_9 |MASK_14,
  129.         MASK_4 |MASK_10 |MASK_8 |MASK_12 |MASK_14,
  130.         MASK_4 |MASK_10 |MASK_11 |MASK_7 |MASK_13 |MASK_12 |MASK_14,
  131.        
  132.         MASK_5 |MASK_10 |MASK_9 |MASK_14,
  133.         MASK_5 |MASK_11 |MASK_8 |MASK_12 |MASK_14,
  134.         MASK_5 |MASK_11 |MASK_7 |MASK_13 |MASK_12 |MASK_14,
  135.        
  136.         MASK_6 |MASK_11 |MASK_10 |MASK_9 |MASK_14,
  137.         MASK_6 |MASK_11 |MASK_8 |MASK_12 |MASK_14,
  138.         MASK_6 |MASK_7 |MASK_13 |MASK_12 |MASK_14,
  139.         MASK_6 |MASK_7 |MASK_13 |MASK_8 |MASK_10 |MASK_9 |MASK_14
  140.     };
  141.     int PATHS_NB = sizeof(PATHS) / sizeof(int);
  142.  
  143.     int doors[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  144.     int doorsNb = sizeof(doors) / sizeof(int);
  145.     int firstDoorIndex = 0;
  146.    
  147.    
  148.     // LOL : i don't know how to recurse ...
  149.     for(int a = 0; a <= 3; a++){
  150.         doors[0]=a;
  151.         for(int b = 0; b <= 3; b++){
  152.             doors[1]=b;
  153.             for(int c = 0; c <= 3; c++){
  154.                 doors[2]=c;
  155.                 for(int d = 0; d <= 3; d++){
  156.                     doors[3]=d;
  157.                     for(int e = 0; e <= 3; e++){
  158.                         doors[4]=e;
  159.                         for(int f = 0; f <= 3; f++){
  160.                             doors[5]=f;
  161.                             for(int g = 0; g <= 3; g++){
  162.                                 doors[6]=g;
  163.                                 for(int h = 0; h <= 3; h++){
  164.                                     doors[7]=h;
  165.                                     for(int i = 0; i <= 3; i++){
  166.                                         doors[8]=i;
  167.                                         for(int j = 0; j <= 3; j++){
  168.                                             doors[9]=j;
  169.                                             for(int k = 0; k <= 3; k++){
  170.                                                 doors[10]=k;
  171.                                                 for(int l = 0; l <= 3; l++){
  172.                                                     doors[11]=l;
  173.                                                     int combination = calculateCombination(doors, doorsNb);
  174.                                                     if(isDoorsInPaths(combination, PATHS, PATHS_NB)){
  175.                                                         printf("%d %d %d %d %d %d %d %d %d %d %d %d - ", doors[0], doors[1], doors[2], doors[3], doors[4],doors[5],
  176.                                                                                                         doors[6], doors[7], doors[8], doors[9], doors[10],doors[11]);
  177.                                                         printBits(sizeof(combination),&combination);
  178.                                                     }
  179.                                                 }
  180.                                             }
  181.                                         }
  182.                                     }
  183.                                 }  
  184.                             }
  185.                         }
  186.                     }
  187.                 }
  188.             }
  189.         }
  190.     }
  191.    
  192.     printf("Done.\n");
  193. }
  194.  
  195. int main(){
  196.     go();
  197.     //printf("%x %x", updateCombination(0, 1), 0b10010000010000);
  198. }
  199.  
  200.  
Add Comment
Please, Sign In to add comment