Skip to content

Instantly share code, notes, and snippets.

@radekstepan
Last active December 31, 2021 21:37
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 radekstepan/acc7b79c4e3f0b243180e9ab4f1fb5b6 to your computer and use it in GitHub Desktop.
Save radekstepan/acc7b79c4e3f0b243180e9ab4f1fb5b6 to your computer and use it in GitHub Desktop.
// https://youtu.be/XEt09iK8IXs?t=1258
let t = 0; // try counter
const s = 10; // search space size
let r = Math.floor(Math.random() * s); // rabbit
// Take a guess.
const guess = g => {
t += 1;
console.log('Try', t, 'in', g, 'rabbit in', r);
if (g === r) {
return true;
}
// Rabbit jumping.
if (!r) {
r += 1;
} else if (r === s) {
r -= 1;
} else {
r += Math.random() > 0.5 ? 1 : -1;
}
}
// Search.
let direction = 1; // forward
let g = 0;
while (true) {
if (guess(g)) {
break;
}
if (g === s && direction === 1) {
direction = -1; // go back
} else {
g += 1 * direction;
}
if (g === 0) {
throw new Error('Sanity check');
}
}
console.log('Found in hole', r, 'on try', t);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment