Skip to content

Instantly share code, notes, and snippets.

@f1729
Last active May 18, 2020 02:41
Show Gist options
  • Save f1729/7894d671494d70dd399f673cccd84c2a to your computer and use it in GitHub Desktop.
Save f1729/7894d671494d70dd399f673cccd84c2a to your computer and use it in GitHub Desktop.
const getHitAndBlow = (entry, mirror, hit, blow) => {
const first = Object.keys(entry)[0];
if (first === undefined) return { hit, blow };
const { [first]: __, ...newEntry } = entry;
if (mirror[first]) {
if (mirror[first] === entry[first]) {
const { [first]: ___, ...newMirror } = mirror;
return getHitAndBlow(newEntry, newMirror, hit + 1, blow);
}
return getHitAndBlow(newEntry, mirror, hit, blow + 1)
}
return getHitAndBlow(newEntry, mirror, hit, blow)
};
const entry = '98756'.split('');
const mirror = '57231'.split('');
const stringToMap = (entry) => {
return entry.reduce((acc, element, index) => {
return {...acc, [element]: index.toString()}
}, {});
};
console.log(getHitAndBlow(stringToMap(entry), stringToMap(mirror), 0, 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment