Solve puzzle with code

Here is a quite common programming interview puzzle – You have two buckets. A 3 gallon bucket and a 5 gallon bucket. Buckets are not marked or graduated. You are to fetch 4 gallon of water in a single trip to the river. How will you do it?
Obviousy, this question is asked by changing the capacities of the two buckets and the amount of water to be fetched.
Your mission, should you choose to accept it, is to write a program to solve this puzzle in a generic way. Your program should take 3 inputs – capacities of the two buckets and the amount of water to be fetched. After solving the puzzle, your program must write out the solution step-by-step. You can use any programming language, any platform, any programming technique to write the program. Use whatever you like – C#, VB, C, C++, Java, Ruby, Python, F#, Boo, Javascript or any other language of your choice. Use design patterns, TDD, BDD, MVVM, Linq or whatever. Make it blazing fast, exploit multiple cores, use artificial intelligence. Go nuts! If you want to write the whole program in T-sql, powershell script or a batch script, go ahead. Heck ! you can even do it with regular expressions, if that your thing!
Your mission, should you choose to accept it, is to write a program to solve this common programming interview puzzle in a generic way.
The puzzle goes like this – You have two buckets. A 3 gallon bucket and a 5 gallon bucket. Buckets are not marked or graduated. You are to fetch 4 gallons of water in a single trip to the river. How will you do it?
tommylees-flickr-buckets
Photo Credit : http://www.flickr.com/photos/71256895@N00/3688461774/sizes/l/in/photostream/

Your program should take 3 inputs – capacities of the two buckets and the amount of water to be fetched. After solving the puzzle, your program must write out the solution step-by-step. You can use any language, any platform and any technique to write the program. Use whatever you like – C#, VB, C, C++, Java, Ruby, Python, F#, Boo, Javascript, powershell script or any other language of your choice. Use design patterns, TDD, BDD, MVVM, Linq or whatever. Make it blazing fast, exploit multiple cores, use artificial intelligence. Go nuts! If you want to write the whole program in T-sql, go ahead. Heck ! you can even do it with regular expressions, if that your thing!