Last active
May 8, 2016 03:21
two-prisoners-count-trees
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
var options = { | |
smaller: 18, | |
bigger: 20, | |
}; | |
var northInferSouthMin; | |
var southInferNorthMax; | |
function northAnswer (trees) { | |
northInferSouthMin = options.bigger - southInferNorthMax; | |
// console.log(' northInferSouthMin', northInferSouthMin); | |
var sumMin = northInferSouthMin + trees; | |
if (sumMin >= options.bigger) { | |
// console.log(' North answer', options.bigger); | |
return options.bigger; | |
} else { | |
// console.log(' North don\'t answer'); | |
return null; | |
} | |
} | |
function southAnswer (trees) { | |
southInferNorthMax = options.smaller - northInferSouthMin; | |
// console.log(' southInferNorthMax', southInferNorthMax); | |
var sumMax = southInferNorthMax + trees; | |
if (sumMax <= options.smaller) { | |
// console.log(' South answer', options.smaller); | |
return options.smaller; | |
} else { | |
// console.log(' South don\'t answer'); | |
return null; | |
} | |
} | |
function dayByday(northTrees, southTrees) { | |
var sum = northTrees + southTrees; | |
northInferSouthMin = 0; | |
southInferNorthMax = options.bigger; | |
for (var i = 1; ;i++) { | |
// console.log('Day', i); | |
switch (northAnswer(northTrees)) { | |
case sum: | |
return true; | |
case null: | |
// Nothing happen | |
break; | |
default: | |
return false; | |
} | |
switch (southAnswer(southTrees)) { | |
case sum: | |
return true; | |
case null: | |
// Nothing happen | |
break; | |
default: | |
return false; | |
} | |
} | |
} | |
for (var i = 0; i <= options.smaller; i++) { | |
if (!dayByday(i, options.smaller-i)) { | |
console.log('fail on', i, options.smaller-i); | |
} else { | |
console.log('pass', i, options.smaller-i); | |
}; | |
} | |
for (var i = 0; i <= options.bigger; i++) { | |
if (!dayByday(i, options.bigger-i)) { | |
console.log('fail on', i, options.bigger-i); | |
} else { | |
console.log('pass', i, options.bigger-i); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment