/* Soccer puzzle in Picat. From the Swedish blog Mattebloggen "Math problem for the younger, week 44" http://mattebloggen.com/2010/11/matteproblem-for-de-yngre-vecka-44/ (translated) """ Five soccer team played a tournament where each team played against each other once. A win gave 3 points, a tie 1 point, and 0 point for a loss. Four of the team got 1, 2, 5, and 7 points. How many points did the fifth team get? """ Solution. x = [1,2,5,7,12] Matches: [0,1,0,0,0] [1,0,1,0,0] [3,1,0,1,0] [3,3,1,0,0] [3,3,3,3,0] 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 => N = 5, % the given results Res = [1,2,5,7], % Number of points for each team X = new_list(N), X :: 0..3*N, % The result of each game Matches = new_array(N, N), Matches :: [0,1,3], % Fill the knpwn results foreach(I in 1..4) X[I] #= Res[I] end, % No team plays against itself foreach(I in 1..N) Matches[I,I] #= 0 end, % Team I's loss is the gain of Team J's (and vice versa), % unless it's a tie... foreach(I in 1..N, J in 1..N, I != J) Matches[I,J] #= 3 #<=> Matches[J,I] #= 0, Matches[I,J] #= 1 #<=> Matches[J,I] #= 1 end, % Count the points for each team foreach(I in 1..N) X[I] #= sum([Matches[I,J] : J in 1..N]) end, Vars = X ++ Matches.vars, solve(Vars), println(x=X), println("Matches:"), foreach(Row in Matches) println(Row.to_list) end, nl, fail, nl.