feat(web): solver input
This commit is contained in:
+14
-3
@@ -7,9 +7,9 @@ use crate::word_list::WordList;
|
|||||||
use derive_more::Display;
|
use derive_more::Display;
|
||||||
use tabular::{Row, Table};
|
use tabular::{Row, Table};
|
||||||
|
|
||||||
struct Guess {
|
pub struct Guess {
|
||||||
pattern: Pattern,
|
pub pattern: Pattern,
|
||||||
information_gained: f64,
|
pub information_gained: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Solver {
|
pub struct Solver {
|
||||||
@@ -141,6 +141,17 @@ impl Solver {
|
|||||||
table.to_string()
|
table.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn guesses(&self) -> &Vec<Guess> {
|
||||||
|
&self.guesses
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn stats(&self) -> (u64, f64) {
|
||||||
|
(
|
||||||
|
self.possible_solutions.len() as u64,
|
||||||
|
self.possible_solutions.equal_likelieness_entropy(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn tabular_format(&self) -> String {
|
pub fn tabular_format(&self) -> String {
|
||||||
let mut table = Table::new("{:<} | {:<}");
|
let mut table = Table::new("{:<} | {:<}");
|
||||||
|
|
||||||
|
|||||||
+14
-9
@@ -21,11 +21,6 @@ pub enum Color {
|
|||||||
GREEN,
|
GREEN,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen]
|
|
||||||
extern "C" {
|
|
||||||
pub fn alert(msg: &str);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[wasm_bindgen]
|
#[wasm_bindgen]
|
||||||
impl Solver {
|
impl Solver {
|
||||||
@@ -50,14 +45,24 @@ impl Solver {
|
|||||||
.collect(),
|
.collect(),
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
alert(&format!("{:?}", pat));
|
|
||||||
|
|
||||||
self.0.apply_guess(pat)?;
|
self.0.apply_guess(pat)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stats(&self) -> String {
|
pub fn guess_infos(&self) -> Vec<f64> {
|
||||||
self.0.tabular_format()
|
self.0
|
||||||
|
.guesses()
|
||||||
|
.iter()
|
||||||
|
.map(|g| g.information_gained)
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solution_space(&self) -> u64 {
|
||||||
|
self.0.stats().0
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn solution_space_uncertainty(&self) -> f64 {
|
||||||
|
self.0.stats().1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+52
-21
@@ -1,33 +1,64 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
let count = $state(0);
|
import { Color, Solver } from "wordle-solver";
|
||||||
|
import WordleLine from "./components/WordleLine.svelte"
|
||||||
|
|
||||||
function increment() {
|
let guesses = $state([{
|
||||||
count += 1;
|
letters: ["", "", "", "", ""],
|
||||||
|
colors: [Color.GREY, Color.GREY, Color.GREY, Color.GREY, Color.GREY]
|
||||||
|
}]);
|
||||||
|
|
||||||
}
|
let cursor = $state(0);
|
||||||
|
|
||||||
|
let solver = Solver.new();
|
||||||
|
|
||||||
|
let info_gained = $state(solver.guess_infos());
|
||||||
|
let solution_space = $state(solver.solution_space());
|
||||||
|
let solution_space_uncertainty = $state(solver.solution_space_uncertainty());
|
||||||
|
|
||||||
|
function handleKey(event: KeyboardEvent) {
|
||||||
|
if (event.key === "Enter") {
|
||||||
|
cursor = 0;
|
||||||
|
const guess = guesses[guesses.length-1];
|
||||||
|
|
||||||
|
solver.guess(guess.letters.join(""), guess.colors)
|
||||||
|
solution_space = solver.solution_space();
|
||||||
|
solution_space_uncertainty = solver.solution_space_uncertainty();
|
||||||
|
info_gained = solver.guess_infos();
|
||||||
|
|
||||||
|
guesses.push({
|
||||||
|
letters: ["", "", "", "", ""],
|
||||||
|
colors: [Color.GREY, Color.GREY, Color.GREY, Color.GREY, Color.GREY]
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
<svelte:window onkeydown={handleKey} />
|
||||||
|
|
||||||
<div class="min-h-screen bg-zinc-950 text-zinc-100 flex items-center justify-center">
|
<div class="min-h-screen bg-zinc-950 text-zinc-100 flex items-center justify-center">
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
<h1 class="mb-6 text-3xl font-bold">
|
<h1 class="mb-6 text-3xl font-bold">
|
||||||
svelte tests
|
Wordle Solver
|
||||||
</h1>
|
</h1>
|
||||||
|
|
||||||
<button
|
<p class="mt-4 text-sm text-zinc-500">
|
||||||
onclick={increment}
|
Solution-space: {solution_space} · {solution_space_uncertainty.toFixed(2)}bits
|
||||||
class="
|
</p>
|
||||||
rounded-lg
|
|
||||||
bg-blue-600
|
<div class="mt-4 flex flex-col gap-2">
|
||||||
px-5 py-2.5
|
{#each guesses as g,i (g)}
|
||||||
font-semibold
|
<WordleLine active={i === guesses.length-1}
|
||||||
text-white
|
bind:cursor
|
||||||
shadow-lg
|
bind:letters={guesses[i].letters}
|
||||||
transition
|
bind:colors={guesses[i].colors}
|
||||||
hover:bg-blue-500
|
info_gained={i < info_gained.length ? info_gained[i] : null}
|
||||||
active:scale-95
|
/>
|
||||||
"
|
{/each}
|
||||||
>
|
</div>
|
||||||
Clicked {count} {count === 1 ? "time" : "times"}
|
|
||||||
</button>
|
|
||||||
|
<p class="mt-4 text-sm text-zinc-500">
|
||||||
|
Type letters · ← → navigate · Space cycle color · Backspace delete · Enter apply
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -0,0 +1,71 @@
|
|||||||
|
<script lang="ts">
|
||||||
|
import { Color } from "wordle-solver";
|
||||||
|
|
||||||
|
let { active=false,
|
||||||
|
cursor=$bindable(0),
|
||||||
|
letters=$bindable(["", "", "", "", ""]),
|
||||||
|
colors=$bindable([Color.GREY, Color.GREY, Color.GREY, Color.GREY, Color.GREY]),
|
||||||
|
info_gained=null } = $props();
|
||||||
|
|
||||||
|
function handleKey(event: KeyboardEvent) {
|
||||||
|
if (!active) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (/^[a-zA-Z]$/.test(event.key)) {
|
||||||
|
letters[cursor] = event.key.toUpperCase();
|
||||||
|
|
||||||
|
if (cursor < 4) {
|
||||||
|
cursor++;
|
||||||
|
} else {
|
||||||
|
cursor = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key === "Backspace") {
|
||||||
|
if (letters[cursor] === "" && cursor > 0) {
|
||||||
|
cursor--;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (active) {
|
||||||
|
letters[cursor] = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key === "ArrowLeft") {
|
||||||
|
cursor = Math.max(0, cursor - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key === "ArrowRight") {
|
||||||
|
cursor = Math.min(4, cursor + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.key === " ") {
|
||||||
|
colors[cursor] = (colors[cursor] + 1) % 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<svelte:window onkeydown={handleKey} />
|
||||||
|
|
||||||
|
<div class="flex justify-center gap-2">
|
||||||
|
{#each letters as letter, i}
|
||||||
|
<div
|
||||||
|
class="
|
||||||
|
flex h-16 w-16 items-center justify-center
|
||||||
|
rounded-lg border-2
|
||||||
|
text-3xl font-bold
|
||||||
|
transition-all
|
||||||
|
"
|
||||||
|
class:border-blue-500={i === cursor && active}
|
||||||
|
class:border-zinc-700={i !== cursor || !active}
|
||||||
|
class:bg-green-600={colors[i] === Color.GREEN}
|
||||||
|
class:bg-yellow-600={colors[i] === Color.YELLOW}
|
||||||
|
class:bg-grey-600={colors[i] === Color.GREY}
|
||||||
|
>
|
||||||
|
{letter}
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
<span class="w-12 text-sm text-zinc-500 self-center">{
|
||||||
|
(info_gained !== null) ? info_gained.toFixed(2) + "bits" : '↵'
|
||||||
|
}</span>
|
||||||
|
</div>
|
||||||
Reference in New Issue
Block a user