Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 3 2 1 is optimal, this program proves it.
- #include <stdio.h>
- int MASK_4 = 1 << 4;
- int MASK_5 = 1 << 5;
- int MASK_6 = 1 << 6;
- int MASK_7 = 1 << 7;
- int MASK_8 = 1 << 8;
- int MASK_9 = 1 << 9;
- int MASK_10 = 1 << 10;
- int MASK_11 = 1 << 11;
- int MASK_12 = 1 << 12;
- int MASK_13 = 1 << 13;
- int MASK_14 = 1 << 14;
- void printBits(size_t const size, void const * const ptr)
- {
- unsigned char *b = (unsigned char*) ptr;
- unsigned char byte;
- int i, j;
- for (i = size-1; i >= 0; i--) {
- for (j = 7; j >= 0; j--) {
- byte = (b[i] >> j) & 1;
- printf("%u", byte);
- }
- }
- puts("");
- }
- int isDoorsMatching(int doors, int mask){
- return (doors&mask) == mask;
- }
- int isDoorsInPaths(int doors, int* paths, int size){
- int isIn = 1;
- for(int i = 0; i < size; i++){
- if(isDoorsMatching(doors, paths[i]))
- return isIn;
- }
- return !isIn;
- }
- int updateCombination(int oldCombination, int score){
- int clearAll = 0b000000000000000;
- int newCombination = oldCombination;
- switch(score){
- case 0:
- newCombination &= clearAll;
- break;
- case 1:
- newCombination |= (MASK_4|MASK_10|MASK_13);
- newCombination &= ((~(MASK_9|MASK_11|MASK_12|MASK_14))&0xFFFF);
- break;
- case 2:
- newCombination |= (MASK_5|MASK_11|MASK_12);
- newCombination &= ((~(MASK_8|MASK_10|MASK_13|MASK_14))&0xFFFF);
- break;
- case 3:
- newCombination |= (MASK_6|MASK_10|MASK_13);
- newCombination &= ((~(MASK_7|MASK_11|MASK_12|MASK_14))&0xFFFF);
- break;
- case 4:
- newCombination |= (MASK_7|MASK_11|MASK_12);
- newCombination &= ((~(MASK_6|MASK_10|MASK_13|MASK_14))&0xFFFF);
- break;
- case 5:
- newCombination |= (MASK_8|MASK_10|MASK_13);
- newCombination &= ((~(MASK_5|MASK_11|MASK_12|MASK_14))&0xFFFF);
- break;
- default: //6
- newCombination |= (MASK_9|MASK_11|MASK_12|MASK_14);
- newCombination &= ((~(MASK_4|MASK_10|MASK_13))&0xFFFF);
- break;
- }
- return newCombination;
- }
- int calculateCombination(int* doors, int doorsNb){
- int isZeroEncountered = 0;
- int combination = 0;
- int score = 0;
- int switches[] = {0, 0, 0, 0}; // index 0 is usused
- for(int i = 0; i < doorsNb; i++){
- int door = doors[i];
- if(isZeroEncountered && door > 0)
- return 0;
- if(door == 0){
- isZeroEncountered = 1;
- continue;
- }
- int switchh = switches[door];
- //printf("b %d %d %d\n", door, switchh, score);
- if(switchh == 0){
- switches[door] = 1;
- score += door;
- } else {
- switches[door] = 0;
- score -= door;
- }
- //printf("a %d %d %d\n", door, switches[door], score);
- combination = updateCombination(combination, score);
- }
- return combination;
- }
- int go()
- {
- /* 6 5 4
- /* 11 10
- /* 7 8 9
- /* 13 12 14
- */
- int PATHS[] = {
- MASK_4 |MASK_9 |MASK_14,
- MASK_4 |MASK_10 |MASK_8 |MASK_12 |MASK_14,
- MASK_4 |MASK_10 |MASK_11 |MASK_7 |MASK_13 |MASK_12 |MASK_14,
- MASK_5 |MASK_10 |MASK_9 |MASK_14,
- MASK_5 |MASK_11 |MASK_8 |MASK_12 |MASK_14,
- MASK_5 |MASK_11 |MASK_7 |MASK_13 |MASK_12 |MASK_14,
- MASK_6 |MASK_11 |MASK_10 |MASK_9 |MASK_14,
- MASK_6 |MASK_11 |MASK_8 |MASK_12 |MASK_14,
- MASK_6 |MASK_7 |MASK_13 |MASK_12 |MASK_14,
- MASK_6 |MASK_7 |MASK_13 |MASK_8 |MASK_10 |MASK_9 |MASK_14
- };
- int PATHS_NB = sizeof(PATHS) / sizeof(int);
- int doors[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
- int doorsNb = sizeof(doors) / sizeof(int);
- int firstDoorIndex = 0;
- // LOL : i don't know how to recurse ...
- for(int a = 0; a <= 3; a++){
- doors[0]=a;
- for(int b = 0; b <= 3; b++){
- doors[1]=b;
- for(int c = 0; c <= 3; c++){
- doors[2]=c;
- for(int d = 0; d <= 3; d++){
- doors[3]=d;
- for(int e = 0; e <= 3; e++){
- doors[4]=e;
- for(int f = 0; f <= 3; f++){
- doors[5]=f;
- for(int g = 0; g <= 3; g++){
- doors[6]=g;
- for(int h = 0; h <= 3; h++){
- doors[7]=h;
- for(int i = 0; i <= 3; i++){
- doors[8]=i;
- for(int j = 0; j <= 3; j++){
- doors[9]=j;
- for(int k = 0; k <= 3; k++){
- doors[10]=k;
- for(int l = 0; l <= 3; l++){
- doors[11]=l;
- int combination = calculateCombination(doors, doorsNb);
- if(isDoorsInPaths(combination, PATHS, PATHS_NB)){
- 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],
- doors[6], doors[7], doors[8], doors[9], doors[10],doors[11]);
- printBits(sizeof(combination),&combination);
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- }
- printf("Done.\n");
- }
- int main(){
- go();
- //printf("%x %x", updateCombination(0, 1), 0b10010000010000);
- }
Add Comment
Please, Sign In to add comment