We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
Post 21
Game Site Journal - 12 Wordle Feedback
Published on: 2025-04-28
Tags:
elixir, Side Project, LiveView, Game Site, Keyboard
This is a little bit of work I did on the Wordle. I was having an issue with the feed-back on the words not being perfect. I wanted to be sure that you would only get a yellow so long as the letter is in the word and not already in the right position and not already counted for. This was the initial setup for the change letter_count = letter_count(word) |> IO.inspect(label: "Letter Count") Enum.map(index_guess, fn {letter, index} -> cond do {letter, index} in index_word -> letter_count = Map.update!(letter_count, letter, fn count -> count - 1 end) |> IO.inspect(label: "green") [letter, "bg-green-400"] letter in String.split(word, "", trim: true) and letter_count[letter] > 0 -> letter_count = Map.update!(letter_count, letter, fn count -> count - 1 end) |> IO.inspect(label: "yellow") [letter, "bg-yellow-300"] true -> [letter, "bg-gray-300"] end However this would not work for words like "Tolls" and you entered "Torts." This would output 1 Green t and t yellow t. So I had the idea that I would need to set all the "greens" then all the "yellows." So I came up with this. First pass: {green_results, letter_count_after_greens} = Enum.map_reduce(index_guess, letter_count, fn {letter, index}, letter_count -> if {letter, index} in index_word do letter_count = Map.update!(letter_count, letter, fn count -> count - 1 end) IO.inspect(letter_count, label: "green update") {[letter, "bg-green-400"], letter_count} else {[{letter, index}], letter_count} end end) Second Pass: {final_results, _final_letter_count} = Enum.map_reduce(green_results, letter_count_after_greens, fn [{letter, index}], letter_count -> if letter in String.split(word, "", trim: true) and letter_count[letter] > 0 do letter_count = Map.update!(letter_count, letter, fn count -> count - 1 end) IO.inspect(letter_count, label: "yellow update") {[letter, "bg-yellow-300"], letter_count} else {[letter, "bg-gray-300"], letter_count} end # Already green [letter, color], letter_count -> {[letter, color], letter_count} end) Let's go over the first part: We went from a Enum.Map to an Enum.reduce. This is because we want to keep the letter_count changeable. We also need to keep the {letter, index} so we can run the same style function on what is not already set. Let's talk about the second pass. First off we have 2 different types of enumerable in green_results: [letter, color] {letter, index} We use function input matching to decided what part of the function we default to. For the first one we default to the second pattern match. That will just keep the same output and still make sure that we keep the letter_count updated. For the second we go through the first pattern match. This is where we have the if statement that will decide whether it's a yellow or gray. It's a pretty elegant solution to the problem while still using a lot of the same code as before. The hardest part of this whole process was determining the way to keep the letter_count updated and still being able to pass the right information to the next Enum.
