Have you ever seen one of those puzzles made with pegs (or golf tees) where you populate all the holes except one, then proceed to "jump" a peg with another and remove the jumped, until you have one peg left (possibly in a specified position)?

There is a classic triangle version of this, and a larger one and many other shapes on the tables at the Cracker Barrel restaurant.

A good many years ago I wrote a little C++ program to solve the triangle puzzle in general, using an integer to represent the state (bit set for peg present) and logic to represent the connectivity between the holes. Changing that logic can make the puzzle into whatever shape and size we want to solve.

If memory serves, I used an array indexed by this state number to point to another valid state closer to the solution, started with the solution and populated the array working backwards, and then could use the array as a map of what to do in any state and/or say whether it was winnable.

I came across one of those puzzles again, and the use of golf tees reminded me of Perl Golf, too. So I started thinking about solving this puzzle in Perl. C++ was pretty efficient, but could not be reconfigured for the puzzle shape. Perl’s strengths is associations, so a similar approach using hashes or references might be the way to go.

So I propose a challenge: to write a Perl Module to solve this general class of puzzle. An instance of the module is used by a main program to configure the actual puzzle and explore the details such as printing out a list of moves.

The goal isn’t to make it short like in Golf, but to make it clear, maintainable, efficient, and reusable. You don’t have to post a whole solution—just discussing different approaches can be enlightening and interesting.

—John


In reply to Peg-hop puzzles by John M. Dlugosz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.