const readline = require("readline"); const rl = readline.createInterface({ input: process.stdin, output: process.stdout, }); const CMD = { reset: "reset what", "reset board": "board fault", "board add": "where to add", "board delete": "no board at all", "reboot backplane": "impossible", "backplane abort": "install first", unknown: "unknown command", }; const CMD_MAP1 = { b: { a: { c: { k: { p: { l: { a: { n: { e: "backplane", }, }, }, }, }, }, }, o: { a: { r: { d: "board" } } }, }, r: { e: { b: { o: { o: { t: "reboot", }, }, }, s: { e: { t: "reset", }, }, }, }, }; const CMD_MAP2 = { a: { b: { o: { r: { t: "abort", }, }, }, d: { d: "add", }, }, b: { a: { c: { k: { p: { l: { a: { n: { e: "backplane", }, }, }, }, }, }, }, o: { a: { r: { d: "board" } } }, }, d: { e: { l: { e: { t: { e: "delete" } } }, }, }, }; const getValue = (value) => { if (typeof value === "object") { const valueArray = Object.values(value); if (valueArray.length === 1) { return getValue(valueArray[0]); } else { return "unknown"; } } else { return value; } }; rl.on("line", function (line) { const tokens = line.split(" "); const tokensLength = tokens.length; // 处理边界情况 if (tokensLength > 2 || tokensLength === 0) { console.log(CMD.unknown); } else if (tokensLength === 1) { const token = tokens[0]; if ("reset".startsWith(token)) { console.log(CMD.reset); } else { console.log(CMD.unknown); } } else { const token1 = tokens[0]; const token2 = tokens[1]; if ( !["b", "r"].includes(token1[0]) || !["a", "b", "d"].includes(token2[0]) ) { console.log(CMD.unknown); } else { const tokenArray1 = token1.split(""); const tokenArray2 = token2.split(""); const result1 = tokenArray1.reduce((a, b) => a[b], CMD_MAP1); const result2 = tokenArray2.reduce((a, b) => a[b], CMD_MAP2); if (!result1 || !result2) { console.log(CMD.unknown); } else { const keys1 = Object.keys(result1); const keys2 = Object.keys(result2); // console.log(keys1, keys2); if (keys1.length === 2 && keys2.length === 2) { console.log(CMD.unknown); } else if (keys1.length === 2) { const key1_1 = getValue(Object.values(result1)[0]); const key1_2 = getValue(Object.values(result1)[1]); const key2 = getValue(result2); if (key2 === "unknown") { console.log(CMD.unknown); } else { console.log( CMD[key1_1 + " " + result2] || CMD[key1_2 + " " + result2] ); } } else if (keys2.length === 2) { // console.log(Object.values(result2)); const key2_1 = getValue(Object.values(result2)[0]); const key2_2 = getValue(Object.values(result2)[1]); const key1 = getValue(result1); // console.log(key2_1, key2_2); if (key1 === "unknown") { console.log(CMD.unknown); } else { console.log( CMD[key1 + " " + key2_1] || CMD[key1 + " " + key2_2] ); } } else { console.log(CMD[getValue(result1) + " " + getValue(result2)]); } } } } });