This commit is contained in:
JOLIMAITRE Matthieu 2024-05-02 16:43:00 +02:00
parent 2fd201c380
commit 27abf00669
3 changed files with 13 additions and 19 deletions

View file

@ -1,11 +1,11 @@
import { assertExists } from "https://deno.land/std@0.224.0/assert/assert_exists.ts";
import { assertEquals } from "https://deno.land/std@0.224.0/assert/assert_equals.ts";
import { Dict } from "../dict.ts";
import { GuessResult, Info } from "../game/game.ts";
import { info_of_guess } from "../game/simulator.ts";
import { Awaitable, enumerate, first, zip } from "../utils.ts";
import { Awaitable, enumerate, first, range, zip } from "../utils.ts";
import { Guessing } from "./guesser.ts";
import { range } from "../utils.ts";
export class ReducingGuesser implements Guessing {
length;
@ -19,13 +19,13 @@ export class ReducingGuesser implements Guessing {
}
public declare_properties() {
return ["candidates"];
return ["candidates", "best score"];
}
public async guess(try_: (guess: string, candidates: number) => Awaitable<GuessResult>) {
if (this.candidates.size === 1) return await try_(first(this.candidates)!, this.candidates.size);
const guess = get_word_with_smallest_cuts(this.candidates, this.words);
const result = await try_(guess, this.candidates.size);
public async guess(try_: (guess: string, candidates: number, score: number) => Awaitable<GuessResult>) {
if (this.candidates.size === 1) return await try_(first(this.candidates)!, this.candidates.size, 1);
const [guess, score] = get_word_with_smallest_cuts(this.candidates, this.words);
const result = await try_(guess, this.candidates.size, score);
if (result.kind === "success") return result;
this.learn(guess, result.informations);
return null;
@ -117,21 +117,16 @@ function word_cuts_alt(word: string, dict: Set<string>) {
}
function get_word_with_smallest_cuts(candidates: Set<string>, dict: Set<string>) {
const total = dict.size;
let best = null as null | [string, number];
for (const [index, candidate] of enumerate(dict.values())) {
for (const candidate of dict.values()) {
const cuts = word_cuts_alt(candidate, candidates);
let max_part_size = 0;
for (const cut of cuts) for (const part of cut) if (part.size > max_part_size) max_part_size = part.size;
if (best === null) best = [candidate, max_part_size];
else if (max_part_size < best[1]) best = [candidate, max_part_size];
// const frac = Math.floor(100 * index / total);
// console.log({ index, total, frac, candidate });
}
assertExists(best);
// console.log("Got", best);
return best[0];
return best;
}
Deno.test("test_word_cuts", () => {
@ -149,7 +144,7 @@ Deno.test("test_word_cuts", () => {
});
Deno.test("test_smallest_cuts", () => {
const best = get_word_with_smallest_cuts(
const [best] = get_word_with_smallest_cuts(
new Set(["aa", "ab", "ac", "ba", "bc"]),
new Set(["ab", "ba"]),
);