Skip to content

Instantly share code, notes, and snippets.

@jjylik
Created December 3, 2022 14:33
Show Gist options
  • Save jjylik/fb2fa809ff22f0a1e043dcfaadb28e66 to your computer and use it in GitHub Desktop.
Save jjylik/fb2fa809ff22f0a1e043dcfaadb28e66 to your computer and use it in GitHub Desktop.
Wolf, goat, cabbage blog post
const animals = ['wolf', 'goat', 'cabbage']
boat.init(animals)
boat.load(animals[0])
boat.moveToNext()
boat.moveToNext()
//The next step is to load the cabbage
boat.load(animals[2])
// Crash!
// The actions need to happen at a certain order.
// Otherwise the internal state of the boat object will corrupt.
// See this issue and discussion why an invalid state is possible using the transport library
// https://github.com/folklore-riddle/transport/issues/12
// Further documentation https://en.wikipedia.org/wiki/Wolf,_goat_and_cabbage_problem
function transferAnimals() {
// TODO There is a new version of the boat API in the making https://github.com/folklore-riddle/transport
// When the version is released, this function will be updated to use the new version.
boat.initialize(animals);
// `boat.load` is a bit awkward, but we have contributed to a PR open which simplifies it.
// Please see https://github.com/folklore-riddle/transport/pull/22
boat.load(GOAT_INDEX);
boat.moveToNextBank();
boat.unload();
boat.moveToNextBank();
boat.load(WOLF_INDEX);
boat.moveToNextBank();
boat.unload();
// NOTE! There is an issue in the boat state which causes the GOAT index be unloaded if we dont load the GOAT here.
// Issue https://github.com/folklore-riddle/transport/issues/53
boat.load(GOAT_INDEX);
boat.moveToNextBank()
boat.unload()
boat.load(GABBAGE_INDEX)
boat.moveToNextBank()
boat.unload()
boat.moveToNextBank();
boat.load(GOAT_INDEX)
boat.moveToNextBank()
boat.unload()
}
function transfer() {
boat.initialize(animals)
boat.load(1)
boat.moveToNextBank()
boat.unload()
boat.moveToNextBank()
boat.load(0)
boat.moveToNextBank()
boat.unload()
boat.load(1)
boat.moveToNextBank()
boat.unload()
boat.load(2)
boat.moveToNextBank()
boat.unload()
boat.moveToNextBank();
boat.load(1)
boat.moveToNextBank()
boat.unload()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment