Given a 5 quart jug, a 3 quart jug, and unlimited supply of water, can you measure exactly 4 quarts? (The jugs are irregularly shaped, so you can't eyeball half the volume or anything like that.)
The solution involves pouring water between the jugs.
I wanted to see if I could solve this puzzle with Alloy. My first attempt (specific.als) hardcodes the two jugs as named relations. It solves the puzzle (finds a counterexample) in about 500ms.
My second attempt (generic.als) is coded to allow for an arbitrary number of jugs and different sizes. It solves the puzzle in about 2000ms.
I'm interested in feedback on the code for both. In particular:
-
Is this idiomatic Alloy?
-
When filling or emptying a single jug, is there a nicer way to express that "other jugs are unchanged" than
all r: Jug - j | r.(s'.jugAmounts) = r.(s.jugAmounts)
? -
Why does the generic code take 4 times as long to solve? Is there a way to make it faster?