/* Drive Ya Nuts puzzle in Picat. From http://www.samstoybox.com/toys/DriveYaNuts.html """ The Drive Ya Nuts puzzle by Milton Bradley was cool and quite difficult. The object of the puzzle is to place all seven nuts such that the numbers on all sides of each nut match the numbers on the adjoining nut. There is but one way to solve the puzzle. Here are two versions of puzzle. Note that the second one is still factory sealed and shows the solution. So you think it sounds easy? """ Some other links: - http://www.jaapsch.net/puzzles/circus.htm This program is a translation of the Prolog version from https://llaisdy.wordpress.com/2015/01/13/that-giants-causeway-puzzle-in-prolog/ Here is one - of 12 - solutions: """ 33 [white,blue,green,yellow,black,red] 66 [yellow,green,black,blue,white,red] --- 22 [green,blue,red,white,black,yellow] 11 [white,black,red,green,blue,yellow] 77 [green,white,yellow,red,blue,black] --- 44 [blue,black,yellow,red,white,green] 55 [red,blue,black,yellow,green,white] """ Cf a CP approach: http://hakank.org/drive_ya_nuts.pi . This Picat model 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 ?=> solveit, nl, fail. go => true. %% tile(Name, Colours). % tiles from picture % tile(11, [red, blue, black, yellow, green, white]). % tile(22, [white, black, yellow, green, blue, red]). % tile(33, [green, blue, black, yellow, red, white]). % tile(44, [white, black, red, green, blue, yellow]). % tile(55, [white, blue, green, yellow, black, red]). % tile(66, [white, yellow, red, blue, black, green]). % tile(77, [red, yellow, green, black, blue, white]). % re-jigged a bit index(-,-) tile(11, [white, black, red, green, blue, yellow]). tile(22, [white, black, yellow, green, blue, red]). tile(33, [white, blue, green, yellow, black, red]). tile(44, [white, green, blue, black, yellow, red]). tile(55, [white, red, blue, black, yellow, green]). tile(66, [white, red, yellow, green, black, blue]). tile(77, [white, yellow, red, blue, black, green]). %% only six rotations allowed index(-) rotation(0). rotation(1). rotation(2). rotation(3). rotation(4). rotation(5). %% rotate(List1, NStepsAntiClockwise, List2). rotate(Cs, 0, Cs1) ?=> Cs = Cs1. rotate([A,B,C,D,E,F], 1, Cs1) ?=> Cs1 = [B,C,D,E,F,A]. rotate([A,B,C,D,E,F], 2, Cs1) ?=> Cs1 = [C,D,E,F,A,B]. rotate([A,B,C,D,E,F], 3, Cs1) ?=> Cs1 = [D,E,F,A,B,C]. rotate([A,B,C,D,E,F], 4, Cs1) ?=> Cs1 = [E,F,A,B,C,D]. rotate([A,B,C,D,E,F], 5, Cs1) => Cs1 = [F,A,B,C,D,E]. %% colour(Name, Rotation, Position, Colour). colour(N, R, P, C) => rotation(R), tile(N, Cs), X is (P + R) mod 6, nth0(X, Cs, C). same_colour(N1, R1, P1, N2, R2, P2) => colour(N1, R1, P1, C), colour(N2, R2, P2, C). solvex(N1, R1, N2, R2, N3, R3, N4, R4, N5, R5, N6, R6, N7, R7) => all_different([N1, N2, N3, N4, N5, N6, N7]), same_colour(N1, R1, 3, N2, R2, 0), same_colour(N1, R1, 4, N4, R4, 1), same_colour(N1, R1, 5, N3, R3, 2), same_colour(N2, R2, 4, N5, R5, 1), same_colour(N2, R2, 5, N4, R4, 2), same_colour(N3, R3, 3, N4, R4, 0), same_colour(N3, R3, 4, N6, R6, 1), same_colour(N4, R4, 3, N5, R5, 0), same_colour(N4, R4, 4, N7, R7, 1), same_colour(N4, R4, 5, N6, R6, 2), same_colour(N5, R5, 5, N7, R7, 2), same_colour(N6, R6, 3, N7, R7, 0). show(N, R) => tile(N, Cs), rotate(Cs, R, Rs), printf("%d %w%n", N, Rs). solveit => solvex(N1, R1, N2, R2, N3, R3, N4, R4, N5, R5, N6, R6, N7, R7), % solve([ffd,split],[N1, R1, N2, R2, N3, R3, N4, R4, N5, R5, N6, R6, N7, R7]), show(N1, R1), show(N2, R2), println("---"), show(N3, R3), show(N4, R4), show(N5, R5), println("---"), show(N6, R6), show(N7, R7). nth0(0, [Head|_], Head1) => Head1 = Head. nth0(N, [_|Tail], Elem) ?=> nonvar(N), M is N-1, nth0(M, Tail, Elem). nth0(N,[_|T],Item) => % Clause added KJ 4-5-87 to allow mode var(N), % nth0(-,+,+) nth0(M,T,Item), N is M + 1.