Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Odd number of pigs in cages

Status
Not open for further replies.

jimoo

Programmer
Jun 2, 2003
1,111
US
A farmer asks you to build 4 changes to hold his 9 pigs and he wants to you put an odd number of pigs in each cage. Each cage must contain at least 1 pig. How can you do that?



Jim
 
Dian, Pigs might. I think pigs would feel strongly about keeping their integrity; no non-integral pigs allowed. Though they might feel happier about non-integral cages.

Sam Bones, sorry, IT is no longer a safe haven from manual work - hadn't you herd?

Olaf, there are enough angry birds in my life.
 
==> 1.5 is considered an odd number?
Only if 1.5 is considered an integer.

--------------
Good Luck
To get the most from your Tek-Tips experience, please read
FAQ181-2886
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
1.5 pigs is considered LUNCH

I do not Have A.D.D. im just easily, Hey look a Squirrel!
 
Here's the output after a small change to my program...

Topology 1: {{P} P} {P} {P}
1. {{[pig]}} {[pig][pig][pig][pig][pig]} {[pig][pig][pig]}
2. {{[pig]}} {[pig][pig][pig][pig][pig][pig][pig]} {[pig]}
3. {{[pig]}[pig][pig]} {[pig][pig][pig]} {[pig][pig][pig]}
4. {{[pig]}[pig][pig]} {[pig][pig][pig][pig][pig]} {[pig]}
5. {{[pig]}[pig][pig][pig][pig]} {[pig][pig][pig]} {[pig]}
6. {{[pig]}[pig][pig][pig][pig][pig][pig]} {[pig]} {[pig]}
7. {{[pig][pig][pig]}} {[pig][pig][pig]} {[pig][pig][pig]}
8. {{[pig][pig][pig]}} {[pig][pig][pig][pig][pig]} {[pig]}
9. {{[pig][pig][pig]}[pig][pig]} {[pig][pig][pig]} {[pig]}
10. {{[pig][pig][pig]}[pig][pig][pig][pig]} {[pig]} {[pig]}
11. {{[pig][pig][pig][pig][pig]}} {[pig][pig][pig]} {[pig]}
12. {{[pig][pig][pig][pig][pig]}[pig][pig]} {[pig]} {[pig]}
13. {{[pig][pig][pig][pig][pig][pig][pig]}} {[pig]} {[pig]}
13 solutions

