Solving Puzzles with Quaere

Published 14 September 07 04:39 AM | andersnoras 

Earlier this year the program manager for the C# compiler, Luke Hoban showed how he used LINQ to solve one of the puzzles in Microsoft’s Puzzle Hunt event. On his blog he shows a similar puzzle, in which there is a mobile consisting of thirteen (A to M) weights suspended from a system of bars. Each weight has weighs between 1 and 13, and the goal is to find the weight each needs to be for the the weights to balance as shown in the diagram below:

                        |
                        |
            +--+--+--+--+--+--+--+
            |                    |
            |                    |
         +--+--+--+--+--+        |
         |     L        M        |
         |                       |
+--+--+--+--+--+--+     +--+--+--+--+--+
H              |  I     |  J        K  |
               |        |              |
      +--+--+--+--+--+  |     +--+--+--+--+--+
      E              F  |     G              |
                        |                    |
            +--+--+--+--+--+  +--+--+--+--+--+--+
            A              B  C                 D

Luke’s example shows that LINQ isn’t only about querying, and this goes for Quaere as well. Below is how to solve the puzzle using Quaere:

public class WeightsPuzzle {
    public void solveIt() {
        Variant solution=(Variant)
            first(
                from("a").in(range(1,13)).
                join("b").in(range(1,13)).on(eq("4 * a","b")).
                from("c").in(range(1,13)).
                join("d").in(range(1,13)).on(eq("5 * c","d")).
                from("e").in(range(1,13)).
                join("f").in(range(1,13)).on(eq("3 * e","2 * f")).
                join("g").in(range(1,13)).on(eq("2 * (c + d)","3 * g")).
                from("h").in(range(1,13)).
                join("i").in(range(1,13)).on(eq("3 * h - 2 * (e + f)","3 * i")).
                from("j").in(range(1,13)).
                join("k").in(range(1,13)).on(eq("3 * (a + b) + 2 * j - 2 * (g + c + d)","k")).
                from("l").in(range(1,13)).
                join("m").in(range(1,13)).on(eq("(h + i + e + f) - l","4 * m")).
                where(eq("4 * (l + m + h + i + e + f)","3 * (j + k + g + a + b + c + d)")).
                select(
                    create(
                        property("a"),
                        property("b"),
                        property("c"),
                        property("d"),
                        property("e"),
                        property("f"),
                        property("g"),
                        property("h"),
                        property("i"),
                        property("j"),
                        property("k"),
                        property("l"),
                        property("m"),
                        property("total","a + b + c + d + e + f + g + h + i + j + k + l + m")
                    )        
                )
            );
        System.out.println("a needs to weigh " + solution.get("a"));
        System.out.println("b needs to weigh " + solution.get("b"));
        System.out.println("c needs to weigh " + solution.get("c"));
        System.out.println("d needs to weigh " + solution.get("d"));
        System.out.println("e needs to weigh " + solution.get("e"));
        System.out.println("f needs to weigh " + solution.get("f"));
        System.out.println("g needs to weigh " + solution.get("g"));
        System.out.println("h needs to weigh " + solution.get("h"));
        System.out.println("i needs to weigh " + solution.get("i"));
        System.out.println("j needs to weigh " + solution.get("j"));
        System.out.println("k needs to weigh " + solution.get("k"));
        System.out.println("l needs to weigh " + solution.get("l"));
        System.out.println("m needs to weigh " + solution.get("m"));
        System.out.println("Total " + solution.get("total"));
    } 
}

I’ll round off this blog post with a challenge. There have been some criticism that Quaere is nothing new, and that there are other projects that achieve the same things. The challenge is to solve this puzzle using one of these frameworks, plain old Groovy or any other Java based technologies.

Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below