diff --git a/src/painting/__tests__/fill.spec.ts b/src/painting/__tests__/fill.spec.ts new file mode 100644 index 0000000..33e3752 --- /dev/null +++ b/src/painting/__tests__/fill.spec.ts @@ -0,0 +1,46 @@ +import { evaluateWord } from "../../solver-simple"; +import { evaluationToSquare, lineToString } from "../../type"; +import { pickRandom } from "../../utils.array"; +import { getWordList } from "../../wordlist"; +import { colors, fill, Grid } from "../fill"; + +it("fill", async () => { + const words = await getWordList(); + + const solution = pickRandom(words); + + const grid = Array.from({ length: 5 }, () => + Array.from({ length: 5 }, () => pickRandom(colors as any)) + ); + + const moves = fill(grid as any, solution, words); + + const lines = moves.map((w) => evaluateWord(solution, w)); + + const grid0 = lines.map((l) => + l.map((x) => evaluationToSquare(x.evaluation)) + ); + + console.log( + ( + (100 * getScore(grid as any, grid0)) / + (grid.length * grid0.length) + ).toFixed(2) + "%", + "\n", + solution, + moves, + "\n" + + grid.map((l) => l.join("")).join("\n") + + "\n\n" + + grid0.map((l) => l.join("")).join("\n") + ); + + expect(moves).toBeDefined(); +}); + +const getScore = (grid: Grid, grid0: Grid) => { + let s = 0; + for (let i = grid.length; i--; ) + for (let j = grid[0].length; j--; ) s += +(grid[i][j] === grid0[i][j]); + return s; +}; diff --git a/src/painting/fill.ts b/src/painting/fill.ts new file mode 100644 index 0000000..a054168 --- /dev/null +++ b/src/painting/fill.ts @@ -0,0 +1,45 @@ +import { evaluateWord } from "../solver-simple"; +import { Line } from "../type"; + +export const colors = ["🟨", "🟩", "⬜"] as const; +export type Color = typeof colors[number]; + +export const evaluationToColor = (e: Line[number]["evaluation"]) => { + switch (e) { + case "correct": + return "🟩"; + case "present": + return "🟨"; + case "absent": + return "⬜"; + } +}; + +export type Grid = Color[][]; + +const _l: Line = Array.from({ length: 6 }, () => ({ + evaluation: "correct", + letter: "x", +})); +export const fill = (grid: Grid, solution: string, wordList: string[]) => + grid.map((line) => { + let bestScore = 0; + let bestWord = wordList[0]; + + for (const word of wordList) { + evaluateWord(solution, word, _l); + + const score = _l.reduce( + (s, { evaluation }, i) => + s + (evaluationToColor(evaluation) === line[i] ? 1 : 0), + 0 + ); + + if (score > bestScore && solution !== word) { + bestScore = score; + bestWord = word; + } + } + + return bestWord; + });