Question

Programming Contest :~}

Asked by: Kdo


The August 30 edition of the EE Community Newsletter posted a puzzle contest.  The newsletter wasn't really clear (sorry editors) on one of the key rules, specifically what language(s) were appropriate for the contest.  Clearly though, C was not acceptable as the size of the passed array is indeterminate to a C function.  Can you believe it?  The foundation language for so many of the so-called "modern" languages isn't allowed to play?

So let them have their silly game.  We'll have ours.

I'm proposing that we conduct the same contest but in C.  I'll provide the points for the winner(s).  Here are the (amended) rules.

1.  The object is to solve the "Cracker Barrel Peg Solitaire" game.  (This game has been around for a very long time, even before Cracker Barrel started to feature it.)  For rules and descriptions see the newsletter.  For a picture of the game, see this link:  http://shop.crackerbarrel.com/images/400/606154.jpg

2.  The number of pegs in the game may/will vary.  The base game has 5 rows in a diamond.  Our game will have no fewer than 5 rows and may have more.  Your code must handle games of arbitrary size.

3.  Only C code is acceptable.  I will test the entries on my AMD 64-3800 Fedora Core 5 system.  Entries will be compiled with gcc using the default options.

4.  Entries will not be accepted prior to September 16, 2006 so everyone will have a chance to think about the problem before source code is passed around in this thread.  I will ask the mods/page editor(s) to delete responses with source code submitted prior to that date.

5.  Entries must be received prior to September 23, 2006.  That gives everyone 2 weeks to write their programs.

6.  Code rules:
     A.  Only C code is acceptable.  It must compile on the system mentioned above or the program will be rejected.
     B.  You can have as many arrays, constants, functions, etc. as you want.
     C.  I will build the main() function used to test the entries.
     D.  main() will call each solver function repeatedly with a predefined set of "games".
     E.  The solver function WILL use the standard prototype.

     int SolveOnce (int size, int *puzzle);   //  returns 0 if no solution, 1 if solution found.

     F.  In the prototypes, size is the number of holes, not the number of rows.
     G.  puzzle[] contain a 1 where a peg exists, a 0 where the hole is empty.
          Note that more than one hole may be empty for any defined game.   ;)
     H.  Your function must be self initializing.  It will be called thousands of times to get accurate timing.
     I.   The array being passed can be considered volatile.  Scribble all that you want.  :)
     Z.  Please make it easy on me.  Structure your code so that in your submission, main() is the last thing in the source code.  This way I can use sed, awk, perl or other scripting tool to simply delete all of the source code starting at main(), append the main() to be used for the test, compile, and execute.


Good Luck, and good coding.  :)
Kent

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2006-09-09 at 08:21:08ID21983551
Tags

c

Topic

C Programming Language

Participating Experts
9
Points
500
Comments
44

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Upload gif/jpg file in cgi\perl
    Hi, I need a cgi in perl that recive from client (by HTML form) uploaded gif/jpg file and save it on the server. As a resule,the cgi file will show the client HTML with the gif/jpg picture.
  2. Game Design or Programming Contest Exist?
    Are there any PC Game Programming or Game Design contests? What are the typical first prizes? Publising/Development deal? Could you please provide URLS. Thanks.
  3. Boosting traffic with photo contest - how?
    I have a website which I have created with Microsoft Frontpage (www.tmbstudios.com). I was toying with the idea of driving traffic to my site by setting up a monthly "photo contest" (something like the one at http://www.somemoorecats.com/photopoll/) Can anyone te...
  4. Puzzle and I need to make sure if my program is correct
    Puzzle discription An interesting puzzle is as follows: A contestant on a TV show is asked to select one of the three doors. Behind two of the doors is a goat. The third door has a new BMW X5 behind it. After the contestant selects a door, the host opens one of the doors that...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: PaulCaswellPosted on 2006-09-09 at 09:05:37ID: 17486376

Hi Kent,

Nice one!

>>It must compile on the system mentioned above or the program will be rejected.
For those of us who do not work with gcc, could we perhaps be informed of the rejection and error codes/line numbers and given a few days grace to fix the problem?

Are there any system restrictions - such as multithreading?

How should we indicate the solution, and the steps to achieve it?

Paul

 

by: KdoPosted on 2006-09-09 at 10:04:13ID: 17486579

Hi Paul,

> For those of us who do not work with gcc, could we perhaps be informed of the rejection and error codes/line numbers and given a few days grace to fix the problem?

I don't expect thousands of entries so some flexibility is appropriate.  This seems like a rational thing to do.

 
> Are there any system restrictions - such as multithreading?

As long as it's in C and compiles as described, it's legal.

Qualification:  Embedded assembler code will NOT be considered C code.

 
> How should we indicate the solution, and the steps to achieve it?

See the prototype under E.   :~}

