Wednesday, November 19, 2014

0hh1

Here's a solver for the puzzles at 0hh1.com.

% USAGE: clingo -n 0 <thisfile.txt # show all solutions to puzzle
%
% Download from: http://sourceforge.net/projects/potassco/files/clingo/3.0.5/

% Two colors: 1 = red, 2 = blue.  (0 = empty)
color(1). color(2).

#const size = 6.
% coord(Y,X) = Y,X is a valid coordinate of a cell in the puzzle.
coord(0..size-1,0..size-1).

% p(Y,X,C) = cell [Y,X] is color C in the original puzzle.
%- X X - - -
%O - - - - -
%- - - - - -
%- - - - O -
%- - - - - O
%- X - - - -

color(1). color(2).

#const size = 6.
% coord(Y,X) = Y,X is a valid coordinate of a cell in the puzzle.
coord(0..size-1,0..size-1).

% p(Y,X,C) = cell [Y,X] is color C in the original puzzle.
%- X X - - -
%O - - - - -
%- - - - - -
%- - - - O -
%- - - - - O
%- X - - - -

p(0,0,2).  p(0,1,1).  p(0,2,0).  p(0,3,0).  p(0,4,1).  p(0,5,2).
p(1,0,1).  p(1,1,0).  p(1,2,0).  p(1,3,2).  p(1,4,0).  p(1,5,0).
p(2,0,0).  p(2,1,2).  p(2,2,1).  p(2,3,1).  p(2,4,2).  p(2,5,0).
p(3,0,0).  p(3,1,0).  p(3,2,2).  p(3,3,0).  p(3,4,0).  p(3,5,0).
p(4,0,1).  p(4,1,0).  p(4,2,2).  p(4,3,1).  p(4,4,2).  p(4,5,0).
p(5,0,1).  p(5,1,2).  p(5,2,0).  p(5,3,0).  p(5,4,2).  p(5,5,0).

% q(Y,X,C) = cell[Y,X] is color C in the solved puzzle.

% The colors in the original puzzle must not change in the solution.
q(Y,X,C) :- p(Y,X,C); C != 0.

% Each empty square in the original puzzle must be colored red or blue in the solution.
1 { q(Y,X,C) : color(C) } 1 :- p(Y,X,0).

% f(Y,X) = cell(Y,X) is filled with some color.
f(Y,X) :- q(Y,X,C); C != 0.

% Solution cannot have empty squares.
:- coord(Y,X); not f(Y,X).

% Solution cannot have three adjacent cells of the same color horizontally.
:- q(Y,X,C); q(Y,X+1,C); q(Y,X+2,C).

% Solution cannot have three adjacent cells of the same color vertically.
:- q(Y,X,C); q(Y+1,X,C); q(Y+2,X,C).

% Exactly half of the cells in every row in the solution must be red.
:-  coord(Y,_); {coord(Y,X) : q(Y,X,2) } != size/2.

% Exactly half of the cells in every column in the solution must be red.
:- coord(_,X); {coord(Y,X) : q(Y,X,1) } != size/2.

% diffrow(Y1, Y2) = rows Y1 and Y2 differ.
diffrow(Y1, Y2) :- q(Y1,X,C1); q(Y2,X,C2); C1 != C2; coord(Y1,_); coord(Y2,_); Y1 < Y2.

% diffcol(X1, X2) = columns X1 and X2 differ.
diffcol(X1, X2) :- q(Y,X1,C1); q(Y,X2,C2); C1 != C2; coord(_,X1); coord(_,X2); X1 < X2.

% Solution cannot have duplicate rows.
:- coord(Y1,_); coord(Y2,_); Y1 < Y2; not diffrow(Y1, Y2).

% Solution cannot have duplicate columns.
:- coord(_,X1); coord(_,X2); X1 < X2; not diffcol(X1, X2).

No comments:

Post a Comment

Followers

Contributors