Analyzing geography
Let's use logic programming to build a solver to analyze geography. In this problem, we will specify information about the location of various states in the US and then query the program to answer various questions based on those facts and rules. The following is a map of the US:
Figure 5: Adjacent and coastal states example map
You have been provided with two text files named adjacent_states.txt
and coastal_states.txt
. These files contain the details about which states are adjacent to each other and which states are coastal. Based on this, we can get interesting information like "What states are adjacent to both Oklahoma and Texas?" or "Which coastal state is adjacent to both New Mexico and Louisiana?"
Create a new Python file and import the following:
from logpy import run, fact, eq, Relation, var
Initialize the relations:
adjacent = Relation()
coastal = Relation()
Define the input...