Better iteration

This commit is contained in:
2026-06-08 04:15:46 -06:00
parent 5738b62cf9
commit 119bad49f1

View File

@@ -1,21 +1,21 @@
/// The length of Wordle words.
const WordLen = 5;
pub const WordLen = 5;
/// A single Wordle word (ASCII).
const Word = [WordLen]u8;
pub const Word = [WordLen]u8;
/// The type of string literals representing words.
const WordLiteral = *const [WordLen:0]u8;
pub const WordLiteral = *const [WordLen:0]u8;
/// A clue for a spot given a guess.
const Clue = enum {
pub const Clue = enum {
Gray,
Yellow,
Green,
};
/// The list of clues for a full guess.
const Clues = [WordLen]Clue;
pub const Clues = [WordLen]Clue;
/// Guesses a word given a solution and returns the clues.
pub fn checkGuess(guess: Word, solution: Word) Clues {
@@ -23,7 +23,7 @@ pub fn checkGuess(guess: Word, solution: Word) Clues {
var clues: Clues = undefined;
// compare each letter pairwise
for (guess, 0..WordLen) |guessLetter, idx| {
for (guess, 0..) |guessLetter, idx| {
// if letters match, the clue is green
if (guessLetter == solution[idx]) {
clues[idx] = Clue.Green;
@@ -34,7 +34,7 @@ pub fn checkGuess(guess: Word, solution: Word) Clues {
clues[idx] = Clue.Gray;
// if a matching letter in the solution can be found, write yellow
for (solution, 0..WordLen) |solutionLetter, _| {
for (solution) |solutionLetter| {
if (guessLetter == solutionLetter) {
clues[idx] = Clue.Yellow;
break;
@@ -47,7 +47,7 @@ pub fn checkGuess(guess: Word, solution: Word) Clues {
}
/// Converts a fixed-length string literal into a word.
fn strToWord(str: WordLiteral) Word {
pub fn strToWord(str: WordLiteral) Word {
var buffer: Word = undefined;
@memcpy(&buffer, str);
return buffer;
@@ -64,9 +64,9 @@ fn expectGuessClues(guess: WordLiteral, solution: WordLiteral, expectClues: Word
const clues = checkGuess(guessWord, solutionWord);
// confirm that the expected clues match the clues
for (clues, 0..) |clue, idx| {
for (clues, expectCluesWord) |clue, expectedChar| {
// convert character to associated clue variant
const expected = switch (expectCluesWord[idx]) {
const expected = switch (expectedChar) {
' ' => Clue.Gray,
'Y' => Clue.Yellow,
'G' => Clue.Green,