/* No Time to Try puzzle in Picat. https://twitter.com/robeastaway/status/1447627451874545665 """ A cracking James Blond puzzle by @stecks in this week’s New Scientist Puzzle set by Katie Steckles #134 No time to try James Blond edges along the corridors of the supervillan's base, and comes to two locked doors, each with a keypad that requires a four-digit code. He will need to get through one of the doors, but there is no time to guess a four-digit code - the number of possible combinations is staggering. But wait! Some of the buttons on the keypads are visible worn down, while others look as if they have never been pressed. [ Left: 1,5,6,0 is worn down Right: 2,6,7 is worn down. ] One door has a keypad with four worn buttons, the other has three. Blond only has time to try one door, and he will have to try all the possible combinations. Which of the two keypads will give him fewer combinations to try - the one with four worn buttons, or the one with three? """ Here are two different approaches, one plain Picat using permutations/1 and one using CP. Both give the same solution. This 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 ?=> Left = [1,5,6,0], LeftPerms = permutations(Left), println(leftPerms=LeftPerms), println(left_len=LeftPerms.len), Right = [2,6,7], RightPerms = [], % Place each of the possible duplicate button as double foreach(R in Right) RightPerms := RightPerms ++ permutations([R] ++ Right) end, RightPermsUnique = RightPerms.remove_dups, println(rightPerms=RightPermsUnique), println(right_len=RightPermsUnique.len), nl. go => true. % % CP approach % go2 ?=> % Left Left = [1,5,6,0], LeftN = Left.len, LeftX = new_list(LeftN), LeftX :: Left, all_different(LeftX), AllLeft = solve_all(LeftX), println(allLeft=AllLeft), println(allLeftLen=AllLeft.len), % Right Right = [2,6,7], RightN = 4, RightX = new_list(RightN), RightX :: Right, nvalue(3,RightX), % exactly 3 different values AllRight = solve_all(RightX), println(allRight=AllRight), println(allRightLen=AllRight.len), nl. go2 => true