Solving Logic Puzzles with Excel

Solving Logic Puzzles with Excel

It probably won't surprise anyone that knows me well, that I quite like puzzles (as/when I have any free time to do them). So when someone recently shared a particularly fiendish logic puzzle with me to solve over Xmas, I happily sat down with a couple of mince pies and a glass of mulled wine one evening to give it a go.

As logic puzzles go, this one was satisfyingly difficult (thanks "Puzzle Baron", whoever you are). It was bigger than usual, there were no giveaway clues (the kind that allow you to put a tick in the box straight away) and several clues were couched in terms of either statement A is true (or statement B is) that are difficult to do much with at first...

---

But I gave it a go anyway, steadily worked my way through all the clues, and after about an hour had almost reached the end, when I discovered a contradiction. Somewhere along the way, I must have made a mistake, probably in carrying out all the routine admin of updating all the affected cells in the grid. So I started over, but frustratingly, the same thing happened again, but this time with a different contradiction.

It was at this point that I asked myself - surely there's a better way to do this with an Excel spreadsheet (automated of course). I didn't exactly want to press a button to solve the puzzle automatically (where's the fun in that?) but I did feel that I wanted some kind of "power tool" to help me with all the routine updates to the grid, and to do them without any mistakes!

And so I wrote my logic puzzle "power tool", which I describe below. Now as I don't have (and won't presume) Puzzle Baron's permission to share their puzzle with you, I've illustrated the tool with excerpts from that old favourite logic puzzle "Who owns the Zebra" - which even has its own Wikipedia entry, in case you fancy giving it a try.

So here's how I did this.

First I created a table of categories and values....

I then wrote software to automatically produce a puzzle-solving grid from the above, with a restart button to run the code anytime I wanted to start over.

 

Each cell in the grid was set up to ONLY accept T (True), F (False) or a number, so if you wanted to (say) record the fact that House #2 has a red door, then you just right-click the appropriate cell, and select True from the drop-down picklist. The value entered is then colour-coded (in green or red) using conditional formatting to provide good visual feedback. 

Why do we need a number? Well that's so you can record the number of the clue which tells you something about that cell, even when you can't yet write TRUE or FALSE there. For example, if clue #2 tells you that EITHER house #1 has a yellow door OR house#5 has a blue door, then you can write it like so...

...and when either one of those 2's disappears as a consequence of other logical steps, you can immediately see that clue #2 is now ready to be acted upon.

So far so good. We have an electronic means of drawing a grid and interacting with it, but solving the puzzle is still an entirely manual task and I wanted a power tool (which is where it gets interesting).

Note that it's possible to write code that is executed when something changes on the sheet, so we can use this to execute code which automatically writes FALSE into the rows and columns, whenever you mark a cell as TRUE. So here, when I declare that house #2 has a red door, the tool automatically excludes house #2 from having any other door colour and excludes any other house from having a red door...

And of course, given that the grid has a diagonal symmetry, how about if we automatically reflect everything about the diagonal while we're about it.

In words, this just means that if house #2 has a red door, then the house with a red door, is house #2. 

But this diagonal symmetry means that everything down the diagonal will be true - (ie, house #2 is house #2), so all grids (in practice) start with the diagonal pre-populated as true...

And the automated actions we've already described, add all those FALSE entries automatically before we even start...

As Sherlock Holmes once said... “When you have eliminated the impossible, whatever remains, however improbable, must be the truth.”

When comes to logic puzzles, that means automatically recognising when only one possibility exists, and setting it to TRUE, for example this...

...is turned into this...

And finally, we should recognise that once we know (for example) that House #3 has an Ivory Door, then anything ELSE we know or learn about House #3 will also be true for the Ivory Door (and vice versa). So for example, if we learn that the Norwegian DOESN'T live at house #3, then he doesn't have an ivory door either!

So the grid above automatically becomes the grid below.

 

These simple rules, once automated, and run in reaction to any changes you make the grid, take away the mundanity of solving logic puzzles. Of course for some people, I appreciate that's what interests them, but for me, finding ways to automate tedious repetitive actions, and leaving the thinking parts is what I do.

Did it help? Absolutely. Using this tool, I solved the original puzzle I was given at Xmas in about 10-15 minutes, whereas without it I twice spent an hour only to be floored by an admin mistake. For me - that's a worthwhile achievement - and one worthy of another glass of mulled wine.


A final footnote - in case anyone with sufficiently advanced Excel skills wishes to try this for themselves here are some important tips...

As each change automatically produces many more changes, it's important not to automatically update anything which already has the desired value (else the cascade of changes will never finish). It's also sensible to check that you're never changing a TRUE value to a FALSE one (or vice versa). Not only is that the signal for a contradiction (which you should flag to the user) it will likely produce an infinite loop of changes.

On a large puzzle, the cascade of changes can cause Excel to recurse very deeply. It's entirely possible that Excel runs out of stack memory before coming up for air! So in an improved version of the code, I ensured that it didn't apply updates immediately, but instead built up a "to-do" list of changes each time I typed something. A separate routine then carried out these actions a few seconds later and serially one after the other, until there was nothing left to change. This doesn't run out of stack memory and works like a charm. :) 

Excel Wizardry
Excel Wizardry Logo
Working Magic with MS Excel

Excel Wizardry

The Laurels, Meadow Close,
Blackwater,
CAMBERLEY,
Surrey
GU17 9DB

Tel: 07766 492 991
Email: kevin@excel-wizardry.co.uk
We use cookies

We use cookies on our website. Some of them are essential for the operation of the site, while others help us to improve this site and the user experience (tracking cookies). You can decide for yourself whether you want to allow cookies or not. Please note that if you reject them, you may not be able to use all the functionalities of the site.