r/dailyprogrammer • u/rya11111 3 1 • Mar 31 '12
[3/31/2012] Challenge #34 [difficult]
Inspired by the restaurant I ate at the other day. This is the puzzle: You have a wooden triangle, roughly equilateral with 5 rows of holes. The top row has one hole, and the bottom has 5, increasing by one hole with each successive row.
One hole of the triangle is empty and the rest are filled with golf tees. To make a move, you jump a golf tee over another adjacent golf tee into a hole immediately beyond it, removing that second golf tee from the game. Your goal is to find a solution set of jumps such that all of the golf tees but one are removed from the board. The notation of such a solution is at your discretion.
Bonus: Investigate if the choice of initial empty hole influences the solvability of the problem, and if so, what is the maximum number of pegs that can be removed given each starting hole.
- thanks to luxgladius for the challenge at /r/dailyprogrammer_ideas
3
u/luxgladius 0 0 Mar 31 '12
When this problem was copied over the little mini-diagram I made wasn't, probably because Reddit mangled the spacing upon copy/paste. I think it was helpful to visualize the problem, so I'll repeat it here for the benefit of those who haven't seen these before.
3
u/spc476 Mar 31 '12
The choice of initial hole has no influence over the solvability of the game. Also, taking into account rotations and reflections, there are only five initial starting locations. Anyway, on to the code (Lua):
3
u/luxgladius 0 0 Apr 01 '12 edited Apr 01 '12
Perl
Did a little pseudographical mode for an animation. Went ahead included the output of that under the first solution below. Each line corresponds to a different starting position of the board being empty. As spc476 said, all starting positions can lead to a solution, and there's probably exploitable symmetry so that I didn't have to do all of these, but oh well.
Output
0,0: 2,0->0,0 2,2->2,0 0,0->2,2 3,0->1,0 3,3->1,1 4,1->2,1 4,2->2,2 1,0->3,2 1,1->3,3 4,4->2,2 2,2->4,2 4,3->4,1 4,0->4,2
1,0: 3,0->1,0 0,0->2,0 3,2->3,0 1,1->3,1 3,0->1,0 3,3->1,1 4,1->2,1 1,0->3,2 4,3->4,1 4,0->4,2 4,2->2,2 1,1->3,3 4,4->2,2
1,1: 3,3->1,1 0,0->2,2 3,1->1,1 1,1->3,3 4,3->2,1 1,0->3,2 3,0->1,0 3,3->3,1 4,1->4,3 4,4->4,2 4,2->2,0 1,0->3,0 4,0->2,0
2,0: 0,0->2,0 2,2->0,0 2,0->2,2 3,3->1,1 0,0->2,2 4,2->2,0 3,0->1,0 4,4->4,2 4,1->4,3 4,3->2,1 2,2->2,0 1,0->3,0 4,0->2,0
2,1: 4,1->2,1 3,3->3,1 1,0->3,2 1,1->3,3 3,0->1,0 0,0->2,0 4,3->4,1 2,0->4,2 4,1->4,3 4,4->2,2 2,2->4,2 4,3->4,1 4,0->4,2
2,2: 0,0->2,2 2,0->0,0 2,2->2,0 3,0->1,0 0,0->2,0 4,1->2,1 2,0->2,2 3,3->1,1 4,3->4,1 4,0->4,2 4,2->2,2 1,1->3,3 4,4->2,2
3,0: 1,0->3,0 2,2->2,0 0,0->2,2 3,0->1,0 3,3->1,1 4,1->2,1 4,2->2,2 1,0->3,2 1,1->3,3 4,4->2,2 2,2->4,2 4,3->4,1 4,0->4,2
3,1: 1,1->3,1 3,3->1,1 4,2->2,2 1,1->3,3 3,0->3,2 1,0->3,0 4,0->2,0 4,4->4,2 4,1->4,3 4,3->2,1 2,0->2,2 3,3->1,1 0,0->2,2
3,2: 1,0->3,2 3,0->1,0 4,1->2,1 1,1->3,1 3,3->1,1 0,0->2,2 4,3->4,1 2,2->4,2 4,1->4,3 4,4->4,2 4,2->2,0 1,0->3,0 4,0->2,0
3,3: 1,1->3,3 2,0->2,2 0,0->2,0 3,0->1,0 3,3->1,1 4,1->2,1 4,2->2,2 1,0->3,2 1,1->3,3 4,4->2,2 2,2->4,2 4,3->4,1 4,0->4,2
4,0: 2,0->4,0 0,0->2,0 3,2->3,0 1,1->3,1 3,0->1,0 3,3->1,1 4,1->2,1 1,0->3,2 4,3->4,1 4,0->4,2 4,2->2,2 1,1->3,3 4,4->2,2
4,1: 4,3->4,1 2,0->4,2 0,0->2,0 1,1->3,1 3,0->1,0 3,3->1,1 4,1->2,1 4,2->2,2 1,1->3,3 4,4->2,2 2,2->2,0 1,0->3,0 4,0->2,0
4,2: 2,0->4,2 0,0->2,0 1,1->3,1 3,0->1,0 3,3->1,1 4,1->2,1 4,2->2,2 1,0->3,2 1,1->3,3 4,4->2,2 2,2->4,2 4,3->4,1 4,0->4,2
4,3: 4,1->4,3 2,0->4,2 0,0->2,0 1,1->3,1 3,0->1,0 3,3->1,1 4,2->2,0 1,0->3,0 4,0->2,0 4,3->2,1 2,0->2,2 1,1->3,3 4,4->2,2
4,4: 2,2->4,4 0,0->2,2 3,1->1,1 1,1->3,3 4,3->2,1 1,0->3,2 3,0->1,0 3,3->3,1 4,1->4,3 4,4->4,2 4,2->2,0 1,0->3,0 4,0->2,0
2
u/ixid 0 0 Apr 01 '12 edited Apr 01 '12
D
As others have discovered every starting position is solvable so I explored how many solutions exist for each position. This is my program, it solves every set of moves for the 5 rotatable starting positions in about 350ms. It can print the solutions in the same manner as luxgladius's. I use a short integer to store the board, each bit other than the sign bit is a peg, and an array with arrays of every possible move as their data, allowing the full tree search to find every possible way of reaching one peg remaining. The moves array is a list of triplets of start, middle and target peg number.
The number of solutions for each empty starting peg are as follows (I hope!):
1
u/[deleted] Mar 31 '12
That is to say, a golf-tee can't move unless it jumps another golf-tee?
1
u/1020302010 Apr 04 '12
Someone please do this is prolog!