From 27abf006695476406e66e884bdce63973651bb41 Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Thu, 2 May 2024 16:43:00 +0200 Subject: [PATCH] cleanup --- src/lib/dict.ts | 2 -- src/lib/guesser/reducing.ts | 25 ++++++++++--------------- src/lib/lib.ts | 5 +++-- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/src/lib/dict.ts b/src/lib/dict.ts index 08eb75b..4c5a80b 100644 --- a/src/lib/dict.ts +++ b/src/lib/dict.ts @@ -1,5 +1,3 @@ -import { range } from "./utils.ts"; - export class Dict { words; letters; diff --git a/src/lib/guesser/reducing.ts b/src/lib/guesser/reducing.ts index 3edf5fb..2ef6117 100644 --- a/src/lib/guesser/reducing.ts +++ b/src/lib/guesser/reducing.ts @@ -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) { - 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) { + 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) { } function get_word_with_smallest_cuts(candidates: Set, dict: Set) { - 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"]), ); diff --git a/src/lib/lib.ts b/src/lib/lib.ts index cc83922..4dfe6c4 100644 --- a/src/lib/lib.ts +++ b/src/lib/lib.ts @@ -1,8 +1,9 @@ -export { Dict } from "./dict.ts"; export type { Guessing } from "./guesser/guesser.ts"; +export type { LoggingStrategy } from "./runner.ts"; + +export { Dict } from "./dict.ts"; export { ExplorerGuesser } from "./guesser/explorer.ts"; export { ReducingGuesser } from "./guesser/reducing.ts"; export { Simulator } from "./game/simulator.ts"; export { ManualProxy } from "./game/proxy.ts"; export { Runner, TableLogging, VerboseLogging } from "./runner.ts"; -export type { LoggingStrategy } from "./runner.ts";