/* Move one coin problem in Picat. Problem from Scam Nation "Can You Solve This Puzzle Faster Than Brian? (w/ Wayne Hoffman)" https://www.youtube.com/watch?v=R0PpXI38zuo From this configuration of coins o oo ooo oooo move exacly one coin to yield a line of coins in the reverse order, i.e. the number of collected coins from left is 4, 3, 2, and 1. (See below for the unique solution.) 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 => % 0 represents empty position Init = [1,0,1,1,0,1,1,1,0,1,1,1,1], Goal = [1,1,1,1,0,1,1,1,0,1,1,0,1], N = Init.len, % decision variables From :: 1..N, To :: 1..N, element(From,Init,1), element(To,Init,0), From #!= To, foreach(K in 1..N) (K #!= From #/\ K #!= To) #=> Goal[K] #= Init[K] end, solve([From,To]), printf("Move the coin in position %d to empty position %d\n",From,To), fail, nl. /* Perhaps one hint of the solution is that positions 2 and 12 are the only positions that are different in the lists Init and Goal... */ go2 => % 0 represents empty position Init = [1,0,1,1,0,1,1,1,0,1,1,1,1], Goal = [1,1,1,1,0,1,1,1,0,1,1,0,1], N = Init.len, foreach(K in 1..N) if Init[K] != Goal[K] then println(k=K) end end, nl, fail, nl. /* Solution: Move the coin in position 12 to empty position 2 1234567890123 positions ------------- o oo ooo oooo Init | position 12 _________v | v position 2 oooo ooo oo o Goal */