ktane-solver
Version:
A library made to solve KTaNE modules
67 lines (56 loc) • 1.77 kB
JavaScript
const CHOICES = [
"ash",
"at",
"b",
"balloon",
"black star",
"butt",
"c with dot",
"kitty",
"copyright",
"curly h",
"double k",
"dragon",
"euro",
"fork",
"half three",
"lambda",
"letter n",
"lightning",
"omega",
"paragraph",
"pig tail",
"question",
"reverse c",
"six",
"smiley",
"stitch",
"white star"
]
const SOLUTIONS = [
["balloon", "at", "lambda", "lightning", "kitty", "curly h", "reverse c"],
["euro", "balloon", "reverse c", "pig tail", "white star", "curly h", "question"],
["copyright", "butt", "pig tail", "double k", "half three", "lambda", "white star"],
["six", "paragraph", "b", "kitty", "double k", "question", "smiley"],
["fork", "smiley", "b", "c with dot", "paragraph", "dragon", "black star"],
["six", "euro", "stitch", "ash", "fork", "letter n", "omega"]
]
class Keypad {
static solve(symbols) {
symbols = symbols.map(v => v.toLowerCase());
if (symbols.length !== 4) {
throw "Expected 4 symbols, got " + symbols.length
}
let checker = (arr, target) => target.every(v => arr.includes(v));
for (let x = 0; x < 6; x++) {
if (checker(SOLUTIONS[x], symbols)) {
return SOLUTIONS[x].filter(x => symbols.includes(x))
}
}
for (let x of symbols) {
if (!CHOICES.includes(x)) throw "Unexpected symbol" + (symbols.length > 1 ? "s" : "") + ", got " + symbols.filter(x => !CHOICES.includes(x))
else if (symbols.length === 4) throw "No solution found for: " + symbols
}
}
}
export default Keypad