{"id":753,"date":"2022-01-28T21:09:09","date_gmt":"2022-01-28T21:09:09","guid":{"rendered":"https:\/\/perfectionatic.org\/?p=753"},"modified":"2022-01-30T11:02:14","modified_gmt":"2022-01-30T11:02:14","slug":"exploring-wordle-with-julia-part-i","status":"publish","type":"post","link":"https:\/\/perfectionatic.org\/?p=753","title":{"rendered":"Exploring Wordle with Julia: Part I"},"content":{"rendered":"\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/www.powerlanguage.co.uk\/wordle\/\" target=\"_blank\">Wordle<\/a>  is a fun and easy game that has shot to fame recently. It is completely minimalistic, no-fills and <a rel=\"noreferrer noopener\" href=\"https:\/\/www.nytimes.com\/2022\/01\/03\/technology\/wordle-word-game-creator.html?fbclid=IwAR2LuFKvtl9ZYg9CUVOpMHqFU9ZWbLc2Orzmr8SieuBs8TiTrTYFZ1U3kXg\" target=\"_blank\">highly addictive<\/a>. We will explore here how to write as simple solver for Wordle with Julia and then adding layers of complexity and sophistication as we go along. <\/p>\n\n\n\n<p> In Wordle we are successively guessing five-letter words. After each guess the game tells us which of our letters occur in the <em>secret<\/em> word and whether they are in the correct position.  As explained in their website.  <\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><a href=\"https:\/\/perfectionatic.org\/wp-content\/uploads\/2022\/01\/image-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/perfectionatic.org\/wp-content\/uploads\/2022\/01\/image-1.png\" alt=\"\" class=\"wp-image-772\" width=\"337\" height=\"311\" srcset=\"https:\/\/perfectionatic.org\/wp-content\/uploads\/2022\/01\/image-1.png 337w, https:\/\/perfectionatic.org\/wp-content\/uploads\/2022\/01\/image-1-300x277.png 300w\" sizes=\"auto, (max-width: 337px) 100vw, 337px\" \/><\/a><\/figure>\n\n\n\n<p>So after each guess we narrow the set of possible words to choose from till we arrive at the secret word, and we only have six tries. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"our-dictionary\">Our Dictionary <\/h2>\n\n\n\n<p>Wordle only deals with a valid five-letter English words. So we need to start grabbing such words. A good place to start is the <a href=\"https:\/\/www-cs-faculty.stanford.edu\/~knuth\/sgb.html\">Standford GraphBase<\/a> data set. <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/gist.github.com\/mbeltagy\/51e4672379a71e5d73c789e6106bc296#file-word_dict-jl\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"constraints\">Constraints<\/h2>\n\n\n\n<p>The next step is the try narrow down the set of feasible words as we are getting clues. Towards that end, we construct  a <code>Dict<\/code> to map letters that are in the word at the <mark style=\"background-color:#00d084\" class=\"has-inline-color\">correct spot<\/mark> to their location, another <code>Dict<\/code> to maps letter in the word that are in the <mark style=\"background-color:#fcb900\" class=\"has-inline-color\">wrong spot<\/mark>, and finally a <code>Set<\/code> of letters that are <mark style=\"background-color:#abb8c3\" class=\"has-inline-color\">not in the word<\/mark>:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/gist.github.com\/mbeltagy\/51e4672379a71e5d73c789e6106bc296#file-constraint_definition-jl\n<\/div><\/figure>\n\n\n\n<p>Now we can encode the the response we get the game as <code>String<\/code> of numbers, where <mark style=\"background-color:#abb8c3\" class=\"has-inline-color\">0<\/mark> means that the character is not in the word,  <mark style=\"background-color:#fcb900\" class=\"has-inline-color\">1<\/mark> means the that character is the word but not in the right spot, and <mark style=\"background-color:#00d084\" class=\"has-inline-color\">2<\/mark> means that the character is in the word at the correct spot. For example <\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-9d6595d7 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<pre class=\"wp-block-preformatted\"><strong>Secret Word<\/strong>:    perky\n<strong>Candidate<\/strong> <strong>Word<\/strong>: <mark style=\"background-color:#fcb900\" class=\"has-inline-color\">e<\/mark><mark style=\"background-color:#abb8c3\" class=\"has-inline-color\">bon<\/mark><mark style=\"background-color:#00d084\" class=\"has-inline-color\">y<\/mark>\n<strong>Response<\/strong>:       10002<\/pre>\n<\/div>\n<\/div>\n\n\n\n<p>We can update our set of constraint for any response by the following function:<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/gist.github.com\/mbeltagy\/51e4672379a71e5d73c789e6106bc296#file-update_constraints_by_response-jl\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"shrinking-the-feasible-set-of-words\">Shrinking the feasible set of words <\/h2>\n\n\n\n<p id=\"shrinking-the-feasible-set-of-words\">The constraints will reduce the feasible set of possible words. This can be done by applying filters that make use of constraints as in the following function:  <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/gist.github.com\/mbeltagy\/51e4672379a71e5d73c789e6106bc296#file-word_set_reduction-jl\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"putting-it-all-together\">Putting it all together<\/h2>\n\n\n\n<p>So all what we have to do now is to apply the following step <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Guess a candidate first word <\/li><li>Get response from the Game<\/li><li>Use that response to update the set of constraints<\/li><li>Use the constraints to reduce the set of possible words <\/li><li>Guess the new candidate word <\/li><li>Repeat step 2 until a word is found or the game is over<\/li><\/ol>\n\n\n\n<p>In the steps 1 and 5 above, the simplest approach is just to pick a word at random. Below is Julia repl session showing how we go about applying the above. The secret word here is &#8220;super&#8221; <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/gist.github.com\/mbeltagy\/51e4672379a71e5d73c789e6106bc296#file-blog_julia_wordle_repl_session1-jl\n<\/div><\/figure>\n\n\n\n<p>We see that we arrived at the correct word after guessing<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>excel<\/li><li>buyer<\/li><li>fumer<\/li><li>purer<\/li><li>super<\/li><\/ul>\n\n\n\n<p>That is a 5\/6 score. Not too bad! But we could do better. How? We explore that in the next part. <\/p>\n\n\n\n<p>See also below an interactive session where the secret word is &#8220;wrung&#8221; <\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/perfectionatic.org\/wp-content\/uploads\/2022\/01\/wordle_simple.gif\"><img loading=\"lazy\" decoding=\"async\" width=\"620\" height=\"402\" src=\"https:\/\/perfectionatic.org\/wp-content\/uploads\/2022\/01\/wordle_simple.gif\" alt=\"\" class=\"wp-image-815\"\/><\/a><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Wordle is a fun and easy game that has shot to fame recently. It is completely minimalistic, no-fills and highly addictive. We will explore here how to write as simple solver for Wordle with Julia and then adding layers of complexity and sophistication as we go along. In Wordle we are successively guessing five-letter words. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101,74,102],"tags":[92,100],"class_list":["post-753","post","type-post","status-publish","format-standard","hentry","category-games","category-julia","category-wordle","tag-julia","tag-wordle"],"_links":{"self":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=753"}],"version-history":[{"count":39,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/753\/revisions"}],"predecessor-version":[{"id":816,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=\/wp\/v2\/posts\/753\/revisions\/816"}],"wp:attachment":[{"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/perfectionatic.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}