Topology 2: {{{{P} P} P} P}
14. {{{{[pig]}}}[pig][pig][pig][pig][pig][pig][pig][pig]}
15. {{{{[pig]}}[pig][pig]}[pig][pig][pig][pig][pig][pig]}
16. {{{{[pig]}}[pig][pig][pig][pig]}[pig][pig][pig][pig]}
17. {{{{[pig]}}[pig][pig][pig][pig][pig][pig]}[pig][pig]}
18. {{{{[pig]}}[pig][pig][pig][pig][pig][pig][pig][pig]}}
19. {{{{[pig]}[pig][pig]}}[pig][pig][pig][pig][pig][pig]}
20. {{{{[pig]}[pig][pig]}[pig][pig]}[pig][pig][pig][pig]}
21. {{{{[pig]}[pig][pig]}[pig][pig][pig][pig]}[pig][pig]}
22. {{{{[pig]}[pig][pig]}[pig][pig][pig][pig][pig][pig]}}
23. {{{{[pig]}[pig][pig][pig][pig]}}[pig][pig][pig][pig]}
24. {{{{[pig]}[pig][pig][pig][pig]}[pig][pig]}[pig][pig]}
25. {{{{[pig]}[pig][pig][pig][pig]}[pig][pig][pig][pig]}}
26. {{{{[pig]}[pig][pig][pig][pig][pig][pig]}}[pig][pig]}
27. {{{{[pig]}[pig][pig][pig][pig][pig][pig]}[pig][pig]}}
28. {{{{[pig]}[pig][pig][pig][pig][pig][pig][pig][pig]}}}
29. {{{{[pig][pig][pig]}}}[pig][pig][pig][pig][pig][pig]}
30. {{{{[pig][pig][pig]}}[pig][pig]}[pig][pig][pig][pig]}
31. {{{{[pig][pig][pig]}}[pig][pig][pig][pig]}[pig][pig]}
32. {{{{[pig][pig][pig]}}[pig][pig][pig][pig][pig][pig]}}
33. {{{{[pig][pig][pig]}[pig][pig]}}[pig][pig][pig][pig]}
34. {{{{[pig][pig][pig]}[pig][pig]}[pig][pig]}[pig][pig]}
35. {{{{[pig][pig][pig]}[pig][pig]}[pig][pig][pig][pig]}}
36. {{{{[pig][pig][pig]}[pig][pig][pig][pig]}}[pig][pig]}
37. {{{{[pig][pig][pig]}[pig][pig][pig][pig]}[pig][pig]}}
38. {{{{[pig][pig][pig]}[pig][pig][pig][pig][pig][pig]}}}
39. {{{{[pig][pig][pig][pig][pig]}}}[pig][pig][pig][pig]}
40. {{{{[pig][pig][pig][pig][pig]}}[pig][pig]}[pig][pig]}
41. {{{{[pig][pig][pig][pig][pig]}}[pig][pig][pig][pig]}}
42. {{{{[pig][pig][pig][pig][pig]}[pig][pig]}}[pig][pig]}
43. {{{{[pig][pig][pig][pig][pig]}[pig][pig]}[pig][pig]}}
44. {{{{[pig][pig][pig][pig][pig]}[pig][pig][pig][pig]}}}
45. {{{{[pig][pig][pig][pig][pig][pig][pig]}}}[pig][pig]}
46. {{{{[pig][pig][pig][pig][pig][pig][pig]}}[pig][pig]}}
47. {{{{[pig][pig][pig][pig][pig][pig][pig]}[pig][pig]}}}
48. {{{{[pig][pig][pig][pig][pig][pig][pig][pig][pig]}}}}
35 solutions

Topology 3: {{P} {P} {P} P }
49. {{[pig]} {[pig]} {[pig]}[pig][pig][pig][pig][pig][pig]}
50. {{[pig][pig][pig]} {[pig]} {[pig]}[pig][pig][pig][pig]}
51. {{[pig][pig][pig]} {[pig][pig][pig]} {[pig]}[pig][pig]}
52. {{[pig][pig][pig]} {[pig][pig][pig]} {[pig][pig][pig]}}
53. {{[pig][pig][pig][pig][pig]} {[pig]} {[pig]}[pig][pig]}
54. {{[pig][pig][pig][pig][pig]} {[pig][pig][pig]} {[pig]}}
55. {{[pig][pig][pig][pig][pig][pig][pig]} {[pig]} {[pig]}}
7 solutions

Topology 4: {{{P} {P} P} P }
56. {{{[pig]} {[pig]}[pig]}[pig][pig][pig][pig][pig][pig]}
57. {{{[pig]} {[pig]}[pig][pig][pig]}[pig][pig][pig][pig]}
58. {{{[pig]} {[pig]}[pig][pig][pig][pig][pig]}[pig][pig]}
59. {{{[pig]} {[pig]}[pig][pig][pig][pig][pig][pig][pig]}}
60. {{{[pig][pig][pig]} {[pig]}[pig]}[pig][pig][pig][pig]}
61. {{{[pig][pig][pig]} {[pig]}[pig][pig][pig]}[pig][pig]}
62. {{{[pig][pig][pig]} {[pig]}[pig][pig][pig][pig][pig]}}
63. {{{[pig][pig][pig]} {[pig][pig][pig]}[pig]}[pig][pig]}
64. {{{[pig][pig][pig]} {[pig][pig][pig]}[pig][pig][pig]}}
65. {{{[pig][pig][pig][pig][pig]} {[pig]}[pig]}[pig][pig]}
66. {{{[pig][pig][pig][pig][pig]} {[pig]}[pig][pig][pig]}}
67. {{{[pig][pig][pig][pig][pig]} {[pig][pig][pig]}[pig]}}
68. {{{[pig][pig][pig][pig][pig][pig][pig]} {[pig]}[pig]}}
13 solutions

