Last active
September 18, 2020 12:18
-
-
Save kyleplo/3ae5dc6aac5614f64db207e2e2ea555c to your computer and use it in GitHub Desktop.
Watermelons in the Desert You live in a desert oasis and grow miniature watermelons that are worth a great deal of money, if you can get them to the market 15 kilometers away across the desert. Your harvest this year is 45 melons, but you have no way to get them to the market, except to carry them across the desert. You have a backpack that hold…
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 dropped = [45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | |
var log = []; | |
var result = 15; | |
var pos = 0; | |
var held = 0; | |
var error = false; | |
var attempt = 0; | |
var done = false; | |
var doerrors = false;// set to true for error messages | |
function reset(){ | |
dropped = [45, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; | |
log = []; | |
result = 15; | |
pos = 0; | |
held = 0; | |
error = false; | |
attempt = 0; | |
if(!doerrors){ | |
console.error = function (n){} | |
}; | |
} | |
function pickup(num){ | |
if(num > dropped[pos]){ | |
console.error("You cannot pick up "+ num + " melons as there are only " + dropped[pos] + " at position "+ pos); | |
error = true; | |
return false; | |
}; | |
if((held + num) > 15){ | |
console.error("You cannot pick up "+ num + " melons as you are already holding " + held + " melons"); | |
error = true; | |
return false; | |
}; | |
held += num; | |
dropped[pos] -= num; | |
log.push("picked up "+ num + " melons"); | |
attempt++; | |
if(attempt > 100){ | |
error = true; | |
}; | |
if(held === 0 && dropped[0] === 0 && pos === 0){ | |
done = true; | |
result = dropped[15]; | |
}; | |
} | |
function drop(num){ | |
if(num > held){ | |
console.error("You cannot drop "+ num + " melons as you only have " + held + " melons"); | |
error = true; | |
return false; | |
}; | |
held -= num; | |
dropped[pos] += num; | |
log.push("dropped " + num + " melons"); | |
attempt++; | |
if(attempt > 100){ | |
error = true; | |
}; | |
if(held === 0 && dropped[0] === 0 && pos === 0){ | |
done = true; | |
result = dropped[15]; | |
}; | |
} | |
function move(num){ | |
if((pos + num) > 15 || (pos + num) < 0){ | |
console.error("You cannot move to position "+ (pos + num) + " as you are at position "+ pos + " and cannot move out of the world"); | |
error = true; | |
return false; | |
}; | |
if((held - num) < 1){ | |
console.error("You cannot move as you don't have any melons"); | |
error = true; | |
return false; | |
}; | |
held -= Math.abs(num); | |
pos += num; | |
log.push("moved " + Math.abs(num) + " miles to position " + pos + " and ate " + Math.abs(num) + " melons"); | |
attempt++; | |
if(attempt > 100){ | |
error = true; | |
}; | |
if(held === 0 && dropped[0] === 0 && pos === 0){ | |
done = true; | |
result = dropped[15]; | |
}; | |
} |
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
// requires above file | |
var record = 15; | |
var recordLog = []; | |
var lt = -1; | |
function random(min, max) { | |
return min + Math.floor(Math.random() * Math.floor(max)); | |
} | |
function start(interval){ | |
reset(); | |
while(!error && !done){ | |
var mt = random(0, 3); | |
if(mt === 0){ | |
pickup(random(1, 15)); | |
}else if(mt === 1){ | |
drop(random(1, held)); | |
}else if(mt === 2){ | |
move(random(1, (15 - pos))); | |
}else if(mt === 3){ | |
move((random(1, (0 + pos))) * -1); | |
}; | |
}; | |
if(done){ | |
if(result > record){ | |
record = result; | |
recordlog = []; | |
for(var i = 0;i < log.length;i++){ | |
recordLog.push(log[i]); | |
}; | |
console.log("new record found: " + record); | |
}; | |
}; | |
lt = setTimeout(function (){start(interval || 500)}, (interval || 500)); | |
} | |
function stop(){ | |
clearTimeout(lt); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment