Lab Assignment #1

Slant puzzle

Introduction

This assignment consists in solving the Slant puzzle, also known as Gokigen (try here an online game) described as follows. We have a grid of n x n empty cells where some grid joints may contain a number from 0 to 4. The purpose of the game is to fill every cell with a diagonal line of one of the two forms: a slash "/" or a backslash "\". To do so, we must respect two constraints:
  1. Each joint with a number must exactly meet that number of diagonal lines
  2. The diagonal lines cannot form any loop
The following images show an example of initial configuration of a puzzle (5 x 5) and the final solution.
initial solved
Initial configuration
Solved puzzle

What to do

Step 1. Each input instance is an ASCII file with the following format. For a grid of n x n, the file contains the corresponding joints in n+1 lines of n+1 characters ended by a newline. Each character can be:
  • 0, 1, 2, 3, 4 = this joint must meet the corresponding number of diagonals
  • . = represents a free joint, that has no constraints
The following shows the input format for the initial configuration above:

...1..
21..12
1.2.1.
1.2..1
.1.22.
..011.


The file slantexamples.zip (NEW!) contains several input files and their solutions.

Step 2. Write a python program encode.py to transform each input ASCII file into a logic program containing facts. For instance:

python encode.py dom01.txt domain.lp

will transform the example above into the set of facts for the predicates you decide to use for representing the problem. The file domain.lp can only contain facts and constant definitions, but no conditional rules or constraints.

Step 3. Encode the tents problem in clingo in a file called slant.lp. You can use

clingo 0 slant.lp domain.lp

to obtain all the answer sets. You should check that each puzzle should have a unique solution. You can use the following file decode.py that allows printing the solution as an output text file:

python decode.py slant.lp domain.lp

To use this program, you need output facts like slash(X,Y) to represent that the cell at X, Y is filled with a slash-shaped diagonal "/". The rest of cells are filled with a backslash "\". You also need a predicate dim(N) to specify the size of the grid in number of cells -- in the example above, dim(5). The result produced by this program will have the form

/\//\
\\///
\\/\/
\\///
\/\\\


The file drawslant.py uses the python library pygame and allows you to visualize both the input domain and the solution in a completed scenario as follows:

python drawslant.py dom01.txt sol01.txt

Assessment & delivery

The maximum grade for this exercise is 20 points = 20% of the course. The deadline for delivery is Wednesday, November 25th, 2026 using the MOODLE assignment in each University. Exercises can be made by groups of 2 students at most. If so, only one student is required to deliver the files in moodle, but all source files must contain the names of the two group members.

Delivery: upload all files in a .zip including a README.txt with the student names and any additional comment you consider relevant.




Maintained by Pedro Cabalar