Skip to content

Instantly share code, notes, and snippets.

@thawkin3
Created August 24, 2022 18:09
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 thawkin3/d9b650b119e85626c839e64f5de5aab9 to your computer and use it in GitHub Desktop.
Save thawkin3/d9b650b119e85626c839e64f5de5aab9 to your computer and use it in GitHub Desktop.
export const findKeyInBox = (box) => {
const pile = [];
pile.push(box);
while (pile.length > 0) {
const currentBox = pile.pop();
console.log(`Looking in Box ${currentBox.id}`);
for (let i = 0; i < currentBox.contents.length; i++) {
if (currentBox.contents[i].type === 'key') {
console.log(`Found the key in Box ${currentBox.id}!`);
return currentBox.id;
}
if (currentBox.contents[i].type === 'box') {
console.log(
`Found Box ${currentBox.contents[i].id} in Box ${currentBox.id}`
);
pile.push(currentBox.contents[i]);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment