Thread: Solving colors wheels problem

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    5

    Solving colors wheels problem

    Hi ,
    I'm trying in last 3 days solving this tricky problem and printing the correct result sequence of the wheels numbers .

    The problem is that Im having 7 wheels colored on same colors (white / red / green ...etc) but it's differently ordered and organized per the wheel which mean each wheel has different color order than the other wheels colors.
    Here's the photo of wheels and the colors of each wheel: ( the wheels numbered from left to right as you see on the photo down )

    Solving colors wheels problem-gamedetails-jpg


    Here's an example of what shall be the game output but it's not fully correct (not correct) since not all the 7 wheels have set on the table - 2 other wheels that left outside can't meet the game rule for same color on the tangent point of adjacent wheels:
    Solving colors wheels problem-example-jpg
    but as you see here in this example it's not fully correct (considered not correct) since there's two other wheels left and can't meet the game rule where each tangent points of adjacent wheels that we've set on the table must be on same color. In addition we need that all the wheels set on the table with the game rule met.


    Any idea how do I write any pseudocode or any approach on how do I solve this issue to write a function that output the sequence of the wheels numbers that I need to set on the table in order to meet the game rule ?

    Need to assign what shall be the number of the middle wheel that we set on the table and what's the correct sequence for the wheels number to set in order to meet the game rule ?!



    Thanks much for any help!
    Last edited by Ajeet Viyana; 05-21-2022 at 06:30 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    So are you looking for the smart way to solve the problem.

    Before you can do this, you need to understand how you solve the problem in real life.



    Or brute force, try every combination until you find something that works.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,640
    The difficulty here is how to represent the problem in the program.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    May 2022
    Posts
    5
    Thanks, I know thats the difficulty here and Im really stuck.
    Any approach idea I can start from?


    About brute force .. how do I know if specific combination works or not? Need the rule/condition for that and I really stuck even finding it diffcult how do I represent the problem in the code.


    A pleasure for any help!

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,640
    If the wheel color data is stored like this:
    Code:
    #define NUM_WHEELS  7
    #define NUM_COLORS  6
     
    enum {Black, Red, White, Blue, Yellow, Green};
     
    int wheels[NUM_WHEELS][NUM_COLORS] = {
        { Black,  Red,    White,  Blue,  Yellow, Green  },
        { Yellow, Blue,   Red,    Black, White,  Green  },
        { Blue,   Yellow, Red,    White, Green,  Black  },
        { Green,  Blue,   Yellow, Red,   White,  Black  },
        { Green,  Red,    White,  Blue,  Black,  Yellow },
        { Red,    Green,  Yellow, Black, Blue,   White  },
        { Yellow, White,  Blue,   Green, Red,    Black  }
    };
    And you number the positions of the wheels like this:
    Code:
         * *     * *
        * 1 *   * 2 *
         * *     * *
     * *     * *     * *
    * 6 *   * 0 *   * 3 *
     * *     * *     * *
         * *     * *
        * 5 *   * 4 *
         * *     * *
    And you number the orientation of a wheel such that the color in the upper left gives the orientation number.
    E.g, wheel number 0 in wheels array above: Black, Red, White, Blue, Yellow, Green
    If Black is in the upper left position, then it is in orientation 0. If Red is in that position then it's orientation 1. Etc.

    Then with a position and orientation array like this:
    Code:
    struct {
        int wheel;
        int orientation;
    } positions[NUM_WHEELS];
    You could go through all possibilities.
    There's quite a few, but only about two hundred million.
    7*6*5*4*3*2*1 = 5040
    6**6 = 46656 (you can assume center wheel is always in orientation 0)
    5040 * 46656 = 235,146,240

    I was able to get the following solution:
    Code:
         W K     R W
        R   G   G   B
         Y B     Y K
     G Y     B Y     K W
    R   K   K   R   R   G
     W B     G W     B Y
         B G     W B
        W   R   R   Y
         Y K     K G
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    May 2022
    Posts
    5
    Quote Originally Posted by john.c View Post
    If the wheel color data is stored like this:
    Code:
    #define NUM_WHEELS  7
    #define NUM_COLORS  6
     
    enum {Black, Red, White, Blue, Yellow, Green};
     
    int wheels[NUM_WHEELS][NUM_COLORS] = {
        { Black,  Red,    White,  Blue,  Yellow, Green  },
        { Yellow, Blue,   Red,    Black, White,  Green  },
        { Blue,   Yellow, Red,    White, Green,  Black  },
        { Green,  Blue,   Yellow, Red,   White,  Black  },
        { Green,  Red,    White,  Blue,  Black,  Yellow },
        { Red,    Green,  Yellow, Black, Blue,   White  },
        { Yellow, White,  Blue,   Green, Red,    Black  }
    };
    And you number the positions of the wheels like this:
    Code:
         * *     * *
        * 1 *   * 2 *
         * *     * *
     * *     * *     * *
    * 6 *   * 0 *   * 3 *
     * *     * *     * *
         * *     * *
        * 5 *   * 4 *
         * *     * *
    And you number the orientation of a wheel such that the color in the upper left gives the orientation number.
    E.g, wheel number 0 in wheels array above: Black, Red, White, Blue, Yellow, Green
    If Black is in the upper left position, then it is in orientation 0. If Red is in that position then it's orientation 1. Etc.

    Then with a position and orientation array like this:
    Code:
    struct {
        int wheel;
        int orientation;
    } positions[NUM_WHEELS];
    You could go through all possibilities.
    There's quite a few, but only about two hundred million.
    7*6*5*4*3*2*1 = 5040
    6**6 = 46656 (you can assume center wheel is always in orientation 0)
    5040 * 46656 = 235,146,240

    I was able to get the following solution:
    Code:
         W K     R W
        R   G   G   B
         Y B     Y K
     G Y     B Y     K W
    R   K   K   R   R   G
     W B     G W     B Y
         B G     W B
        W   R   R   Y
         Y K     K G

    Thanks Much, it makes sense !!

    appreciated , could you please attach the function that output the following solution that you attached ? I will ofcourse try to understand it.

  7. #7
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Interesting problem... Thanks for posting it.

  8. #8
    Registered User
    Join Date
    May 2022
    Posts
    5
    Quote Originally Posted by john.c View Post
    If the wheel color data is stored like this:
    Code:
    #define NUM_WHEELS  7
    #define NUM_COLORS  6
     
    enum {Black, Red, White, Blue, Yellow, Green};
     
    int wheels[NUM_WHEELS][NUM_COLORS] = {
        { Black,  Red,    White,  Blue,  Yellow, Green  },
        { Yellow, Blue,   Red,    Black, White,  Green  },
        { Blue,   Yellow, Red,    White, Green,  Black  },
        { Green,  Blue,   Yellow, Red,   White,  Black  },
        { Green,  Red,    White,  Blue,  Black,  Yellow },
        { Red,    Green,  Yellow, Black, Blue,   White  },
        { Yellow, White,  Blue,   Green, Red,    Black  }
    };
    And you number the positions of the wheels like this:
    Code:
         * *     * *
        * 1 *   * 2 *
         * *     * *
     * *     * *     * *
    * 6 *   * 0 *   * 3 *
     * *     * *     * *
         * *     * *
        * 5 *   * 4 *
         * *     * *
    And you number the orientation of a wheel such that the color in the upper left gives the orientation number.
    E.g, wheel number 0 in wheels array above: Black, Red, White, Blue, Yellow, Green
    If Black is in the upper left position, then it is in orientation 0. If Red is in that position then it's orientation 1. Etc.

    Then with a position and orientation array like this:
    Code:
    struct {
        int wheel;
        int orientation;
    } positions[NUM_WHEELS];
    You could go through all possibilities.
    There's quite a few, but only about two hundred million.
    7*6*5*4*3*2*1 = 5040
    6**6 = 46656 (you can assume center wheel is always in orientation 0)
    5040 * 46656 = 235,146,240

    I was able to get the following solution:
    Code:
         W K     R W
        R   G   G   B
         Y B     Y K
     G Y     B Y     K W
    R   K   K   R   R   G
     W B     G W     B Y
         B G     W B
        W   R   R   Y
         Y K     K G

    Do I need to use two " for loops " ? if you could attach the function would be much much appreciated !

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    1,640
    Do I need to use two " for loops " ? if you could attach the function would be much much appreciated
    I'm not posting the answer, at least not anytime soon.

    Yes, you will need nested loops, although it's a little more complicated than that.

    You need to generate the permutations of the wheel placements.
    You can use Heap's algorithm for that. I used the non-recursive form.

    For each permutation, you need to check all orientations (although you can fix the center wheel at orientation 0).
    This can be programmed as 6 nested loops, or perhaps more neatly using recursion.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  10. #10
    Registered User
    Join Date
    May 2022
    Posts
    5
    Quote Originally Posted by john.c View Post
    I'm not posting the answer, at least not anytime soon.

    Yes, you will need nested loops, although it's a little more complicated than that.

    You need to generate the permutations of the wheel placements.
    You can use Heap's algorithm for that. I used the non-recursive form.

    For each permutation, you need to check all orientations (although you can fix the center wheel at orientation 0).
    This can be programmed as 6 nested loops, or perhaps more neatly using recursion.
    I understand, but really I'm stuck , else I wouldn't ask .
    This question is complicated and like 5 days Im stuck so it says it's very challenged !! and Im really interested on how you built the function since I can't arrive to what you arrived.

    Kindly , could you write the pseudo code steps (data structure) for building the function if you dont want to attach the function?

    Really thanks much.

  11. #11
    Registered User
    Join Date
    Dec 2017
    Posts
    1,640
    It would be much more work to write detailed pseudocode than to post the program. But basically it is:
    Code:
    For each permutation of the wheel positions:
       For each orientation of the wheels in those positions:
          If it is a solution:
              Print it
    Why don't you show some work.
    Try this for starters.
    Code:
    #include <stdio.h>
     
    void output(int *a, int n) {
        for (int i = 0; i < n; ++i) printf("%d ", a[i]);
        putchar('\n');
    }
     
    // Print out all permutations of a
    void generate(int *a, int n) {
        // Fill this in with the non-recursive form of Heap's algorithm.
    }
     
    int main() {
        int a[] = {0, 1, 2, 3};
        generate(a, 4);
    }
    Expected output:
    0 1 2 3
    1 0 2 3
    2 0 1 3
    0 2 1 3
    1 2 0 3
    2 1 0 3
    3 1 0 2
    etc.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  12. #12
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    I'm wondering if anyone tried to write a solution for this puzzle? Is it solvable with the given wheels?

    I tried writing a brute-force solution and it could match right up to the last wheel where it failed.

  13. #13
    Registered User
    Join Date
    Dec 2017
    Posts
    1,640
    As I said before, I was able to find the following solution, which is in fact the only solution. Below I added the wheel number as per the original picture so that you can check that these are the actual wheels. K means black.

    Solving colors wheels problem-wheels-jpg
    Code:
    .    W K     R W
        R 4 G   G 5 B
         Y B     Y K
     G Y     B Y     K W
    R 6 K   K 3 R   R 2 G
     W B     G W     B Y
         B G     W B
        W 7 R   R 1 Y
         Y K     K G
    You could PM me your code if you want me to look at it.
    I could PM you my code if you want.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  14. #14
    Registered User
    Join Date
    Feb 2022
    Location
    Canada, PEI
    Posts
    103
    Got the same solution as you when I re-wrote the code...Must have been a typo..I'll send it to you anyway.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-06-2013, 07:49 AM
  2. Simplifying Numerical Solving before actual solving
    By Vespasian in forum Tech Board
    Replies: 3
    Last Post: 10-14-2012, 11:39 AM
  3. Need help in solving this problem
    By lokachari in forum C Programming
    Replies: 7
    Last Post: 10-27-2009, 07:32 PM
  4. Relation between material colors and light colors.
    By indigo0086 in forum Game Programming
    Replies: 3
    Last Post: 04-18-2007, 03:20 PM
  5. Alloy wheels, (car).
    By adrianxw in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-14-2005, 05:24 AM

Tags for this Thread