You have a 5 gallon bucket and a 3 gallon bucket with as much water as you need, but no other measuring devices. Fill the 5 gallon bucket with exactly 4 gallons of water. The solution is fairly simple:
or
To write this as a program, you need to be able to be able to create a decision tree. You need to be able to define a desired state. You need to be able to pipe the results of the decision back into the tree until you attain the desired state. A bonus would be to do this in the least number of iterations.
It is easy to describe these with words.
Of course there are some limitations on that last one
Is this type of programming practical with Perl? Would this program be too complex to generalize? If I were adept enough to write a program to solve the above riddle, it shouldn't be difficult to modify it to solve the following:
A farmer has a chicken, grain, and a fox. He needs to cross a river and can only take one item with him at a time. He can cross as many times as he needs to, but he can not leave the chicken and the fox together or the chicken and grain.
Solution available upon request. Better yet, write the program to solve it!
I have always been fascinated with AI and "stateful" programming, but always felt that languages like C were just too cumbersome to casually dabble with. Is Perl the same?
PS
This post was for open discussion, I didn't expect someone to write the code. I am more interested in open discussion about this type of programming. With that said, I am VERY interested to see if anyone is capable of writing the code described above in the general sense
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Solving riddles with Perl
by mdillon (Priest) on Sep 17, 2002 at 22:44 UTC | |
Re: Solving riddles with Perl
by Helter (Chaplain) on Sep 19, 2002 at 13:29 UTC |