Skip to content

Instantly share code, notes, and snippets.

@saska-gist
Created November 24, 2018 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saska-gist/0051e9d87d99ddd3a2fb37aac35d1cd1 to your computer and use it in GitHub Desktop.
Save saska-gist/0051e9d87d99ddd3a2fb37aac35d1cd1 to your computer and use it in GitHub Desktop.
# A scip model for solving the puzzle at
# https://www.puzzleprime.com/brain-teasers/deduction/the-ping-pong-puzzle/
set players := { 'A','B','C' };
param NM := 21; # 21 = (10+15+17)/2 number of matches
set matches := { 1..NM };
set m3p := matches*players*players*players;
set m2p := matches*players*players;
# two play at a time, one doesn't
# x[m,p1,p2,p3] = 1 means that p1 and p2 were the players of the m-th game,
# with p3 watching
var x[m3p] binary;
# who won the m-th match between p1 and p2?
# 1 means the first player (p1) won
var w[matches] binary;
# players must be different
subto t1:forall <m,p1,p2,p3> in m3p
with (p1==p2 or p2==p3 or p1==p3) do x[m,p1,p2,p3]==0;
# the winner of each match stays on the table and
# will play the next match with the third player
subto t2:forall <m,p1,p2,p3> in m3p with m < NM do
x[m+1,p1,p2,p3] ==
# either p1 won the previous match as first player
x[m,p1,p3,p2]*w[m] +
# or p1 won the previous match as second player
x[m,p3,p1,p2]*(1-w[m]);
# A played 10 matches in total
subto nma: sum <m,p1,p2> in m2p: (x[m,'A',p1,p2] + x[m,p1,'A',p2]) == 10;
# B played 15 matches in total
subto nmb: sum <m,p1,p2> in m2p: (x[m,'B',p1,p2] + x[m,p1,'B',p2]) == 15;
# C played 17 matches in total
subto nmc: sum <m,p1,p2> in m2p: (x[m,'C',p1,p2] + x[m,p1,'C',p2]) == 17;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment