1 条题解
-
5
这里使用包 wsgs 的源码,略微修改以下即可。
直接提交会发现 TLE,所以我们特判前两步,而由于此包非常优秀所以仍可在 步内完成任务。
var initwordlist = (str) => { var res = new Array(); str.split('\n').forEach(word => { var tmp = ''; for (var i = 0; i < word.length; i++) if ("qwertyuiopasdfghjklzxcvbnm".includes(word[i])) tmp += word[i]; res.push(tmp); }); return res; } const defaultWords = { possible: initwordlist(require('fs').readFileSync('valid_words.txt', 'utf8')), support: initwordlist(require('fs').readFileSync('allowed_guesses.txt', 'utf8')) }; const mainChecker = (player, answer) => { var result = new Array(0, 0, 0, 0, 0); for (var i = 97; i < 123; i++) { var ids = new Array(), ansids = new Array(); for (var j = 0; j < 5; j++) { if (answer.charCodeAt(j) == i && player.charCodeAt(j) == i) { result[j] = 3; continue; } if (answer.charCodeAt(j) == i) ansids.push(j); if (player.charCodeAt(j) == i) ids.push(j); } for (var j = 0; j < ansids.length && j < ids.length; j++) result[ids[j]] = 1 + ((ansids[j] != ids[j] - 1 && ansids[j] != ids[j] + 1) ? 1 : 0); for (var j = ansids.length; j < ids.length; j++) result[ids[j]] = 0; } return result; } const std3 = (player, answer) => { if (player == answer) return "correct"; var tmp = mainChecker(player, answer); var result = ""; tmp.forEach(x => result += " YYG"[x]); return result; }; const defaultRule = { default: { checker: std3, name: "Standard Mode", validator: (res) => typeof res == 'string' && /^[ GY]{5}$/.test(res) } }; class WSGS { #rule; #words; #possible; #records; constructor() { this.#rule = defaultRule.default; this.#words = defaultWords; this.#possible = defaultWords.possible; this.#records = new Array(); } getRule() { return this.#rule; } setRule(name, checker, validator) { this.#rule = { name, checker, validator }; } getWords() { return this.#words; } setWords(support, possible) { this.#words = { support, possible }; } getPossibleWords() { return this.#possible; } setPossibleWords(possibleWords) { this.#possible = possibleWords; } getRecords() { return this.#records; } cleanRecords() { this.#records = new Array(); this.#possible = this.#words.possible; } #listPossibleWords() { var newPossible = new Array(); for (var word of this.#possible) { var flag = true; for (var record of this.#records) if (this.#rule.checker(record.word, word) != record.result) flag = false; if (flag) newPossible.push(word); } this.#possible = newPossible; } setLastResult(result) { if (this.#records.length && (result == 'correct' || this.#rule.validator(result))) { this.#records[this.#records.length - 1].result = result; this.#listPossibleWords(); } } guess(word) { if (this.#words.support.includes(word)) this.#records.push({ word, result: "" }); } getNextGuesses() { if (this.#possible.length == 0) return []; if (this.#possible.length == 1) return [{ word: this.#possible[0], score: 1 }]; var Result = new Array(); for (var guess of this.#words.support) { var results = {}; for (var answer of this.#possible) { var result = this.#rule.checker(guess, answer); if (!results[result]) results[result] = 0; results[result]++; } var score = 0, total = 0; for (var result in results) { var p = this.#possible.length / results[result]; score += Math.log2(p) / p; total += results[result]; } Result.push({ word: guess, score }); } Result.sort((x, y) => y.score - x.score); return Result; } getNextGuess() { var result = this.getNextGuesses(); if (result.length) return result[0].word; else return "ERROR"; } } var wordle = new WSGS(); var readline = require('readline'); const rl = readline.createInterface({ input: process.stdin, output: process.stdout }) console.log('weary'); wordle.guess('weary'); rl.on('line', (line) => { var tmp = ""; for (var i = 0; i < line.length; i++) if ("GYW".includes(line[i])) tmp += line[i] == 'W' ? ' ' : line[i]; wordle.setLastResult(tmp); var nxt; if (wordle.getRecords().length == 1) nxt = 'pilot'; else nxt = wordle.getNextGuess(); console.log(nxt); wordle.guess(nxt); });
- 1
信息
- ID
- 243
- 时间
- 3000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- (无)
- 递交数
- 65
- 已通过
- 4
- 上传者