Compare commits
No commits in common. "febec0cf105023a8a90a9b8ffefe0e8010a999fa" and "1b737755c9f4d6d84e2d8c261d5f3fbb8da96fee" have entirely different histories.
febec0cf10
...
1b737755c9
9
Makefile
9
Makefile
|
@ -1,9 +0,0 @@
|
||||||
all: wordle_opt.wasm
|
|
||||||
|
|
||||||
wordle_opt.wasm: wordle_opt/target/wasm32-unknown-unknown/release/wordle_opt.wasm
|
|
||||||
#wasm-snip $< -o $@
|
|
||||||
cp $< $@
|
|
||||||
|
|
||||||
wordle_opt/target/wasm32-unknown-unknown/release/wordle_opt.wasm: wordle_opt/src/*.rs
|
|
||||||
cd wordle_opt; cargo build --target wasm32-unknown-unknown --release
|
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
target/
|
|
|
@ -1,62 +0,0 @@
|
||||||
# This file is automatically @generated by Cargo.
|
|
||||||
# It is not intended for manual editing.
|
|
||||||
version = 3
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "cfg-if"
|
|
||||||
version = "0.1.10"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "libc"
|
|
||||||
version = "0.2.144"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2b00cc1c228a6782d0f076e7b232802e0c5689d41bb5df366f2a6b6621cfdfe1"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "memory_units"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8452105ba047068f40ff7093dd1d9da90898e63dd61736462e9cdda6a90ad3c3"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wee_alloc"
|
|
||||||
version = "0.4.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "dbb3b5a6b2bb17cb6ad44a2e68a43e8d2722c997da10e928665c72ec6c0a0b8e"
|
|
||||||
dependencies = [
|
|
||||||
"cfg-if",
|
|
||||||
"libc",
|
|
||||||
"memory_units",
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi"
|
|
||||||
version = "0.3.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
|
||||||
dependencies = [
|
|
||||||
"winapi-i686-pc-windows-gnu",
|
|
||||||
"winapi-x86_64-pc-windows-gnu",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi-i686-pc-windows-gnu"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "wordle_opt"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"wee_alloc",
|
|
||||||
]
|
|
|
@ -1,17 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "wordle_opt"
|
|
||||||
version = "0.1.0"
|
|
||||||
edition = "2021"
|
|
||||||
|
|
||||||
[lib]
|
|
||||||
crate-type = ["cdylib"]
|
|
||||||
bench = false
|
|
||||||
|
|
||||||
[profile.release]
|
|
||||||
lto = true
|
|
||||||
opt-level = 's'
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
wee_alloc = "*"
|
|
|
@ -1,117 +0,0 @@
|
||||||
use std::alloc::{alloc, dealloc, Layout};
|
|
||||||
|
|
||||||
extern crate wee_alloc;
|
|
||||||
|
|
||||||
#[global_allocator]
|
|
||||||
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
|
|
||||||
|
|
||||||
// webassembly version of all the wordle logic
|
|
||||||
// because the javascript version is slow as hell on firefox
|
|
||||||
use std::os::raw::c_char;
|
|
||||||
|
|
||||||
static mut LOOKUP: Option<&'static mut [u8]> = None;
|
|
||||||
static mut STRINGS: Option<&'static mut [u8]> = None;
|
|
||||||
static mut STR_IDXS: Option<&'static mut [*const c_char]> = None;
|
|
||||||
|
|
||||||
fn unwrap_or_abort<T>(v: Option<T>) -> T {
|
|
||||||
match v {
|
|
||||||
Some(v) => v,
|
|
||||||
None => std::process::abort(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn alloc_ary<T>(sz: usize) -> &'static mut [T] {
|
|
||||||
let layout = unwrap_or_abort(Layout::from_size_align(
|
|
||||||
sz*std::mem::size_of::<T>(),
|
|
||||||
std::mem::align_of::<T>()
|
|
||||||
).ok());
|
|
||||||
let ptr = alloc(layout) as *mut T;
|
|
||||||
std::slice::from_raw_parts_mut(ptr, sz)
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe fn dealloc_ary<T>(v: &'static mut [T]) {
|
|
||||||
let layout = unwrap_or_abort(Layout::from_size_align(
|
|
||||||
v.len()*std::mem::size_of::<T>(),
|
|
||||||
std::mem::align_of::<T>()
|
|
||||||
).ok());
|
|
||||||
dealloc(v.as_ptr() as *mut u8, layout);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern fn init_strings(sz: usize) -> *mut c_char {
|
|
||||||
unsafe {
|
|
||||||
if let Some(v) = &mut STRINGS {
|
|
||||||
dealloc_ary(v);
|
|
||||||
STRINGS = None;
|
|
||||||
}
|
|
||||||
STRINGS = Some(alloc_ary(sz));
|
|
||||||
unwrap_or_abort(STRINGS.as_deref_mut().map(|v| v.as_mut_ptr() as *mut c_char))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern fn init_idx() -> usize {
|
|
||||||
// TODO
|
|
||||||
0
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern fn init_lookup() -> *mut c_char {
|
|
||||||
unsafe {
|
|
||||||
let num_strings = STR_IDXS.as_ref().map(|idxs| idxs.len()).unwrap_or(0);
|
|
||||||
if let Some(v) = &mut LOOKUP {
|
|
||||||
dealloc_ary(v);
|
|
||||||
STRINGS = None;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOOKUP = Some(alloc_ary(num_strings*num_strings));
|
|
||||||
unwrap_or_abort(
|
|
||||||
LOOKUP.as_deref_mut().map(|v| v.as_mut_ptr() as *mut c_char))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[no_mangle]
|
|
||||||
pub extern fn calc_match(guess: *const c_char, reference: *const c_char) -> u8 {
|
|
||||||
let mut unmatched = vec![0u8; 26];
|
|
||||||
let mut ret = 0;
|
|
||||||
let mut matched = 0;
|
|
||||||
|
|
||||||
let mut mul = 1;
|
|
||||||
for i in 0..5 {
|
|
||||||
unsafe {
|
|
||||||
if *(guess.offset(i)) == *(reference.offset(i)) {
|
|
||||||
ret += 1*mul;
|
|
||||||
matched |= 1 << i;
|
|
||||||
} else {
|
|
||||||
unmatched[*(reference.offset(i)) as usize - 'a' as usize] += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mul *= 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
mul = 1;
|
|
||||||
for i in 0..5 {
|
|
||||||
if (matched & (1 << i)) == 0 {
|
|
||||||
let idx = unsafe { *(guess.offset(i)) as usize - ('a' as usize) };
|
|
||||||
if unmatched[idx] > 0 {
|
|
||||||
ret += 2*mul;
|
|
||||||
unmatched[idx] -= 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mul *= 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn it_works() {
|
|
||||||
let result = add(2, 2);
|
|
||||||
assert_eq!(result, 4);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue