Please create an account to participate in the Slashdot moderation system

 



Forgot your password?
typodupeerror
×
Toys Programming Technology

Programming Puzzles 392

An anonymous reader writes "Spotted over at the Economist: 'Sliding-block puzzles look easy, but they can be tricky to solve. The best known is the 15 Puzzle, which became hugely popular in the late 1870s. This involves square tiles labelled with the numbers 1 to 15, which must be arranged in the correct order inside a four-by-four frame.' While we've all tried these puzzles, the inventor of Quzzle set out to design the easiest looking - yet most difficult puzzle around and turned to CS to find it. While the original article touches on it, at the puzzle's site you'll find Jim Lewis, the inventor, wrote a program in Haskell, a functional programming language to find the best design."
This discussion has been archived. No new comments can be posted.

Programming Puzzles

Comments Filter:
  • by TheLink ( 130905 ) on Saturday December 04, 2004 @02:01AM (#10994833) Journal
    One version [freehackers.org].
  • by mtrisk ( 770081 ) on Saturday December 04, 2004 @02:10AM (#10994870) Journal
    That's all great and dandy, but take a look at the article. It's about a guy who writes programs that will make physical puzzles; this has nothing to do with programming puzzles or exercises.
  • by CheesyPeteza ( 814646 ) on Saturday December 04, 2004 @02:10AM (#10994871)
    You'd think he would have created a web version of this puzzle so we could all try it out. :/
  • You can play it here (Score:5, Informative)

    by ambrosine10 ( 747895 ) on Saturday December 04, 2004 @02:11AM (#10994873)
    The puzzle can be played here [puzzleworld.org].
  • GAP (Score:1, Informative)

    by Anonymous Coward on Saturday December 04, 2004 @02:20AM (#10994901)
    You can analyze puzzles like this using GAP. Here [st-and.ac.uk] is an example using Rubik's cube(Google cache [64.233.161.104] since the site seems down to be down at the moment.)
  • Question 3 Solved (Score:5, Informative)

    by LighthouseJ ( 453757 ) on Saturday December 04, 2004 @02:38AM (#10994948)
    3. C/C++ : Exchange two numbers without using a temporary variable.

    My Computer Architecture teacher asked us essentially this question, he said Intel asked it in a way more palatible for computer engineers than CS students but it still works.

    If A contains one number (int or register) and B contains the other:

    B = A XOR B;
    A = A XOR B;
    B = A XOR B;

    Explanation:
    XOR is bitwise exclusive OR and is basically true when the bits differ.
    Say if we're dealing with 4-bit numbers. In the example, say A = 1011 (11 in decimal) and B = 0101 (5 in decimal).

    Step 1: If you take the exclusive OR of A (1011) and B(0101), the result is 1110 and put that into B (or A would be fine). Do bit by bit comparisons based on position from left to right and if the bits are different, mark a one in it's position, if they are the same, mark a zero. By this point A contains an original pattern, 1011 (decimal 11) and B contains the difference 1110 (decimal 14) between patterns.

    Step 2: Take the XOR of A and B again and store in the variable with an original pattern (A in this instance). 1011 XOR 1110 comes out to being 0101 (the original value for B) and we store it in A.

    Step 3: Take the final XOR of A and B again, store in last register B. 0101 XOR 1110 comes out to 1011 (our original A) and is stored in register B.

    Voila, 2 values exchanged very elegantly without wasted space and done in the same amount of steps as using a temporary variable. If you still don't understand, it works on the premise that if you have two piece of data, you only need one piece of data and whats different from the two pieces of data, that way you can reconstruct the other piece of data.
  • by Rahga ( 13479 ) on Saturday December 04, 2004 @02:38AM (#10994951) Journal
    First of all, for GNOME 2.10, gnome-game's Klotski has been updated and now supports SVG and comes with 37 puzzles, several classic wood puzzles from Minrobu Abe (this one may be solvable in 227 moves, not sure [rahga.com]...)

    I've also started a hint function that thells the user the precalculated minimum number of moves for each puzzle. The only problem is that Microsoft's Sunshine [rahga.com] puzzle is huge, and I've not seen any solutions for it online yet, never mind a calculated minimum. Any klotski addicts out there want to help me out?
  • by JasonSkywalker ( 204047 ) on Saturday December 04, 2004 @02:48AM (#10994984)
    This is what popped into my head upon reading this post... same principal as the XOR method but simpler to explain to layfolk...

    A = A*B
    B = A/B (gives B the orig A)
    A = A/B (gives A the orig B)

  • by Anonymous Coward on Saturday December 04, 2004 @02:51AM (#10994992)
    And for those looking to cheat, the solution is availible here (warning: pdf document) [johnrausch.com] on page 20.

    Unfortunately, reading the solution is almost a puzzle intself.
  • by LighthouseJ ( 453757 ) on Saturday December 04, 2004 @02:55AM (#10995011)
    True, but what if you have large numbers, like 64-bit values? To properly handle that, your ALU need to be able to multiply 2 64-bit values and the result is a 128-bit product then perform the division. With my style, you can keep the 64-bit bus sizes and the computation needed is considerably small, XOR is a very cheap instruction computationally.
  • by davebaum ( 653977 ) on Saturday December 04, 2004 @03:22AM (#10995088)
    ooops - should've previewed. The code didn't paste well into HTML. Here's another try:

    #include <stdio.h>

    int main()
    {
    if (printf("Hello, world\n")) { }
    }
  • by Otto ( 17870 ) on Saturday December 04, 2004 @03:29AM (#10995111) Homepage Journal
    1. Write a "Hello World" program in 'C' without using a semicolon.

    void main()
    {
    if (printf("Hello World!\n") {}
    }

    2. Write a C++ program without using any loop (if, for, while etc) to print numbers from 1 to 100 and 100 to 1;

    Dumb....

    void main()
    {
    printf("1\n2\n3\n... and so on...");
    }

    3. C/C++ : Exchange two numbers without using a temporary variable.

    x^=y; y^=x; x^=y;

    4. C/C++ : Find if the given number is a power of 2.

    if (!( x & (x-1)) printf("x is a power of 2\n");

    5. C/C++ : Multiply x by 7 without using multiplication (*) operator.

    x = (x6. C/C++ : Write a function in different ways that will return f(7) = 4 and f(4) = 7

    int function(int x) {
    switch (x)
    {
    case 7: return 4;
    case 4: return 7;
    }
    }
    Or countless other ways...

    10. Convert (integer) number in binary without loops.

    I assume you mean to print the binary form of an int without using loops. So I didn't use a loop, I used recursion. ;)

    void printbits(int x)
    {
    int n=x%2;
    if(x>=2) printbits(x/2);
    printf("%d", n);
    }

    13. Swap two numbers without using a third variable.

    Same problem as #3 above.

    That's enough for now...
  • Traffic (Score:3, Informative)

    by FlunkedFlank ( 737955 ) on Saturday December 04, 2004 @03:34AM (#10995131)
    The pic in the article reminds me of Traffic [stanford.edu], that awesomely addicting old Palm game (based on a real-world sliding block puzzle). I wish there was a PocketPC version!
  • by AndrewStephens ( 815287 ) * on Saturday December 04, 2004 @03:57AM (#10995206) Homepage
    This idea is not new, the guy that runs www.puzzlebeast.com [puzzlebeast.com] uses a similar program to generate ingenious puzzles. Read about the program he uses, then check our his java applets if you have a an hour or eight to spare.
  • Re:Question 3 Solved (Score:4, Informative)

    by cliveholloway ( 132299 ) on Saturday December 04, 2004 @03:59AM (#10995211) Homepage Journal

    What a mouthful! Us Perl programmers are, of course, lazy :)

    ($A,$B)=($B,$A);

    cLive ;-)

  • by Anonymous Coward on Saturday December 04, 2004 @04:14AM (#10995249)
    why does the r have to come from r. You still have to destroy the parity even if you use the r from rate
  • Re:Question 3 Solved (Score:2, Informative)

    by Anonymous Coward on Saturday December 04, 2004 @05:02AM (#10995358)
    Why would the XOR swap method run slower than the TMP register method on an out of order processor?

    A good bonus question. Anyone who can answer it (or even would think to ask it) realizes how useless micro-optimizations like the XOR swap trick tend to be.

    My answer, check me: an out-of-order processor does dependency analysis to form a partial order of the operations. Where there are no dependencies, some operations can be performed in parallel. The XOR swap method has a partial order length of 3. The temporary swap method has a partial order length of 2. Thus, the temporary swap method is faster. (Assuming the temporary is available and the processor can perform two operations in parallel.)

  • FULL SOLUTION (Score:4, Informative)

    by Kelerain ( 577551 ) <avc_mapmaster&hotmail,com> on Saturday December 04, 2004 @05:03AM (#10995361)
    I've worked through that solution, and writen up a much more aproachable version. Slashdots lameness filter prevents me from posting it in full, so I will link to it on my website here:
    http://www.abysmalstudios.com/files/quzzlesolution .txt [abysmalstudios.com]

    although that will most certainly disapear in a while, as I clean up my server from time to time. In the interests of preserving history, I'll give you the excessively condensed version:

    The goal is to move the A block in the far upper left corner, to the far upper right corner.

    ----
    AA11
    AA23
    **23 - starting puzzle condition (* is a space)
    4556
    4778
    ----

    The code below gives a block (1-8 or 'A') and an u/d/l/r direction.
    AD1LL2U3U6UL3DD2R6UUAR4UU5L7L8LU7RR5D8 LLAD6DL2L3UU AR8RU5U7LLAD8RR2D1R4U6DL2L8LU3D1R2U6RR2D4D1LL8U3U6 UAU7RR5D2D6L8D1R4U2LAL3DD1R8R6R4R2UUAL6DD8L3U6RAR2 DD4L8LUAU6LL7U5RR6DR7L5L3DDAR8DD4R2UU8LD4D2D1LAU

    You will have to break it up manually, because slashdots lameness filter wont even let me put line breaks in. Grab a copy of the text from my site while you can!
  • by xenocide2 ( 231786 ) on Saturday December 04, 2004 @05:09AM (#10995377) Homepage
    The point isnt't to actually cheat, the point is to trick the puzzle solver by moving one of the Rs to the wrong R spot. They'll leave it there when it belongs down below, unless they're lucky or supergeniuses.
  • Re:FULL SOLUTION (Score:3, Informative)

    by Anonymous Coward on Saturday December 04, 2004 @05:40AM (#10995437)
    Near the end of the solution is the sequence:

    5RR6DR7L5L

    The correct sequence is:

    5RR6DL7L5L

    Note that changes only one move from R to L.

    Other than that, the solution works wonderfully :-) Thanks for posting it.

  • by robbyjo ( 315601 ) on Saturday December 04, 2004 @06:36AM (#10995542) Homepage
    I think much of the tricks are covered in Bit Twiddling hacks here [stanford.edu].

  • Re:FULL SOLUTION (Score:3, Informative)

    by nachoboy ( 107025 ) on Saturday December 04, 2004 @07:43AM (#10995646)
    The solution on your website [Last-Modified: Sat, 04 Dec 2004 08:52:13 GMT] mislabels move 69 as move 68, and move 72 is noted as 6DR but should be 6DL.

    Despite that, the instructions were quite helpful. Thank you.
  • by johntromp ( 565732 ) on Saturday December 04, 2004 @08:53AM (#10995749)
    Orimazes are sliding block puzzles in disguise where the blocks are all unit size but limited to either horizontal or vertical movement. There is only one free spot, so sliding the blocks is equivalent to moving the free spot, which gives it the flavor of a maze.

    The hardest 4x4 instance can be played at
    http://www.cwi.nl/~tromp/oriscript4.html [www.cwi.nl]
    while the hardest 5x5 instance is at
    http://www.cwi.nl/~tromp/oriscript5.html [www.cwi.nl]

    -John

  • Re:Anyone Know (Score:2, Informative)

    by Infinityis ( 807294 ) on Saturday December 04, 2004 @09:57AM (#10995904) Homepage
    http://www.johnrausch.com/SlidingBlockPuzzles/quzz le.htm
  • by Superfluid Blob ( 798525 ) on Saturday December 04, 2004 @12:10PM (#10996378) Homepage
    The original (and I believe it was Sam Lloyd himself who came up with it) had RATEYOUR in on colour of tile and MINDPAL in another, so there was no possibility of swapping the As around.

HELP!!!! I'm being held prisoner in /usr/games/lib!

Working...