-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dmbates/properscoring
Handle duplicate characters when scoring a guess @JuliaRegistrator register()
- Loading branch information
Showing
8 changed files
with
220 additions
and
179 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## MixedModels v0.2.0 Release Notes | ||
|
||
- Bugfix release Issue #8, *Incorrect scoring when characters are repeated in guess* | ||
- Fixing this led to slightly better mean number of guesses to solution. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,19 @@ | ||
name = "Wordlegames" | ||
uuid = "1cb69566-e1cf-455f-a587-fd79a2e00f5a" | ||
authors = ["Douglas Bates <[email protected]> and contributors"] | ||
version = "0.1.1" | ||
version = "0.2.0" | ||
|
||
[deps] | ||
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c" | ||
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0" | ||
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" | ||
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" | ||
ThreadsX = "ac1d9e8a-700a-412c-b207-f0111f4b6c0d" | ||
|
||
[compat] | ||
AbstractTrees = "0.3" | ||
DataFrames = "1" | ||
Primes = "0.5" | ||
Tables = "1" | ||
ThreadsX = "0.1" | ||
julia = "1.7" | ||
|
||
[extras] | ||
|
@@ -25,4 +23,4 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c" | |
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" | ||
|
||
[targets] | ||
test = ["AbstractTrees", "Test", "Tables", "Primes"] | ||
test = ["AbstractTrees", "Primes", "Tables", "Test"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
""" | ||
hasdups(guess::NTuple{N,Char}) where {N} | ||
Returns `true` if there are duplicate characters in `guess`. | ||
""" | ||
function hasdups(guess::NTuple{N,Char}) where {N} | ||
@inbounds for i in 1:(N - 1) | ||
gi = guess[i] | ||
for j in (i + 1):N | ||
gi == guess[j] && return true | ||
end | ||
end | ||
return false | ||
end | ||
|
||
""" | ||
scorecolumn!(col, guess::NTuple{N,Char}, targets::AbstractVector{NTuple{N,Char}}) | ||
Return `col` updated with the scores of `guess` on each of the elements of `targets` | ||
If there are no duplicate characters in `guess` a simple algorithm is used, otherwise | ||
the more complex algorithm that accounts for duplicates is used. | ||
""" | ||
function scorecolumn!( | ||
col::AbstractVector{<:Integer}, | ||
guess::NTuple{N,Char}, | ||
targets::AbstractVector{NTuple{N,Char}}, | ||
) where {N} | ||
if axes(col) ≠ axes(targets) | ||
throw( | ||
DimensionMismatch("axes(col) = $(axes(col)) ≠ $(axes(targets)) = axes(targets)") | ||
) | ||
end | ||
if hasdups(guess) | ||
onetoN = (1:N...,) | ||
svec = zeros(Int, N) # scores for characters in guess | ||
unused = trues(N) # has a character in targets[i] been used | ||
@inbounds for i in axes(targets, 1) | ||
targeti = targets[i] | ||
fill!(unused, true) | ||
fill!(svec, 0) | ||
for j in 1:N # first pass for target in same position | ||
if guess[j] == targeti[j] | ||
unused[j] = false | ||
svec[j] = 2 | ||
end | ||
end | ||
for j in 1:N # second pass for match in unused position | ||
if iszero(svec[j]) | ||
for k in onetoN[unused] | ||
if guess[j] == targeti[k] | ||
svec[j] = 1 | ||
unused[k] = false | ||
break | ||
end | ||
end | ||
end | ||
end | ||
sc = 0 # similar to undup for evaluating score | ||
for s in svec | ||
sc *= 3 | ||
sc += s | ||
end | ||
col[i] = sc | ||
end | ||
else # simplified alg. for guess w/o duplicates | ||
@inbounds for i in axes(targets, 1) | ||
sc = 0 | ||
targeti = targets[i] | ||
for j in 1:N | ||
sc *= 3 | ||
gj = guess[j] | ||
sc += (gj == targeti[j] ? 2 : gj ∈ targeti) | ||
end | ||
col[i] = sc | ||
end | ||
end | ||
return col | ||
end | ||
|
||
""" | ||
scoretype(nchar) | ||
Return the smallest type `T<:Unsigned` for storing the scores from a pool of items of length `nchar` | ||
""" | ||
@inline function scoretype(nchar) | ||
if nchar ≤ 0 || nchar > 80 | ||
throw(ArgumentError("nchar = $nchar is not in `1:80`")) | ||
end | ||
return if nchar ≤ 5 | ||
UInt8 | ||
elseif nchar ≤ 10 | ||
UInt16 | ||
elseif nchar ≤ 20 | ||
UInt32 | ||
elseif nchar ≤ 40 | ||
UInt64 | ||
else | ||
UInt128 | ||
end | ||
end | ||
|
||
""" | ||
tiles(score::Integer, ntiles::Integer) | ||
tiles(svec::AbstractVector{<:Integer}) | ||
Return a length-`ntiles` `String` tile pattern from the numeric score `score`. | ||
""" | ||
function tiles(sc::Integer, ntiles) | ||
result = sizehint!(Char[], ntiles) # initialize to an empty array of Char | ||
for _ in 1:ntiles # _ indicates we won't use the value of the iterator | ||
sc, r = divrem(sc, 3) | ||
push!(result, iszero(r) ? '🟫' : (isone(r) ? '🟨' : '🟩')) | ||
end | ||
return String(reverse(result)) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e973a3f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register()
e973a3f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/55905
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: