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