Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binary-puzzle exercise #446

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 1
},
{
"slug": "binary-puzzle",
"name": "Binary Puzzle",
"uuid": "3b24f261-3916-4bbf-a52f-5758173ad83e",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
],
"foregone": [
Expand Down
21 changes: 21 additions & 0 deletions exercises/practice/binary-puzzle/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Instruction

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))](https://www.swi-prolog.org/man/clpfd.html), which is perfect for these types of tasks.

````exercism/note
To help you get started, the stub file already includes the [`clpfd` library](https://www.swi-prolog.org/man/clpfd.html) via:

```prolog
:- use_module(library(clpfd)).
```
````
6 changes: 6 additions & 0 deletions exercises/practice/binary-puzzle/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 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.
28 changes: 28 additions & 0 deletions exercises/practice/binary-puzzle/.meta/binary_puzzle.example.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
:- use_module(library(clpfd)).

binary_puzzle(Rows) :-
length(Rows, Dimension),
transpose(Rows, Cols),
maplist(same_length(Rows), Rows),
maplist(max_two_identical_consecutive_numbers, Rows),
maplist(max_two_identical_consecutive_numbers, Cols),
maplist(binary_digits, Cols, ColsAsIntegers),
maplist(binary_digits, Rows, RowsAsIntegers),
all_different(RowsAsIntegers),
all_different(ColsAsIntegers),
Half #= Dimension // 2,
maplist(zeros_and_ones_have_same_count(Half), Rows),
maplist(zeros_and_ones_have_same_count(Half), Cols).

binary_digits(Digits, Decimal) :-
Digits ins 0..1,
foldl([Digit,Acc,NewAcc]>>(NewAcc #= Acc << 1 + Digit), Digits, 0, Decimal).

zeros_and_ones_have_same_count(Count, Digits) :-
global_cardinality(Digits, [0-Count, 1-Count]).

max_two_identical_consecutive_numbers([_, _]).
max_two_identical_consecutive_numbers([X, Y, Z | Rest]) :-
X #= Y #==> Y #\= Z,
Y #= Z #==> X #\= Z,
max_two_identical_consecutive_numbers([Y, Z | Rest]).
17 changes: 17 additions & 0 deletions exercises/practice/binary-puzzle/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"erikschierboom"
],
"files": {
"solution": [
"binary_puzzle.pl"
],
"test": [
"binary_puzzle_tests.plt"
],
"example": [
".meta/binary_puzzle.example.pl"
]
},
"blurb": "Solve a binary puzzle"
}
3 changes: 3 additions & 0 deletions exercises/practice/binary-puzzle/binary_puzzle.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:- use_module(library(clpfd)).

binary_puzzle(Rows).
83 changes: 83 additions & 0 deletions exercises/practice/binary-puzzle/binary_puzzle_tests.plt
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
pending :-
current_prolog_flag(argv, ['--all'|_]).
pending :-
write('\nA TEST IS PENDING!\n'),
fail.

:- begin_tests(binary_puzzle).

test(puzzle_4x4, condition(true)) :-
Rows =
[
[_, 1, _, 0],
[_, _, 0, 1],
[_, 0, _, _],
[1, 1, _, 0]
],
binary_puzzle(Rows),
maplist(label, Rows),
Rows ==
[
[0, 1, 1, 0],
[1, 0, 0, 1],
[0, 0, 1, 1],
[1, 1, 0, 0]
].

test(puzzle_6x6, condition(pending)) :-
Rows =
[
[1, _, _, 0, _, _],
[_, _, 0, 0, _, 1],
[_, 0, 0, _, _, 1],
[_, _, _, _, _, _],
[0, 0, _, 1, _, _],
[_, 1, _, _, 0, 0]
],
binary_puzzle(Rows),
maplist(label, Rows),
Rows ==
[
[1, 0, 1, 0, 1, 0],
[0, 1, 0, 0, 1, 1],
[1, 0, 0, 1, 0, 1],
[0, 1, 1, 0, 1, 0],
[0, 0, 1, 1, 0, 1],
[1, 1, 0, 1, 0, 0]
].

test(puzzle_12x12, condition(pending)) :-
Rows =
[
[_, _, 1, _, _, _, _, 0, _, _, 0, _],
[_, _, _, 1, _, 0, _, _, 1, _, _, 1],
[_, _, 0, _, 0, _, _, _, _, 0, _, 1],
[1, _, 0, _, _, _, 0, 0, _, _, _, _],
[_, 1, _, _, _, _, _, _, _, _, _, 1],
[_, _, _, _, 1, _, _, _, _, 0, _, _],
[_, _, _, _, _, _, 1, _, 0, 0, _, _],
[_, _, 0, _, _, _, _, 1, _, _, 1, _],
[1, _, 0, 0, _, _, _, _, _, 0, _, _],
[_, _, _, _, _, 1, _, _, _, 0, _, 0],
[_, 0, _, _, _, _, 0, _, _, _, 1, _],
[0, 0, _, 1, _, 1, _, 1, _, 1, _, _]
],
binary_puzzle(Rows),
maplist(label, Rows),
Rows ==
[
[0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0],
[0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1],
[1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0],
[0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1],
[0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0],
[1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0],
[1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1],
[0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0],
[1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0],
[0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1]
].

:- end_tests(binary_puzzle).
Loading