I don't care about the steps, just solvable / not solvable.  I fully expect that some puzzles will not be solvable so that a function can't "give up" once hopelessly lost and default to "solved".


Kent

 

by: x4uPosted on 2006-09-09 at 16:19:48ID: 17487538

This is a nice challange. I guess C will lead to more intresting entries than C# for such a task.

Can you define any constraints about the field size? I.e. how many bits will be neccessarry to store a single field index?

 

by: KdoPosted on 2006-09-09 at 18:28:28ID: 17487861

Hi x4u,

You can use whatever structure(s) you like.  The source array will be integers.  (int puzzle[]).


Kent

 

by: Infinity08Posted on 2006-09-10 at 00:34:14ID: 17488574

Nice challenge !

A few questions :

1) Can we assume a maximum for size ? If so which, or do we use the full capacity of an int (which seems quite excessive heh). Or do we scale the code for any value, including possibly future 256 bit integers ?

2) If we post the code here, how will you prevent people copying code (parts), or getting ideas from other people's code ?

3) a clarification on the form of the board. A diamond looks something like this :

    xxxx
     xxxx
      xxxx
       xxxx

    In the image you linked to, a triangle is shown ... which do we use ?

4) I assume that the location of the last remaining peg doesn't matter ?

 

by: KdoPosted on 2006-09-10 at 05:58:49ID: 17489318

Hi Infinity08,

1) Don't assume too much.  A puzzle may have 5 rows or it may have 1,000 (or more).  I will keep the puzzle size down so that it all fits in memory in my machine so that page faulting isn't an issue.  And no, I'm not gonna tell ya how much memory I have.  :)

2)  Good question.  Perhaps mailing me the codes is best.  I can repost them if the author wishes or they author can post them after the deadline.

3)  It's a pyramid form where length (row(n)) = n.  Connectivity from any hole is to the 1 or two adjacent holes horizontally and the 1, 2, 3, or 4 adjacent holes at 60, 120, 240, and 300 degrees.  A jump requires thee holes in line.

4)  Nah.  I thought about this.  Also though about "how many solutions" from a given puzzle.  But I'm gonna mirror the puzzle that the other guys get to play with.


Good to see you.  It's been a while since I've seen you on the forum,
Kent

 

by: Infinity08Posted on 2006-09-10 at 10:34:14ID: 17490088

I've been around, but a lot less often than I'd like to.

I think I'll start a thread in my brain to think about this problem :) Should be fun.

Oh yes, and I also suppose there's a limit on execution time ? We don't want the code to run for days :)

 

by: KdoPosted on 2006-09-10 at 15:24:01ID: 17490824

Hi Infinity08,

> Oh yes, and I also suppose there's a limit on execution time ? We don't want the code to run for days :)

If it does, that's Ok.  The politically correct term is "Last Runner Up".   ;)


Kent

 

by: billtouchPosted on 2006-09-10 at 18:09:37ID: 17491211

Hi Kent,

I would say great idea, but it has been said several times, so I will just go on to my question...

Puzzle is an array of int representing what exactly?  Does Puzzle[0] ==1 mean that the top hole of the triangle has a pin in it or does it represent that the left hole in the bottom row has a pin in it or something else?

Thanks,
Bill


 

by: KdoPosted on 2006-09-11 at 04:45:24ID: 17493594


Good question Bill,

For consistency's sake and everyone's easy, Puzzle[0] represents the top hole.  Puzzle[1] and Puzzle[2] represent the second row, etc.  


         0
       1  2
     3  4  5
   6  7  8  9
etc...

Kent

 

by: imladrisPosted on 2006-09-11 at 08:08:46ID: 17495148

And, how do you win? Fastest? Shortest? Most elegant?

 

by: KdoPosted on 2006-09-11 at 10:33:23ID: 17496341


Fastest Execution Time.

After the contest ends, I'll post the entries.  Our peers can be the judge(s) of "most elegant".   :~}

Kent

 

