loading...

Daily Challenge #285 - Jewel Thief

thepracticaldev profile image dev.to staff ・1 min read

Word on the street is that you claim to be a safe-cracking jewel thief. But are you?

Let's test your safe-cracking abilities. To prove you are a real thief, you need to crack the combination of the safe, then open it and tell me the value of the jewels inside!

The safe object has two methods:

unlock(combination) returns a string. click means the first part of the combination is correct, click click for the first two parts, click click click means all three are correct.

open() returns an integer containing the value of the safe's contents.

The safe combination format is made up of 3 dial spins:

  • Spin the dial left L or right R
  • Numbers on the dial 00 - 99
  • Each part of the combination is separated by -

Let's see how well you know how to crack safes. Good luck!


This challenge comes from dinglemouse on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Posted on by:

thepracticaldev profile

dev.to staff

@thepracticaldev

The hardworking team behind dev.to ❤️

Discussion

markdown guide
 
const crackSafe = safe => {
  const combo = ['L0', 'L0', 'L0'];
  const directions = ['L', 'R'];

  const try = (stage, combo) => {
    const comboWorked = new Array(stage + 1).fill('click').join(' ');

    for (let i = 0; i < 100; i++) {
      directions.forEach(dir => {
        combo[stage] = `${dir}${i}`;
        if safe.unlock(combo.join('-')) === comboWorked {
          return true;
        }
      });
    }
    return false;
  };

  for (let stage = 0; stage < 3; stage++) {
    if (!try(stage, combo)) {
      return -1;
    }
  }

  return safe.open(combo);
};

Not very dynamic but should solve for the problem as written!