Home > Community > Forums > Functional Verification Shared Code > Einstein's puzzle (System Verilog) solved by Incisive92

Email

* Required Fields

Recipients email * (separate multiple addresses with commas)

Your name *

Your email *

Message *

Contact Us

* Required Fields
First Name *

Last Name *

Email *

Company / Institution *

Comments: *

 Einstein's puzzle (System Verilog) solved by Incisive92 

Last post Sun, Nov 22 2009 9:38 PM by redwire. 1 replies.
Started by Vinayhonnavara 20 Nov 2009 12:54 PM. Topic has 1 replies and 18602 views
Page 1 of 1 (2 items)
Sort Posts:
  • Fri, Nov 20 2009 12:54 PM

    Einstein's puzzle (System Verilog) solved by Incisive92 Reply

    Hello All,

    Following is the einstein's puzzle solved by cadence Incisive92  (solved in less than 3 seconds -> FAST!!!!!!)

    Thanks,

    Vinay Honnavara

    Verification engineer at Keyu Tech

    vinayh@keyutech.com

     

     

     

     // Author: Vinay Honnavara

    // Einstein formulated this problem : he said that only 2% in the world can solve this problem
    // There are 5 different parameters each with 5 different attributes
    // The following is the problem

    // -> In a street there are five houses, painted five different colors (RED, GREEN, BLUE, YELLOW, WHITE)

    // -> In each house lives a person of different nationality (GERMAN, NORWEGIAN, SWEDEN, DANISH, BRITAIN)

    // -> These five homeowners each drink a different kind of beverage (TEA, WATER, MILK, COFFEE, BEER),

    // -> smoke different brand of cigar (DUNHILL, PRINCE, BLUE MASTER, BLENDS, PALL MALL)

    // -> and keep a different pet (BIRD, CATS, DOGS, FISH, HORSES)


    ///////////////////////////////////////////////////////////////////////////////////////
    // *************** Einstein's riddle is: Who owns the fish? ***************************
    ///////////////////////////////////////////////////////////////////////////////////////

    /*
    Necessary clues:

    1. The British man lives in a red house.
    2. The Swedish man keeps dogs as pets.
    3. The Danish man drinks tea.
    4. The Green house is next to, and on the left of the White house.
    5. The owner of the Green house drinks coffee.
    6. The person who smokes Pall Mall rears birds.
    7. The owner of the Yellow house smokes Dunhill.
    8. The man living in the center house drinks milk.
    9. The Norwegian lives in the first house.
    10. The man who smokes Blends lives next to the one who keeps cats.
    11. The man who keeps horses lives next to the man who smokes Dunhill.
    12. The man who smokes Blue Master drinks beer.
    13. The German smokes Prince.
    14. The Norwegian lives next to the blue house.
    15. The Blends smoker lives next to the one who drinks water.
    */




    typedef enum bit [2:0]  {red, green, blue, yellow, white} house_color_type;
    typedef enum bit [2:0]  {german, norwegian, brit, dane, swede} nationality_type;
    typedef enum bit [2:0]  {coffee, milk, water, beer, tea} beverage_type;
    typedef enum bit [2:0]  {dunhill, prince, blue_master, blends, pall_mall} cigar_type;
    typedef enum bit [2:0]  {birds, cats, fish, dogs, horses} pet_type;




    class Einstein_problem;

        rand house_color_type house_color[5];
        rand nationality_type nationality[5];
        rand beverage_type beverage[5];
        rand cigar_type cigar[5];
        rand pet_type pet[5];
            rand int arr[5];
        
        constraint einstein_riddle_solver {
        
            
        
            foreach (house_color[i])
                foreach (house_color[j])
                   if (i != j)
                    house_color[i] != house_color[j];
            foreach (nationality[i])
                foreach (nationality[j])
                   if (i != j)
                    nationality[i] != nationality[j];
            foreach (beverage[i])
                foreach (beverage[j])
                   if (i != j)
                    beverage[i] != beverage[j];
            foreach (cigar[i])
                foreach (cigar[j])
                   if (i != j)
                    cigar[i] != cigar[j];
            foreach (pet[i])
                foreach (pet[j])
                   if (i != j)
                    pet[i] != pet[j];
        
        
            //1) The British man lives in a red house.
            foreach(nationality[i])
                    (nationality[i] == brit) -> (house_color[i] == red);
                    
            
            //2) The Swedish man keeps dogs as pets.
            foreach(nationality[i])
                    (nationality[i] == swede) -> (pet[i] == dogs);
                    
                    
            //3) The Danish man drinks tea.        
            foreach(nationality[i])
                    (nationality[i] == dane) -> (beverage[i] == tea);
            
            
            //4) The Green house is next to, and on the left of the White house.
            foreach(house_color[i])        
                     if (i<4)
                        (house_color[i] == green) -> (house_color[i+1] == white);
            
            
            //5) The owner of the Green house drinks coffee.
            foreach(house_color[i])
                    (house_color[i] == green) -> (beverage[i] == coffee);
                    
            
            //6) The person who smokes Pall Mall rears birds.
            foreach(cigar[i])
                    (cigar[i] == pall_mall) -> (pet[i] == birds);
            
            
            //7) The owner of the Yellow house smokes Dunhill.
            foreach(house_color[i])
                    (house_color[i] == yellow) -> (cigar[i] == dunhill);
            
            
            //8) The man living in the center house drinks milk.
            foreach(house_color[i])
                    if (i==2) // i==2 implies the center house (0,1,2,3,4) 2 is the center
                        beverage[i] == milk;
            
            
            
            //9) The Norwegian lives in the first house.
            foreach(nationality[i])        
                    if (i==0) // i==0 is the first house
                        nationality[i] == norwegian;
            
            
            
            //10) The man who smokes Blends lives next to the one who keeps cats.
            foreach(cigar[i])        
                    if (i==0) // if the man who smokes blends lives in the first house then the person with cats will be in the second
                        (cigar[i] == blends) -> (pet[i+1] == cats);
            
            foreach(cigar[i])        
                    if (i>0 && i<4) // if the man is not at the ends he can be on either side
                        (cigar[i] == blends) -> (pet[i-1] == cats) || (pet[i+1] == cats);
            
            foreach(cigar[i])        
                    if (i==4) // if the man is at the last
                        (cigar[i] == blends) -> (pet[i-1] == cats);
            
            foreach(cigar[i])        
                    if (i==4)
                        (pet[i] == cats) -> (cigar[i-1] == blends);
            
            
            //11) The man who keeps horses lives next to the man who smokes Dunhill.
            foreach(pet[i])
                    if (i==0) // similar to the last case
                        (pet[i] == horses) -> (cigar[i+1] == dunhill);
            
            foreach(pet[i])        
                    if (i>0 & i<4)
                        (pet[i] == horses) -> (cigar[i-1] == dunhill) || (cigar[i+1] == dunhill);
                        
            foreach(pet[i])        
                    if (i==4)
                        (pet[i] == horses) -> (cigar[i-1] == dunhill);
                        


            //12) The man who smokes Blue Master drinks beer.
            foreach(cigar[i])
                    (cigar[i] == blue_master) -> (beverage[i] == beer);
            
            
            //13) The German smokes Prince.
            foreach(nationality[i])        
                    (nationality[i] == german) -> (cigar[i] == prince);
            

            //14) The Norwegian lives next to the blue house.
            foreach(nationality[i])
                    if (i==0)
                        (nationality[i] == norwegian) -> (house_color[i+1] == blue);
            
            foreach(nationality[i])        
                    if (i>0 & i<4)
                        (nationality[i] == norwegian) -> (house_color[i-1] == blue) || (house_color[i+1] == blue);
            
            foreach(nationality[i])        
                    if (i==4)
                        (nationality[i] == norwegian) -> (house_color[i-1] == blue);
            

            //15) The Blends smoker lives next to the one who drinks water.            
            foreach(cigar[i])            
                    if (i==0)
                        (cigar[i] == blends) -> (beverage[i+1] == water);
            
            foreach(cigar[i])        
                    if (i>0 & i<4)
                        (cigar[i] == blends) -> (beverage[i-1] == water) || (beverage[i+1] == water);
                        
            foreach(cigar[i])        
                    if (i==4)
                        (cigar[i] == blends) -> (beverage[i-1] == water);
            
        } // end of the constraint block
        


        // display all the attributes
        task display ;
            foreach (house_color[i])
                begin
                    $display("HOUSE : %s",house_color[i].name());
                end
            foreach (nationality[i])
                begin
                    $display("NATIONALITY : %s",nationality[i].name());
                end
            foreach (beverage[i])
                begin
                    $display("BEVERAGE : %s",beverage[i].name());
                end
            foreach (cigar[i])
                begin
                    $display("CIGAR: %s",cigar[i].name());
                end
            foreach (pet[i])
                begin
                    $display("PET : %s",pet[i].name());
                end
            foreach (pet[i])
                if (pet[i] == fish)
                    $display("THE ANSWER TO THE RIDDLE : The %s has %s ", nationality[i].name(), pet[i].name());
        
        endtask // end display
        
        
    endclass




    program main ;

        initial
            begin
                Einstein_problem ep;
                ep = new();
                if(!ep.randomize())
                    $display("ERROR");
                ep.display();
            end
    endprogram // end of main

            

    • Post Points: 20
  • Sun, Nov 22 2009 9:38 PM

    • redwire
    • Top 10 Contributor
    • Joined on Thu, Jul 17 2008
    • Allen, TX
    • Posts 874
    • Points 13,460
    Re: Einstein's puzzle (System Verilog) solved by Incisive92 Reply

     Nice work.  I assume you saw http://en.wikipedia.org/wiki/Zebra_Puzzle and the LISP solution here: http://www.weitz.de/einstein.html

    Let's see you run it in SKILL and see which variant is faster. :)

    • Post Points: 5
Page 1 of 1 (2 items)
Sort Posts:
Started by Vinayhonnavara at 20 Nov 2009 12:54 PM. Topic has 1 replies.