by: nafis_devlprPosted on 2006-09-12 at 05:20:47ID: 17501776

Kdo,

some suggestions,

1. it will be better to use char array rather than an int array as we will be using only 1 and 0 and you won't have to think about the size of the array as it can be easily terminated by '\0'.

2. multithreading should be allowed with certain libraries or not allowed at all, this will give a c-programmer who didn't use any of the multithreading library and it is fare also cause its an algorithm contest not an technical knowledge contest.

Nafis

 

by: KdoPosted on 2006-09-12 at 05:32:36ID: 17501859


Hi Nafis,

>> 1. it will be better to use char array rather than an int array as we will be using only 1 and 0 and you won't have to think about the size of the array as it can be easily terminated by '\0'.

I considered other data types, but settled on integers for the simple reason that memory access is a single-instruction, atomic operation on all of today's popular processors.  Using a byte addressable scheme may result in less than optimal performance in some processors.  (This isn't a fair test environment for people trying to develop their best algorithm.)

The only way that you can terminate the array with '\0' is if the data is character (i.e. ASCII) based instead of binary.  Again, testing for zero/non-zero is sometimes more efficient than testing for specific values so we'll stick with binary.


>> 2. multithreading should be allowed with certain libraries or not allowed at all, this will give a c-programmer who didn't use any of the multithreading library and it is fare also cause its an algorithm contest not an technical knowledge contest.

If you want to multi-thread, go ahead.  If you feel that the overhead of multi-threading is a waste, don't do it.  Determining the best algorithm is part of the game.  :~}


You do hint at something that needs clarification.  The test system is a single processor PC/Server, just as are the tools being used by most people.
Kent

 

by: nafis_devlprPosted on 2006-09-12 at 05:37:10ID: 17501897

Kdo,

Then how will I know the size of the array??

Nafis

 

by: KdoPosted on 2006-09-12 at 05:43:09ID: 17501946



Look at rule 6E.   :~}

Kent

 

by: nafis_devlprPosted on 2006-09-12 at 05:45:12ID: 17501969

Kdo,

Sorry, don't know why I was thinking "holes" as "empty holes", probably the name hole got me :-(

Nafis

 

by: PaulCaswellPosted on 2006-09-12 at 08:38:23ID: 17503964

Kent,

Can we assume the more common C extensions such as:

for ( int i = 0; ...

Paul

 

by: PaulCaswellPosted on 2006-09-12 at 09:41:00ID: 17504555

Can we also assume that the number of rows is odd?

If not, how can the diamond shape be constructed with an even number of rows?

Paul

 

by: KdoPosted on 2006-09-12 at 09:44:26ID: 17504599


>Can we also assume that the number of rows is odd?

No.


>If not, how can the diamond shape be constructed with an even number of rows?

Bowling pins are arranged in a 4 row diamond.  The classic peg game is a 5 row diamond.  

Extrapolate to visualize a 6 row diamond.  :~}


Kent

 

by: Infinity08Posted on 2006-09-12 at 10:11:04ID: 17504862

>> If not, how can the diamond shape be constructed with an even number of rows?
There seems to be a difference in definition of a diamond shape, as I already cleared up earlier with Kdo ... a diamond is a triangle in this case.

 

by: PaulCaswellPosted on 2006-09-12 at 11:11:28ID: 17505414

>>Extrapolate to visualize a 6 row diamond.  :~}
000100
001110
011111
111111
011110
001100

That cant be right! :-(

Paul

 

by: PaulCaswellPosted on 2006-09-12 at 11:15:38ID: 17505456

001000
011100
111110
011111
001110
000100

?

 

by: Infinity08Posted on 2006-09-12 at 11:16:18ID: 17505463

Generally, a diamond shape is the same as a rhombus :

http://en.wikipedia.org/wiki/Rhombus

But in this case, replace diamond with triangle :)

 

by: KdoPosted on 2006-09-12 at 11:19:49ID: 17505493

Hi Paul,

I keep using the term "Diamond" (apologies), but the shape of the puzzle is a triangle -- the upper half of a diamond.  Check out the picture in the link my original post.

  http://shop.crackerbarrel.com/images/400/606154.jpg

Think of the pins in a bowling alley.

  Front row       1
  Second row   2 3
  Third row     4 5 6
  Fourth row  7 8 9 10

This puzzle is the same, except that with all due deference to C, the pins are numberered relative 0.

