Tracks
/
Prolog
Prolog
/
Exercises
/
Binary Puzzle
Binary Puzzle

Binary Puzzle

Easy

Introduction

Your grandma has recently taken a keen interest in solving binary puzzles. Whilst she's becoming better at it, she does get stuck fairly regularly. Being the helpful grandchild you are, you decide to build a small application that can solve any binary puzzle. This will allow you to give your grandma hints for her next move.

Instructions

Your task is to solve a binary puzzle. Every binary puzzle has exactly one solution, which can be found by applying these rules:

  1. Every cell must contain either a zero or a one.
  2. More than two of the same digits can't be adjacent. Thus, if two adjacents cells contain the same digit, the cells next to the digits must contain the other digit.
  3. Each row and each column must contain an equal number of zeros and ones.
  4. Each row and column is unique.

You'll get passed in an uncompleted puzzle as rows of zeros and ones, with placeholders (_) for the unknown values. The goal is to define the rules for the binary puzzle, which will let the tests figure out the values for the placeholders (you don't do that yourselves). Your implementation must useProlog's Constraint Logic Programming over Finite Domains (CLP(FD)), which is perfect for these types of tasks.

Note

To help you get started, the stub file already includes the clpfd library via:

:- use_module(library(clpfd)).
Edit via GitHub The link opens in a new window or tab
Prolog Exercism

Ready to start Binary Puzzle?

Sign up to Exercism to learn and master Prolog with 109 exercises, and real human mentoring, all for free.