Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I coded in SWI-Prolog</p> <pre><code>/* File: pack_squares.lp Author: Carlo,,, Created: Nov 29 2012 Purpose: http://stackoverflow.com/questions/13623775/prolog-constraint-processing-packing-squares */ :- module(pack_squares, [pack_squares/0]). :- [library(clpfd)]. pack_squares :- maplist(square, [5,4,3,2], Squares), flatten(Squares, Coords), not_overlap(Squares), Coords ins 1..10, label(Coords), maplist(writeln, Squares), draw_squares(Squares). draw_squares(Squares) :- forall(between(1, 10, Y), ( forall(between(1, 10, X), sumpts(X, Y, Squares, 0)), nl )). sumpts(_, _, [], S) :- write(S). sumpts(X, Y, [[X1,Y1, X2,Y2]|Qs], A) :- ( ( X &gt;= X1, X =&lt; X2, Y &gt;= Y1, Y =&lt; Y2 ) -&gt; B is A+X2-X1+1 ; B is A ), sumpts(X, Y, Qs, B). square(D, [X1,Y1, X2,Y2]) :- X1 + D - 1 #= X2, Y1 + D - 1 #= Y2. not_overlap([_]). not_overlap([A,B|L]) :- not_overlap(A, [B|L]), !, not_overlap([B|L]). not_overlap(_, []). not_overlap(Q, [R|Rs]) :- not_overlap_c(Q, R), not_overlap_c(R, Q), not_overlap(Q, Rs). not_overlap_c([X1,Y1, X2,Y2], Q) :- not_inside(X1,Y1, Q), not_inside(X1,Y2, Q), not_inside(X2,Y1, Q), not_inside(X2,Y2, Q). not_inside(X,Y, [X1,Y1, X2,Y2]) :- X #&lt; X1 #\/ X #&gt; X2 #\/ Y #&lt; Y1 #\/ Y #&gt; Y2. </code></pre> <p>here is the last lines displayed when running <code>?- aggregate_all(count,pack_squares,C).</code>, notably C counts total placements</p> <pre><code>... 0002255555 0002255555 [6,6,10,10] [7,2,10,5] [4,3,6,5] [5,1,6,2] 0000220000 0000224444 0003334444 0003334444 0003334444 0000055555 0000055555 0000055555 0000055555 0000055555 C = 169480. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload