/* Inshi no heya grid puzzle in Picat. https://en.wikipedia.org/wiki/Inshi_no_heya "" Inshi no heya (...; lit. "factoring rooms") is a type of logic puzzle published by Nikoli. It is a specific form of the KenKen puzzle genre where every operation is implied to be multiplication. Rules Inshi no heya is played on a square grid, broken into "rooms" by heavier borders. One of every room's dimensions will be a single cell; the length or width of the room varying by room. Each room may run either horizontally or vertically, and has a small number appearing in its upper left corner. The puzzle starts with all the cells empty. The goal is to fill all the cells with nonzero single-digit numbers (1 through n, where n is the length of the grid's edge) such that: * The numbers in each room, when multiplied together, equal the small number in the upper left corner of the room * No number appears twice in a column or row """ Solution: [3,4,1,5,2] [2,1,3,4,5] [5,3,2,1,4] [1,5,4,2,3] [4,2,5,3,1] This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> puzzle(1,Grid,Products), inshi_no_heya(Grid,Products, X), foreach(Row in X) println(Row.to_list) end, nl, fail, nl. go => true. inshi_no_heya(Grid,Products, X) => N = Grid.len, X = new_array(N,N), X :: 1..N, MaxHint = Grid.flatten.max, foreach(I in 1..N) all_different([X[I,J] : J in 1..N]), all_different([X[J,I] : J in 1..N]) end, foreach(H in 1..MaxHint) T = [X[I,J] : I in 1..N, J in 1..N, Grid[I,J] == H], prod(T) #= Products[H] end, solve(X). puzzle(1,Grid,Products) :- Grid = [[ 1, 2, 2, 3, 4], [ 1, 5, 5, 6, 4], [ 7, 7, 8, 6, 4], [ 9,10, 8,11,11], [ 9,10, 8,12,12]], Products = [6,4,5,40,3,4,15,40,4,10,6,3].