Statues In the Garden

Run Settings
LanguageJavaScript
Language Version
Run Command
/** * This is a rough program that simulates the working out of a logic problem * * The Premise of the Logic Problem as follows:- * * Two Prisoners are trapped in their individual cells, * with no means of communicating with each other. * Each of the prisoners can see through their cell window * a number of status. * Prisoner A sees 12 statues * Prisoner B sees 8 statues * * Each day the Warden will ask the prisoners, * in the same order, * how many statues do the two prisonser see combined? * 18 or 20 statues? * If ONE of the prisoners answer correctly, * BOTH prisoners will be released. * If ONE of the prisoners ansers incorrectly, * they will be imprisoned for the remainder of their lives. * Prisoners may choose to answer or pass. * * How would the two Prisoners devise a means of arriving at the answer? * **/ var query = [18,20]; //The Question posed by the Warden var min = null; // The collective implied minimum var max = null; // The collective implied maximum var minVisible = 12; //The number of statues Prisoner A sees var maxVisible = 8; // The number of statues Prisoner B sees var steps = 1; // The number of Days that has passed /** * Given a choice of two numbers, * what are the complementary values to arrive at the two number * given a target number * * Eg, * given [18, 20] * and a target number of 8 * the possible complementary values to arrive at 18 and 20 are * [10, 12] **/ function getPossibleValues(target, query ){ return query .map(item=>item-target) .filter(item=>item>=0); } /** * Prisoner A's deductive logic and implying the Minimum Value **/ function minimize( query, min, max, visible ){ min = min || 0; console.log( "Maximize : " ); console.log( getPossibleValues(min,query)); return Math.min.apply(null,getPossibleValues(min,query)); } /** * Prisoner B's deductive logic and implying the Maximum Value **/ function maximize( query, min, max ){ console.log( "Maximize :"); min = min || 0; var valueFilter = item=>item<=max; for( var i = min; i <= max; i++ ){1 var possibleValues = getPossibleValues(i,query).filter(valueFilter); console.log(i, "=>", possibleValues); if( possibleValues.length > 1 ){ console.log( "First Number with no definite solutions : ", i, " => ", possibleValues ); return i; } } return null; } /** * Simulates the daily routine where the Warden asks the Prisoners **/ function step(){ console.log("Day ", steps ); max = minimize(query, min, max, minVisible); min = maximize(query, min, max, maxVisible); console.log("Min : ", min, "; Max : ", max, "; MinVisible : ", minVisible, "; MaxVisible : ", maxVisible ); steps++; } /** * Run the simulation until a solution is reached **/ do{ step(); } while( min !== maxVisible && max !== minVisible || steps >= 1000 ); if( steps < 1000 ){ console.log( "Day " + steps++ ); console.log( "Prisoner arrives at the answer ", min+max ); } else { console.log( 1000, " days has passed - both prisoners have given up" ); }
Editor Settings
Theme
Key bindings
Full width
Lines