FiveThirtyEight Riddler — Prolog to the Rescue!

Matt Dodge
8 min readFeb 23, 2019

One of my weekly routines is to solve the FiveThirtyEight Riddler puzzle. If you've never done them before, they are a set of weekly riddles that tend to involve math, logic, and statistics. Sometimes, though, they make you think. That's silly, thinking is what computers are for!

This week's puzzle is a perfect example of a puzzle that can be solved with one of my favorite programming languages, Prolog. I believe that in order to be a good software engineer or computer scientist you must be versed in many different programming languages. There simply isn't one "best" programming language, they come in all shapes and sizes and some are better than others for a certain problem.

One rather niche programming language is Prolog. If you've never seen it or worked with it before I highly recommend checking out some tutorials and getting familiar with the basics. Essentially, it's a language that allows you to define a set of rules within a problem space and then find the answers, if they exist, that satisfy all of those rules.

In this post, I'll walk through how we can use Prolog to quickly and succinctly arrive at an answer for this week's Riddler puzzle. I'll be using SWI-Prolog for this, but I believe it will work in any flavor of Prolog that you choose.

Problem Statement

--

--

Matt Dodge

Constantly struggling to figure out whether I like software development or the Dodgers more. CTO of niolabs.