Last active
August 29, 2015 14:02
-
-
Save mikolalysenko/d32cfb2c74dcdc2746ef to your computer and use it in GitHub Desktop.
Goat, Wolf, Lion Joke
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
nice! line 33 could also be
.map(Number)