Topology 5: {{{P} P} {P} P }
69. {{{[pig]}} {[pig]}[pig][pig][pig][pig][pig][pig][pig]}
70. {{{[pig]}} {[pig][pig][pig]}[pig][pig][pig][pig][pig]}
71. {{{[pig]}} {[pig][pig][pig][pig][pig]}[pig][pig][pig]}
72. {{{[pig]}} {[pig][pig][pig][pig][pig][pig][pig]}[pig]}
73. {{{[pig]}[pig][pig]} {[pig]}[pig][pig][pig][pig][pig]}
74. {{{[pig]}[pig][pig]} {[pig][pig][pig]}[pig][pig][pig]}
75. {{{[pig]}[pig][pig]} {[pig][pig][pig][pig][pig]}[pig]}
76. {{{[pig]}[pig][pig][pig][pig]} {[pig]}[pig][pig][pig]}
77. {{{[pig]}[pig][pig][pig][pig]} {[pig][pig][pig]}[pig]}
78. {{{[pig]}[pig][pig][pig][pig][pig][pig]} {[pig]}[pig]}
79. {{{[pig][pig][pig]}} {[pig]}[pig][pig][pig][pig][pig]}
80. {{{[pig][pig][pig]}} {[pig][pig][pig]}[pig][pig][pig]}
81. {{{[pig][pig][pig]}} {[pig][pig][pig][pig][pig]}[pig]}
82. {{{[pig][pig][pig]}[pig][pig]} {[pig]}[pig][pig][pig]}
83. {{{[pig][pig][pig]}[pig][pig]} {[pig][pig][pig]}[pig]}
84. {{{[pig][pig][pig]}[pig][pig][pig][pig]} {[pig]}[pig]}
85. {{{[pig][pig][pig][pig][pig]}} {[pig]}[pig][pig][pig]}
86. {{{[pig][pig][pig][pig][pig]}} {[pig][pig][pig]}[pig]}
87. {{{[pig][pig][pig][pig][pig]}[pig][pig]} {[pig]}[pig]}
88. {{{[pig][pig][pig][pig][pig][pig][pig]}} {[pig]}[pig]}
20 solutions

[bigsmile]


 
Uh Sam, you left the gate open on 69, how long do you think those last two pigs will wait before escaping?

p5
 
Now I've also had the time to do a brute force attack on the problem. I was not separating cage topology from the rest of the problem in the same way, but made a brute force attack on the topology too: first I did compute 4 pig counts from 1-9 summing to 9 and put these counts right after a '{', which resulted in a raw solution like {p1{p2{p3{p4, then added 0 to the maximum number of possible cage closings '}' at each position.

I end up with only 74 solutions:

Code:
 1 {1{1}{2{5}}}
 2 {1}{1}{2{5}}
 3 {1{1}{4{3}}}
 4 {1}{1}{4{3}}
 5 {1{1}{6{1}}}
 6 {1}{1}{6{1}}
 7 {1{2{1}}{5}}
 8 {1}{2{1}}{5}
 9 {1{2{3}}{3}}
