/* Stars and arrows in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 106. Stars and arrows Enter a star in some empty cells of the grid. Each arrow points to at least one star. Arrows point to a whole row, column, or diagonal, also through other stars and arrows. The numbers on the left and top of the grid indicate how many stars are located in the row or column. (taken from Kleber (2013)) (Fig. 10.19) """ The puzzle is 1 2 3 2 0 2 se sw 2 e nw 3 0 n 1 ne nw w The arrows: - e: east - n: north - ne: north east - nw: north west - se: south east - sw: south west - w: west This is the unique solution, "*" represents the stars 1 2 3 2 0 --------------- se * sw * . | 2 . e * * nw | 2 * * * . . | 3 . n . . . | 0 ne . * nw w | 1 See shinro.pi with a slightly different model. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. import cp. main => go. go => puzzle(1,Puzzle,Rows,Cols), N = Puzzle.len, X = new_array(N,N), X :: 0..1, foreach(I in 1..N) sum([X[I,J] : J in 1..N]) #= Rows[I], sum([X[J,I] : J in 1..N]) #= Cols[I] end, % The arrows directs to the complete row/column/diagonal foreach(I in 1..N, J in 1..N, nonvar(Puzzle[I,J])) A = 0, B = 0, foreach(D in atom_chars(Puzzle[I,J])) X[I,J] #= 0, dir(D,[A2,B2]), A := A + A2, B := B + B2 end, % The row/column/diagonal in the direction of the arrow T = [], AT = I, BT = J, while(AT+A >= 1, AT+A <= N, BT+B >= 1, BT+B <= N) AT := AT + A, BT := BT + B, T := T ++ [X[AT,BT]] end, if T.len > 0 then sum(T) #>= 1 end end, println(solve), solve(X.vars), foreach(J in 1..N) printf("%3d",Cols[J]) end, nl, println(" ---------------"), foreach(I in 1..N) foreach(J in 1..N) if X[I,J] == 1 then printf("%3s","*") elseif nonvar(Puzzle[I,J]) then printf("%3s", atom_chars(Puzzle[I,J])) else printf("%3s",".") end end, printf(" | %2d\n", Rows[I]) end, nl, fail, nl. dir(n,[-1,0]). dir(s,[1,0]). dir(e,[0,1]). dir(w,[0,-1]). puzzle(1,Puzzle,Rows,Cols) => Puzzle = [[se, _,sw,_,_ ,_ ], [_, e, _,_ ,nw ], [_, _, _,_ ,_ ], [_, n, _,_ ,_ ], [ne, _, _, nw ,w ] ], Rows = [2,2,3,0,1], Cols = [1,2,3,2,0].