r/dailyprogrammer • u/sceleris927 0 1 • Aug 31 '17
[2017-08-31] Challenge #329 [Intermediate] Solve the Water Bucket Riddle
Description
You are handed two buckets, one can hold 3 liters and the other 5 liters of water. You are allowed to
- fill a bucket with water until it is full
- empty a bucket
- transfer water from one bucket into the other until the target bucket is full
In the original riddle, you are to describe the actions that need to be done in order to get exactly 4 liters of water. Example solution:
Two buckets (3L, 5L):
Fill 5L -> (0,5)
5L to 3L -> (3,2)
Empty 3L -> (0,2)
5L to 3L -> (2,0)
Fill 5L -> (2,5)
5L to 3L -> (3,4)Another solution:
Fill 3L -> (3,0)
3L to 5L -> (0,3)
Fill 3L -> (3,3)
3L to 5L -> (1,5)
Empty 5L -> (1,0)
3L to 5L -> (0,1)
Fill 3L -> (3,1)
3L to 5L -> (0,4)Your task is to find a path of actions to obtain a target volume l <= max(m, n) liters of water, given two buckets of size m, n, where m and n are coprime.
Input Description
The input will be three numbers representing m, n, and l respectively.
Output Description
The format of the output will be a list of pairs representing the contents of the buckets m and n at each step:
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1), (3, 1), (0, 4)]
If there is no solution, print "no solution".
Challenge Input
3 5 4
6 16 7
101 317 64
571 317 420
1699 1409 1334Challenge Output
[(0, 0), (3, 0), (0, 3), (3, 3), (1, 5), (1, 0), (0, 1), (3, 1), (0, 4)]
no solution
[(0, 0), (101, 0), (0, 101), ... (0, 280), (101, 280), (64, 317)]
[(0, 0), (571, 0), (254, 317), ... (571, 166), (420, 317)]
[(0, 0), (1699, 0), (290, 1409), ... (0, 1044), (1699, 1044), (1334, 1409)]Credit
This challenge was suggested by user /u/itah! If you have an idea for a challenge please share it on /r/dailyprogrammer_ideas.
7
u/[deleted] Aug 31 '17
C++
Well, this almost feels like cheating... But if you just fill one bucket, fill it to the other and empty the other if it's full, it will get to the solution eventually:
5
u/skeeto -9 8 Aug 31 '17 edited Aug 31 '17
C using a custom "spiral" technique to search for the solution. First I learned how to mathematically solve the problem here. It turns the problem into the form:
Where you solve for
xandy. That's one equation, two unknowns, so there are either no solutions or infinitely many solutions (I think?). A nice solution is one wherexandyare small. That search space is essentially a 2D spiral, so I dug out my old spiral code to spiral through possiblexandy.Once it finds a solution, it just needs to follow some rules to walk it to the actual solution: fill the positive bucket when it's empty. Empty the negative bucket when it's full. Otherwise transfer from positive to negative.
1
u/ethorad Aug 31 '17
The description states that m, n are coprime but the second set of input has them as 6 and 16, which aren't coprime. Was that deliberate?
3
u/mapio Sep 01 '17
You can find a complete mathematical treatment of this here https://homes.di.unimi.it/~santini/_downloads/ridsi-212-98.pdf in case you are curious :-)
2
u/Specter_Terrasbane Aug 31 '17 edited Sep 01 '17
Python 2
Edit: Realized I had made an inefficient design decision: I was re-doing calculations that didn't need to be, by iterating through everything in
attemptseach loop ... should have only been calculating off anything new that had been added toattemptsin the prior loop. Addedlast_progressto track what was accomplished in the prior iteration, and now instead of taking multiple seconds to calculate all five challenge tests, the whole thing finishes virtually immediately. :)Test Code
Output
2
u/curtmack Aug 31 '17
Common Lisp
Straightforward BFS implementation.
2
u/gabyjunior 1 2 Aug 31 '17 edited Sep 01 '17
C
Using the technique shown in below pictures, hosted on a french website (link to the full article).
It consists in drawing the path of a ball that bounces on the edges of a parallepipedic billiard table, until the target is reached.
There are two paths possible, starting from (0, 0), the pictures are matching the first challenge input (bucket1 = 3, bucket2 = 5, target = 4):
Path 1
Path 2
The program implements a BFS that follows both paths simultaneously until the target is reached by one of them, or all states have been visited.
It visits (m+n)*2 states in the worst case (only states that have either one bucket full and/or one empty are visited).
The program requires the smallest bucket capacity to be entered first.
2
u/olzd Sep 01 '17
Dyalog APL:
1
u/[deleted] Aug 31 '17 edited Aug 31 '17
C, simple algorithm, doesn't search for the fastest path.
Number of steps for each example:
1
u/MattieShoes Aug 31 '17 edited Aug 31 '17
C++ brute force, no math or logic short cuts other than pruning transpositions to already-found states
smooshes state of buckets into a 64 bit integer as a key
tree[key] contains key of parent node
expands tree in memory
stops when it finds the target value
When all leaves are transpositions, the tree is fully expanded and it knows there are no solutions
Is damn near instant
requires
-std=c++11because I used lazy struct initializationshould find the shortest solution (one of them anyway)
1
u/cheers- Aug 31 '17 edited Sep 06 '17
Javavscript (Node)
Edit: fixed a bug
A BFS solution that computes the shortest path and doesn't add to the queue nodes already visited.
1
u/[deleted] Aug 31 '17 edited Sep 01 '17
Ruby
I noted the solution pattern in the examples and implemented that as best I could, with the first two spots of an array serving as virtual 'buckets'. Feedback and criticism is welcome!
Edit: Didn't realize only m and n needed to be coprime. Removed 2 lines of extraneous code.
Output
1
u/octolanceae Sep 01 '17
** Python3 **
I did a total hack job on this one I think. I felt like making a class for readability sake. It was easier for me to keep track of moves and better illustrated the solution (to me). Not as elegant as some of the other solutions for sure.
Feedback welcome.
** Output **
1
u/AZXXZAZXQ Sep 01 '17
Not the best or most efficient, but I think it works for the challenge outputs.
1
u/[deleted] Sep 01 '17 edited Sep 01 '17
Python 3
BFS from previous states to each of the six next states until a bucket contains the target volume. Uses a dictionary to both keep track of the path and avoid repeats.
** Edit, realized this uses BFS
1
u/srandtimenull Sep 04 '17 edited Sep 04 '17
C
Well, I'm a lot late, but I managed to write a super-short C program.
I "cheated" taking input arguments and include files via gcc command line:
I felt good with these, since they're mandatory and a costant offset.
I could have cheated more defining the
printfmacro...but that sounded like real cheat.No fancy math involved, just the following algo:
Code follows, 159 characters (newline included):
EDIT: of course everyone is challenged to do better than me...I know it could be shorter.
1
u/Scroph 0 0 Sep 05 '17 edited Sep 05 '17
D solution that implements the algorithm mentioned by /u/gabyjunior, or a naive version of it. Experimenting with the experimental -betterC flag of dmd. Because of this, I had to create a Vector struct (no leaks according to valgrind) :
1
u/[deleted] Sep 08 '17 edited Sep 08 '17
Late solution as I got caught up on the BFS implementation. It's pretty bloated,slow, and messy so I will clean it up shortly. Thanks for helping me out for those who answered my questions!
0
u/[deleted] Aug 31 '17
[deleted] — view deleted comment
0
u/[deleted] Aug 31 '17
[deleted] — view deleted comment