I love problem solving. I love riddles. I love programming. These things are not unique, but I haven't found too many resources that depict methodologies on how to combine all three. Consider the following riddle from one of the Die Hard (I think) movies.
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:
Fill the 5 gallon bucket all the way up
Pour it into the 3 gallon bucket until it is full
Empty the 3 gallon bucket
Pour the remaining 2 gallons into the 3 gallon bucket
Fill the 5 gallon bucket all the way up
Finish filling the 3 gallon bucket
or
Fill the 3 gallon bucket all the way up
Pour all of the 3 gallons into the 5 gallon bucket
Fill the 3 gallon bucket all the way up
Finish filling the 5 gallon bucket
Empty the 5 gallon bucket
Pour the 1 gallon in the 3 gallon bucket into 5 gallon
Fill up the 3 gallon bucket
Pour it into the 5 gallon bucket
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.
I want to have 4 gallons in the 5 gallon bucket.
I can fill a bucket until it is full
I can empty a bucket
I can pour one bucket into another
Of course there are some limitations on that last one
If the first bucket isn't empty to begin with
Until the first bucket is empty
or until the second bucket is full
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?
Limbic~Region
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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.