Skip to content

Instantly share code, notes, and snippets.

@rndmcnlly
Created December 9, 2011 06:53
Show Gist options
  • Save rndmcnlly/1450537 to your computer and use it in GitHub Desktop.
Save rndmcnlly/1450537 to your computer and use it in GitHub Desktop.
Stone Network Puzzle
%%%%
%%%% PROBLEM ENCODING
%%%%
%% setup the puzzle grid
dim(1..width).
pos((X,Y)) :- dim(X;Y).
step(1;-1,0;;0,1;-1).
adj((X,Y),(X+DX,Y+DY)) :-
step(DX,DY),
pos((X,Y);(X+DX,Y+DY)).
%% guess location of black stones
black_stones { black(P):pos(P) } black_stones.
% elect a root (convenience for connectivity definition)
1 { root(P):pos(P) } 1.
black(P) :- root(P).
%% construct a solution from white stones
0 { white(P):pos(P) } white_stones.
:- black(P), white(P). % no overlap
solid(P) :- pos(P), 1 { white(P), black(P) } 1.
connected(P) :- root(P).
connected(P2) :- connected(P1), adj(P1,P2), solid(P2).
:- black(P), not connected(P). % connection required
#const width=9.
#const black_stones=8.
#const white_stones=12.
$ clingo instance.lp encoding.lp visualization.lp | lonsdaleite -cu
http://chart.googleapis.com/chart?chl=graph{node[shape=circle,style=filled,fillcolor=white,fixedsize=true];64[pos=%222,8!%22];58[pos=%225,7!%22];48[pos=%224,6!%22,fillcolor=black,fontcolor=white];47[pos=%223,6!%22];46[pos=%222,6!%22,fillcolor=black,fontcolor=white];45[pos=%221,6!%22];39[pos=%224,5!%22];37[pos=%222,5!%22];36[pos=%221,5!%22,fillcolor=black,fontcolor=white];33[pos=%227,4!%22];30[pos=%224,4!%22,fillcolor=black,fontcolor=white];27[pos=%221,4!%22,fillcolor=black,fontcolor=white];23[pos=%226,3!%22,fillcolor=black,fontcolor=white];22[pos=%225,3!%22];21[pos=%224,3!%22];19[pos=%222,3!%22];15[pos=%227,2!%22];14[pos=%226,2!%22,fillcolor=black,fontcolor=white];6[pos=%227,1!%22,fillcolor=black,fontcolor=white,shape=doublecircle];4[pos=%225,1!%22];47--48;46--47;45--46;39--48;37--46;36--45;36--37;30--39;27--36;22--23;21--30;21--22;14--23;14--15;6--15}&cht=gv:neato
%%%%
%%%% VISUALIZATION (https://github.com/rndmcnlly/Lonsdaleite)
%%%%
#begin_lua
function pos_str(x,y) return Val.new(Val.ID,[["]]..x..","..y..[[!"]]) end
#end_lua.
node_id(P,(Y-1)*width+X-1) :- pos((X,Y)), P := (X,Y).
graphviz_engine(neato).
graphviz_global_node_attr(shape,circle).
graphviz_global_node_attr(style,filled).
graphviz_global_node_attr(fillcolor,white).
graphviz_global_node_attr(fixedsize,true).
graphviz_node(I) :- solid(P), node_id(P,I).
graphviz_node_attr(I,pos,@pos_str(X,Y)) :- solid((X,Y)), node_id(P,I), P := (X,Y).
graphviz_node_attr(I,fillcolor,black) :- black(P), node_id(P,I).
graphviz_node_attr(I,fontcolor,white) :- black(P), node_id(P,I).
graphviz_node_attr(I,shape,doublecircle) :- root(P), node_id(P,I).
graphviz_edge(I1,I2) :-
I1<I2,
solid(P1;P2),
adj(P1,P2),
node_id(P1,I1;;P2,I2).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment