|
| 1 | +/** |
| 2 | + * @name River crossing puzzle |
| 3 | + * @description This implements the classical puzzle where a man is trying to |
| 4 | + * ferry a goat, a cabbage, and a wolf across a river. |
| 5 | + * His boat can only take himself and at most one item as cargo. |
| 6 | + * His problem is that if the goat is left alone with the cabbage, |
| 7 | + * it will eat it. |
| 8 | + * And if the wolf is left alone with the goat, it will eat it. |
| 9 | + * How does he get everything across the river? |
| 10 | + * |
| 11 | + * Note: Parts of this QL file are included in the corresponding .rst file. |
| 12 | + * Make sure to update the line numbers if you change anything here! |
| 13 | + */ |
| 14 | + |
| 15 | +/** A possible cargo item. */ |
| 16 | +class Cargo extends string { |
| 17 | + Cargo() { |
| 18 | + this = "Nothing" or |
| 19 | + this = "Goat" or |
| 20 | + this = "Cabbage" or |
| 21 | + this = "Wolf" |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/** One of two shores. */ |
| 26 | +class Shore extends string { |
| 27 | + Shore() { |
| 28 | + this = "Left" or |
| 29 | + this = "Right" |
| 30 | + } |
| 31 | + |
| 32 | + /** Returns the other shore. */ |
| 33 | + Shore other() { |
| 34 | + this = "Left" and result = "Right" |
| 35 | + or |
| 36 | + this = "Right" and result = "Left" |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +/** Renders the state as a string. */ |
| 41 | +string renderState(Shore manShore, Shore goatShore, Shore cabbageShore, Shore wolfShore) { |
| 42 | + result = manShore + "," + goatShore + "," + cabbageShore + "," + wolfShore |
| 43 | +} |
| 44 | + |
| 45 | +/** A record of where everything is. */ |
| 46 | +class State extends string { |
| 47 | + Shore manShore; |
| 48 | + Shore goatShore; |
| 49 | + Shore cabbageShore; |
| 50 | + Shore wolfShore; |
| 51 | + |
| 52 | + State() { this = renderState(manShore, goatShore, cabbageShore, wolfShore) } |
| 53 | + |
| 54 | + /** Returns the state that is reached after ferrying a particular cargo item. */ |
| 55 | + State ferry(Cargo cargo) { |
| 56 | + cargo = "Nothing" and |
| 57 | + result = renderState(manShore.other(), goatShore, cabbageShore, wolfShore) |
| 58 | + or |
| 59 | + cargo = "Goat" and |
| 60 | + result = renderState(manShore.other(), goatShore.other(), cabbageShore, wolfShore) |
| 61 | + or |
| 62 | + cargo = "Cabbage" and |
| 63 | + result = renderState(manShore.other(), goatShore, cabbageShore.other(), wolfShore) |
| 64 | + or |
| 65 | + cargo = "Wolf" and |
| 66 | + result = renderState(manShore.other(), goatShore, cabbageShore, wolfShore.other()) |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Holds if the state is safe. This occurs when neither the goat nor the cabbage |
| 71 | + * can get eaten. |
| 72 | + */ |
| 73 | + predicate isSafe() { |
| 74 | + // The goat can't eat the cabbage. |
| 75 | + (goatShore != cabbageShore or goatShore = manShore) and |
| 76 | + // The wolf can't eat the goat. |
| 77 | + (wolfShore != goatShore or wolfShore = manShore) |
| 78 | + } |
| 79 | + |
| 80 | + /** Returns the state that is reached after safely ferrying a cargo item. */ |
| 81 | + State safeFerry(Cargo cargo) { result = this.ferry(cargo) and result.isSafe() } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns all states that are reachable via safe ferrying. |
| 85 | + * `path` keeps track of how it is achieved. |
| 86 | + * `visitedStates` keeps track of previously visited states and is used to avoid loops. |
| 87 | + */ |
| 88 | + State reachesVia(string path, string visitedStates) { |
| 89 | + // Trivial case: a state is always reachable from itself. |
| 90 | + this = result and |
| 91 | + visitedStates = this and |
| 92 | + path = "" |
| 93 | + or |
| 94 | + // A state is reachable using pathSoFar and then safely ferrying cargo. |
| 95 | + exists(string pathSoFar, string visitedStatesSoFar, Cargo cargo | |
| 96 | + result = this.reachesVia(pathSoFar, visitedStatesSoFar).safeFerry(cargo) and |
| 97 | + // The resulting state has not yet been visited. |
| 98 | + not exists(int i | i = visitedStatesSoFar.indexOf(result)) and |
| 99 | + visitedStates = visitedStatesSoFar + "/" + result and |
| 100 | + path = pathSoFar + "\n Ferry " + cargo |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +/** The initial state, where everything is on the left shore. */ |
| 106 | +class InitialState extends State { |
| 107 | + InitialState() { this = renderState("Left", "Left", "Left", "Left") } |
| 108 | +} |
| 109 | + |
| 110 | +/** The goal state, where everything is on the right shore. */ |
| 111 | +class GoalState extends State { |
| 112 | + GoalState() { this = renderState("Right", "Right", "Right", "Right") } |
| 113 | +} |
| 114 | + |
| 115 | +from string path |
| 116 | +where any(InitialState i).reachesVia(path, _) = any(GoalState g) |
| 117 | +select path |
| 118 | + |
0 commit comments