Last active
December 30, 2019 21:06
-
-
Save waltz/2b5bef6db7ce59aea7c60970842d9388 to your computer and use it in GitHub Desktop.
This file contains 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 findWord = rules => { | |
let word = rules.join('').replace(/\>/g, '').split('').filter((v, i, a) => a.indexOf(v) === i); | |
console.log('base characters:', word); | |
let done = false; | |
while (!done) { | |
done = true; | |
for (let i = 0; i < rules.length; i++) { | |
let rule = rules[i]; | |
let firstLetter = rule[0]; | |
let secondLetter = rule[2]; | |
if (word.indexOf(firstLetter) > word.indexOf(secondLetter)) { | |
console.log('rule broken:', firstLetter, word.indexOf(firstLetter), secondLetter, word.indexOf(secondLetter)); | |
word.splice(word.indexOf(firstLetter), 1); | |
word.splice(word.indexOf(secondLetter), 0, firstLetter); | |
done = false; | |
} | |
console.log('current form:', word.join('')); | |
} | |
} | |
return word.join(''); | |
} | |
console.log(findWord(["U>N", "G>A", "R>Y", "H>U", "N>G", "A>R"])); // HUNGARY | |
console.log(findWord(["I>F", "W>I", "S>W", "F>T"])); // SWIFT | |
console.log(findWord(["R>T", "A>L", "P>O", "O>R", "G>A", "T>U", "U>G"])); // PORTUGAL | |
console.log(findWord(["W>I", "R>L", "T>Z", "Z>E", "S>W", "E>R", "L>A", "A>N", "N>D", "I>T"])); // SWITZERLAND |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment