index.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { readFileSync } from 'fs';
  2. import { resolve, dirname } from 'path';
  3. import { fileURLToPath } from 'url';
  4. import type { WordBank } from '@wordle/shared';
  5. import { solveEntropy, solveFrequency, solveVowelFirst, solveGreedy } from '@wordle/shared';
  6. const __dirname = dirname(fileURLToPath(import.meta.url));
  7. const MAX_ATTEMPTS = 6;
  8. const SOLVERS = [
  9. { name: 'Entropy', fn: solveEntropy },
  10. { name: 'Frequency', fn: solveFrequency },
  11. { name: 'Vowel First', fn: solveVowelFirst },
  12. { name: 'Greedy', fn: solveGreedy },
  13. ];
  14. function validateBank(bankPath: string) {
  15. console.log(`\n=== ${bankPath} ===`);
  16. const bank: WordBank = JSON.parse(readFileSync(bankPath, 'utf-8'));
  17. const allWords = bank.guessable;
  18. console.log(`Validating ${bank.targets.length} targets with ${allWords.length} words...`);
  19. let ok = 0, unsolvable = 0;
  20. for (const target of bank.targets) {
  21. const results = SOLVERS.map((s) => ({
  22. name: s.name,
  23. ...s.fn(target.word, allWords, MAX_ATTEMPTS),
  24. }));
  25. const passResults = results.filter((r) => r.attempts <= MAX_ATTEMPTS);
  26. if (passResults.length === 0) {
  27. console.log(` UNSOLVABLE: ${target.word}`);
  28. unsolvable++;
  29. } else {
  30. ok++;
  31. }
  32. if ((ok + unsolvable) % 10 === 0) console.log(` ${ok + unsolvable}/${bank.targets.length}...`);
  33. }
  34. console.log(`Done! ${ok} solvable, ${unsolvable} unsolvable.`);
  35. }
  36. const DATA_DIR = resolve(__dirname, '../../data');
  37. validateBank(resolve(DATA_DIR, 'word-bank.json'));
  38. validateBank(resolve(DATA_DIR, 'word-bank-ru.json'));
  39. console.log('\nAll banks validated.');