deno.land / x / masx200_leetcode_test@10.6.5 / guess-the-word / index.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Master } from "./Master.ts";
function findSecretWord(wordlist: string[], master: Master) { const N = wordlist.length; const H: number[][] = new Array(N).fill(0).map(() => Array(N).fill(0)); for (let i = 0; i < N; ++i) { for (let j = i; j < N; ++j) { const match = getMatch(wordlist[i], wordlist[j]);
H[i][j] = H[j][i] = match; } } let possible = [...wordlist.keys()]; const seen = new Set<number>(); while (possible.length) { const ansguesses: number[] = solve(possible, H); const j = ansguesses[Math.floor(Math.random() * ansguesses.length)];
seen.add(j); const d = master.guess(wordlist[j]); if (d === 6) return; if (d >= 0) { possible = possible.filter((i) => !seen.has(i) && d === H[i][j] ); } else { return; } }}
function solve( possible: number[], H: number[][],) { const ansgrps: [number, number][] = []; for (const i of possible) { const groups: Array<number> = Array(7).fill(0); for (const j of possible) { if (j !== i) { groups[H[i][j]]++; } } const maxgroup = groups.reduce((a, v) => a > v ? a : v); ansgrps.push([maxgroup, i]); } const minlen = Math.min(...ansgrps.map((a) => a[0]));
const ansguesses: number[] = ansgrps.filter((a) => a[0] === minlen).map( (a) => a[1], ); return ansguesses;}
function getMatch(v: string, w: string) { return Array.prototype.filter.call( v, (c, k) => w[k] === c, ).length;}
export default findSecretWord;
masx200_leetcode_test

Version Info

Tagged at
a year ago