Joe Conley Tagged puzzles Random thoughts on technology, books, golf, and everything else that interests me Hacking NPR's Sunday Puzzle <p>I’m a big fan of puzzles. I’ll often start my day attempting the Philadelphia Inquirer’s jumble and crossword, with varying degress of success. One puzzle I never miss is <a href="">NPR’s Weekend Edition Puzzle</a> featuring New York Times puzzle editor Will Shortz. At the end of each segment, he poses a question to the audience, and occasionally these questions can be solved with the help of programming. To that end, I’ve built an app to help non-programmers solve these puzzles. I’ve also added common puzzle utilities like an Anagram checker and a Scrabble solution generator.</p> <p>You can find a running version of the puzzle solver <a href="">here</a>. The Scala library of the puzzle utilities can be found <a href="">here</a>. This project also has an npr package which shows examples of programs written to solve past NPR puzzles.</p> <h2 id="puzzle-solver">Puzzle Solver</h2> <p>This single-page app searches through a specified list of words searching for one of three things: anagrams, potential Scrabble solutions, or most powerfully, a regular expression. We’ll use this last mode to solve a recent NPR puzzle.</p> <h3 id="puzzle-modes">Puzzle Modes</h3> <h4 id="anagrams">Anagrams</h4> <p>This mode will search for all potential anagrams of the input word. Helpful for solving the jumble commonly found in your newspaper. For example, here are today’s four jumble clues from the Inquirer:</p> <div class="well well-lg"> GREEV WORNC KNITSY KRUTYE </div> <p>Setting the app controls to Mode = Anagram and Word List Source = Scrabble (a good list source for most purposes), we set Input once for each jumble and after hitting Submit, we get one proper anagram for each jumble.</p> <h4 id="scrabble">Scrabble</h4> <p>This mode will search for all possible valid Scrabble words based on the letters (i.e. your Scrabble hand) provided. You can also specify how many wild cards (i.e. “blanks”) are in your hand. This is useful not only to help find solutions but to verify solutions as well (faster than leafing through a Scrabble dictionary).</p> <h4 id="regular-expressions">Regular expressions</h4> <p>This is the most powerful mode. This will return all words matching a valid Java regular expression. You can find a good tutorial about Java regular expressions <a href="">here</a>.</p> <h3 id="word-lists">Word Lists</h3> <p>I’ve gathered two common word lists, a list of valid Scrabble words mentioned <a href="">here</a> and the <a href=";content-type=text%2Fplain">UNIX word list</a>. I’ve also added a space to add a custom list of words to search.</p> <h2 id="technology">Technology</h2> <p>I’ve built this app using <a href="">Play Scala</a> as the backend. After importing my puzzles library, here’s the relevant controller code:</p> <script src=""></script> <p>I probably could have handled the JSON a bit safer by using a Reads[T] object to handle the parsing, but as this app was fairly simple I used the unsafe conversion Please don’t think less of me!</p> <p>I’ve also employed <a href="">Knockout.js</a> to manage the front-end functionality. Knockout.js is a lightweight MVVM framework which manages DOM updates automatically and succinctly, ensuring that your front-end code is not a monolith of jQuery calls. Here’s the code for the front-end:</p> <script src=""></script> <p>And that’s it! A good future exercise would be to stream the solutions reactively, especially when dealing with a long word list. This would be done using Play’s <a href="">Enumeratee library</a>. If anyone’s interested in that I can post a follow-up detailing that solution.</p> <h2 id="puzzle-solution---double-s-non-programmers">Puzzle Solution - Double S (Non-Programmers)</h2> <p>Now our tool is ready to help us solve a recent puzzle. Here’s the question, reprinted from <a href="">NPR’s website</a>:</p> <div class="well well-lg">What word, containing two consecutive S's, becomes its own synonym if you drop those S's?</div> <p>Using Regex mode and our Scrabble word list, we can define the regular expression .<em>ss.</em> to return all words with a double S:</p> <p><img src="/assets/regex.bmp" alt="Regex Step One" /></p> <p>From there, we use Excel to store this list in the first column. We can then copy this list in the second column and do a Find/Replace to remove instances of “SS”. We could inspect each row for synonyms but as we have 1976 results, that’s a lot of words to inspect. To further narrow down our choices, I used Excel to transpose the second column into one row, copy that into a text file and do another Find/Replace by highlighting the space between each word and replacing with a vertical bar. This gives us another regular expression that should look like this:</p> <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>abbe|aby|abyes|...|zestle|ziple|zonele </code></pre></div></div> <p>Using that regular expression, we can use our Puzzle Solver to determine which words in the list are valid words and which aren’t (we only care about words that are valid). This second search takes significantly longer, but we’ll eventually get a list back of 156 valid words. Plugging this list back into Excel, we can use a VLOOKUP to identify the pairs of words which are valid. After sorting to see valid word pairs first, we can inspect to see which pairs are synonyms. Luckily, the answer appears fairly early in the list (spreadsheet can be downloaded <a href="">here</a>):</p> <p><img src="/assets/SS.bmp" alt="Regex Solution" /></p> <h2 id="puzzle-solution---double-s-programmers">Puzzle Solution - Double S (Programmers)</h2> <p>The previous non-coding solution might have seemed a bit convoluted. A much simpler method would be to use my Scala library directly to find the solutions, which can be done with as little as four lines of code:</p> <script src=""></script> <h2 id="conclusion">Conclusion</h2> <p>If you enjoy NPR’s Sunday Puzzle, I’d highly recommend <a href="">Blaine’s Puzzle Blog</a> as an excellent companion resource. This blog community offers tantalizing, interesting hints for the solution of the puzzle and often digress into other challenging puzzles as well.</p> <p>Happy puzzling!</p> Mon, 10 Feb 2014 00:00:00 +0000