10 {1}{2{3}}{3}
11 {1{2{5}}{1}}
12 {1}{2{5}}{1}
13 {1{3}{2{3}}}
14 {1}{3}{2{3}}
15 {1{3}{4{1}}}
16 {1}{3}{4{1}}
17 {1{4{1}}{3}}
18 {1}{4{1}}{3}
19 {1{4{3}}{1}}
20 {1}{4{3}}{1}
21 {1{5}{2{1}}}
22 {1}{5}{2{1}}
23 {1{6{1}}{1}}
24 {1}{6{1}}{1}
25 {2{1{1}{5}}}
26 {2{1}{1}{5}}
27 {2{1}}{1}{5}
28 {2{1{3}{3}}}
29 {2{1}{3}{3}}
30 {2{1}}{3}{3}
31 {2{1{5}{1}}}
32 {2{1}{5}{1}}
33 {2{1}}{5}{1}
34 {2{2{2{3}}}}
35 {2{2{4{1}}}}
36 {2{3{1}{3}}}
37 {2{3}{1}{3}}
38 {2{3}}{1}{3}
39 {2{3{3}{1}}}
40 {2{3}{3}{1}}
41 {2{3}}{3}{1}
42 {2{4{2{1}}}}
43 {2{5{1}{1}}}
44 {2{5}{1}{1}}
45 {2{5}}{1}{1}
46 {3{1}{2{3}}}
47 {3}{1}{2{3}}
48 {3{1}{4{1}}}
49 {3}{1}{4{1}}
50 {3{2{1}}{3}}
51 {3}{2{1}}{3}
52 {3{2{3}}{1}}
53 {3}{2{3}}{1}
54 {3{3}{2{1}}}
55 {3}{3}{2{1}}
56 {3{4{1}}{1}}
57 {3}{4{1}}{1}
58 {4{1{1}{3}}}
59 {4{1}{1}{3}}
60 {4{1}}{1}{3}
61 {4{1{3}{1}}}
62 {4{1}{3}{1}}
63 {4{1}}{3}{1}
64 {4{2{2{1}}}}
65 {4{3{1}{1}}}
66 {4{3}{1}{1}}
67 {4{3}}{1}{1}
68 {5{1}{2{1}}}
69 {5}{1}{2{1}}
70 {5{2{1}}{1}}
71 {5}{2{1}}{1}
72 {6{1{1}{1}}}
73 {6{1}{1}{1}}
74 {6{1}}{1}{1}

Will check within the next few days, which solutions I am missing.

Bye, Olaf.
 
One missing solution that's easy to spot is the one with all nine pigs in an innermost cage: {{{{9}}}}. More generally, it appears that all of your solutions have at least one pig in each cage. This is a requirement if the cage is standing alone, but a cage that contains nested cages can get all its pigs from the inner cages. So there is no requirement that an outer cage in a nest contain a separate quota of pigs.
 
You also have some duplicate solutions. For example,

6. {1}{1}{6{1}}
74 {6{1}}{1}{1}

are the same solution, but with the cages in a different order.
 
Sam, all your pigs are facing us. For artistic reality, they should be at random angles.
 
Thanks, yes, I simply should also consider 0. This is how Sam Bones did make all his solutions the same string length. I should have looked closer there. Yes, and I did not yet remove permuations.

Bye, Olaf.
 
p5wizard said:
Uh Sam, you left the gate open on 69, how long do you think those last two pigs will wait before escaping?

Ouch! due to a copy/paste error, we have two pigs running free in this thread. Everyone watch where you step!


 
Actually there could be 7 pigs running around, although two seemed too busy in the trough ;-) .

p5
 
Ok, now I'm at 175 solutions including 0 in my nomenclature. Permutations should account for 87 of these solutions to be double solutions. {0{0{0{9}}}} surely is one solution without a permutation.

So, yes, separating cage topologies surely does simplify finding the distinct different solutions.

Bye, Olaf.
 
One of the ways I removed permutations is if there were two unnested cages, I would only print it if cage 1 was >= cage 2. That is...

{P1} >= {P2}

That means these would print...

{5} {1}
{3} {3}

...but this would not...

{1} {5}

That effectively removes the unnested cage dupes due to order.

I didn't have dupes like {1}{1}{6{1}} and {6{1}}{1}{1} because I hardcoded the topologies.

 
Thanks, Sam.

That's how I'd approach this, too. Sorting is always a good way to reveal permutations.

Bye, Olaf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top