Skip to content

Instantly share code, notes, and snippets.

@mikolalysenko
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikolalysenko/d32cfb2c74dcdc2746ef to your computer and use it in GitHub Desktop.
Save mikolalysenko/d32cfb2c74dcdc2746ef to your computer and use it in GitHub Desktop.
Goat, Wolf, Lion Joke
//Solves that silly goat/wolf/lion problem
//
// From this challenge: http://unriskinsight.blogspot.dk/2014/06/fast-functional-goats-lions-and-wolves.html
//
// Example performance on input: 1017 1055 1006
//
// C++: 31.695 s
// JS: 0.059 s
//
// Checkmate C++?
function findAB(l, g, w) {
var d = g + w
if(d % 2 !== 0) {
return -1
}
var c = (d / 2)|0
var ab = c - g
return l - Math.abs(ab) + c
}
function solve(pop) {
for(var i=0; i<3; ++i) {
var x = findAB(pop[i], pop[(i+1)%3], pop[(i+2)%3])
if(x >= 0) {
var res = [0,0,0]
res[i] = x
console.log(res)
}
}
}
var args = process.argv.slice(2).map(function(x) { return +x })
solve(args)
@stagas
Copy link

stagas commented Jun 6, 2014

nice! line 33 could also be .map(Number)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment