Public
Edited
Mar 21, 2023
2 forks
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
viewof firstHoleToCheck = Inputs.radio([0, 1], {
label: "First hole to check",
value: 1
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
state = {
hopButton;

let state = this || {
currentHole: firstHoleToCheck,
rabbitPosition: start,
message: "Not started..."
};

// state.possibleRabbitPositions[firstGuess] = 1;
if (paused) return yield state;

for (; state.currentHole < length; ++state.currentHole) {
await Promises.delay(delay);

// Did we find the bunny?
if (state.rabbitPosition === state.currentHole) {
state.message = `Found bunny at hole ${state.currentHole} ⛳🐰`;
return yield state;
}

/* attempt to keep track of all possible rabbit positions but blows up as numbers become very large
for (let i = 0; i < length; i++) {
const rabbitsPossiblyAtPos = d3.range(state.possibleRabbitPositions[i]);

if (i === 0) {
for (const d of rabbitsPossiblyAtPos) {
state.possibleRabbitPositions[1] += 1;
}
} else if (i === length - 1) {
for (const d of rabbitsPossiblyAtPos) {
state.possibleRabbitPositions[length - 2] += 1;
}
} else {
for (const d of rabbitsPossiblyAtPos) {
const hop = randomHop();
const nextHole = i + hop;
state.possibleRabbitPositions[nextHole] += 1;
} // count rabbit can be possibly be in this hole
}
}*/

// If not, bunny hops
if (state.rabbitPosition === length - 1) {
state.rabbitPosition -= 1;
} else if (state.rabbitPosition === 0) {
state.rabbitPosition += 1;
} else {
const hop = randomHop();
state.rabbitPosition += hop;
}

state.message =
state.currentHole === length - 1
? `Last hole (${state.currentHole}) and bunny not found 🐇`
: `Bunny not found in hole ${state.currentHole} 🕳️`;

yield state;
}
}
Insert cell
randomHop = () => (Math.random() > 0.5 ? 1 : -1)
Insert cell
holes = {
const newHoles = Array.from({ length }).fill(false);
newHoles[state.rabbitPosition] = true;

return newHoles;
}
Insert cell
getBorderStyle = (h, i) =>
h && state.currentHole === i
? "3px solid hotpink"
: h
? "2px solid crimson"
: state.currentHole === i
? "2px solid black"
: "1px solid hsl(0, 0%, 90%)"
Insert cell
rabbitStartParity = checkParity(start)
Insert cell
firstCheckParity = checkParity(firstHoleToCheck)
Insert cell
checkParity = (number) => (number % 2 ? "odd" : "even")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more