Last active
May 18, 2020 02:41
-
-
Save f1729/7894d671494d70dd399f673cccd84c2a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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