In the bowling alley picture above, 1 "touches" 2 and 3.  1 can jump over 2 or 3 to land in 4 or 6.  The key is that 1 MUST jump over a pin and land in an empty hole.  In the 4-row bowling pin example, no position can jump to more than 2 other positions making this game trivial.  Adding another row adds a lot more options as there becomes more landing spots.  Above, 4 can land in 1 or 6.  In a 5 row puzzle 4 can land in 1, 6, 11, or 13.  As the puzzle grows, some/many locations can have up to 6 landing spots.


Apologies,
Kent

 

by: PaulCaswellPosted on 2006-09-12 at 11:26:56ID: 17505566

Got it!! :-) I had the word 'diamond' wrong.

Gosh there's such a lot we get to learn from the colonies. :-)

Paul

 

by: KdoPosted on 2006-09-12 at 12:13:35ID: 17505966


Hey -- stick with me kid.  I'll introduce you to FM radio, Color TV, milk shakes, and indoor plumbing.


    :~}

 

by: PaulCaswellPosted on 2006-09-12 at 13:00:38ID: 17506329

Hey! It was Thomas Crapper who invented the 'indoor plumbing', not just an Englishman but a Yorkshireman! We're proud of that part of our history! :-))

My apologies for digressing on an up-till-now good thread. :-)

Paul

 

by: bastibartelPosted on 2006-09-12 at 13:19:13ID: 17506490

Hi Kdo,

You said speed is the goal.
What if the fastest algorithm is not as good a solver as some other more thorough approach.

Is it going to be something like Biathlon where a 'miss' gets a time penalty...
 
Cheers!
Sebastian

 

by: Infinity08Posted on 2006-09-12 at 14:03:10ID: 17506856

I would think that correctness is a lot more important than speed. So, a miss would mean instant loss. Otherwise you can just guess, and have an ultra-fast algorithm that has a few time penalties, and still win ...

 

by: KdoPosted on 2006-09-25 at 05:54:32ID: 17591852



We have a winner!!!!!!!!!!!!!!!!!



  Film at 11:00.


 

by: cwwkiePosted on 2006-09-25 at 08:59:46ID: 17593468

 

by: Infinity08Posted on 2006-09-25 at 09:25:41ID: 17593696

>>  Film at 11:00.
What does that mean ?

 

by: cwwkiePosted on 2006-10-01 at 10:13:06ID: 17638563

>>  >>  Film at 11:00.
>>  What does that mean ?

I also like to know :~}

 

by: Infinity08Posted on 2006-10-29 at 05:00:17ID: 17828538

>> >>  Film at 11:00.
>> What does that mean ?
I'm still not clear about this ...

See Kdo's last post ...

 

by: KdoPosted on 2006-10-31 at 06:28:32ID: 17842140


Hi Paul,

I've been distracted.  :(

I'll close and award points.
Kent

 

by: KdoPosted on 2006-11-15 at 11:35:37ID: 17949758


Well, I've not had a chance to actually run the submission, but Infinity08 was the only (repeat ONLY) member that submitted an entry.

I do therefore declare him the clear winner.


Free Points to all that played,
Kent

 

by: Infinity08Posted on 2006-11-15 at 11:49:11ID: 17949861

Heh, I kind of imagined there wouldn't be too much entries, but still ... just me ??? Come on people !!

 

by: KdoPosted on 2006-11-15 at 12:01:35ID: 17949962


Maybe we can at least resurrect some discussion with a post-mortem?

 

by: billtouchPosted on 2006-11-15 at 12:25:37ID: 17950171

I love this idea but for me it came at a time when I was on a project that was way behind. Maybe we could keep this going. I'd like to enter something...

Also... could we see some winning source code?

Bill

 

by: Infinity08Posted on 2006-11-15 at 13:35:30ID: 17950810

I have no problem with re-opening this contest to get more entries ...

 

by: billtouchPosted on 2006-11-15 at 13:42:51ID: 17950864

The other possibility is to start a different contest. Of course, continuing this one would give me a reason to finish my code...

The contest was over before I had time to finish it and now - my computers are on a very slow mule train from California to Florida. They are due here sometime next week. Then I will be able to join in!

Bill

 

by: AtourayPosted on 2009-11-20 at 02:31:44ID: 25868986

I have seen wish_C post seeking for a sample of the peg jump game code in C. I think it is a good idea to have it posted here so that some of us who were not able to participate can study it.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...