/* Archery Puzzle in Picat. http://www.eonhq.com/m/puzzles/images/archery-puzzle.jpg Archery puzzle by Sam Loyd: """ How close can the young archer come to scoring a total of 100 - using as many arrows as she please. [The targets are: 16, 17, 23, 24, 39, 40] """ Via: The Aperiodical: "Manchester MathsJam June 2012 Recap" http://aperiodical.com/2012/06/manchester-mathsjam-june-2012-recap/ Solution (unique): x = [2,4,0,0,0,0] z = 100 i.e. 2 arrows on score 16 and 4 arrows on score 17: 2*16 + 4*17 = 100 This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => N = 6, Targets = [16, 17, 23, 24, 39, 40], Target = 100, puzzle(Targets,Target, X,Z,DiffZ), println(x=X), println(used_targets=[to_fstring("%d arrows on %d", X[I],Targets[I]) : I in 1..N, X[I] > 0]), println(z=Z), println(diffZ=DiffZ), nl. % % Check if it's an unique solution. (It is.) % go2 => Targets = [16, 17, 23, 24, 39, 40], Target = 100, puzzle(Targets,Target, _X,_Z,DiffZ), All=find_all([X2,Z2], puzzle(Targets,Target, X2,Z2,DiffZ)), println(all=All), println(len=All.len), nl. puzzle(Targets,Target, X,Z,DiffZ) => N = Targets.len, % decision variables X = new_list(N), X :: 0..10, Z #= sum([X[I]*Targets[I] : I in 1..N]), DiffZ #= abs(Target-Z), if var(DiffZ) then solve($[min(DiffZ)], X) else solve($[ff,split], X) end.