Skip to content

Instantly share code, notes, and snippets.

@strattondev
Last active December 26, 2015 04:29
Show Gist options
  • Save strattondev/7093348 to your computer and use it in GitHub Desktop.
Save strattondev/7093348 to your computer and use it in GitHub Desktop.
%Shane Forster - 500382833 - Section 2
%Dexter Speller-Drews - 500393730 - Section 2
%Wesley Stratton - 500294272 - Section 2
% Answers at bottom of document
% For refernece
% O = oakville
% P = pickering
% R = richmondhill
% S = scarborough
% T = toronto
% Oakville
% Since Oakville won 2 of 3 rounds prior to round 4 (which it did not participate) and we know that it lost in round 2 to Pickering, Oakville must have won rounds 1 and 3.
% Forced draws in round 4 and 5
% Pickering
% Loss in round 1
% Win in round 2
% Forced loss in round 3 due to O and P already winning in the round
% Draw in round 4 b/c Oakville is sitting out therefor they must have played
% As of round 5 it is the only team to have not sat out therefor they sit this round
% Richmond Hill
% Sits round one due to forced contraints on the other 4 teams.
% Must lose in round 2 and win in round 3; Round 2 has 2 wins already (Pickering and Toronto) forcing Richmond Hill to lose, therefore forcing a win for them in round 3
% Draw in round 4 and 5 since it sat round 1
% Scarborough
% Won is round 1 over Pickering
% Sits round 2 b/c matches O v P and T v R.
% Forced loss in round 3 due to O and P already winning in the round
% Draw in round 4 since Oakville is sitting out therefor they must have played
% Forced draw in round 5
% Toronto
% Since they lost and won in rounds 1 and 2 respectivly, since Oakville won over a team and you cannot have 3 teams winning in a single round, Toronto must have lost in round 1 to Oakville and then won in round 2 over Richmond Hill.
% Sat in round 3
% Forced draws in round 4 and 5
% ABCDE were easier to type then R# each time
% A = Round 1
% B = Round 2
% C = Round 3
% D = Round 4
% E = Round 5
% I have attempted to put the most relevant contraint at the top of each function to cause quicker breaks
verify_o(A,B,C,D,E) :-
B = 0,
A = 2,
C = 2,
D = -1,
E = 1.
verify_p(A,B,C,D,E) :-
A = 0,
B = 2,
not C = 1,
not C = -1,
not (C = -1, D = -1),
not (D = -1, E = -1),
not (C = -1, E = -1),
not D = 2, not D = 0,
not E = 2, not E = 0.
verify_r(A,B,C,D,E) :-
D = 1,
E = 1,
% No more then 1 win and 1 loss in rounds 1-3
not (A = 2, B = 2, C = 2),
not (A = 0, B = 0, C = 0),
not (A = 2, B = 2),
not (A = 0, B = 0),
not (A = 2, C = 2),
not (A = 0, C = 0),
not (B = 2, C = 2),
not (B = 0, C = 0),
not (A = -1, B = -1),
not (A = -1, C = -1),
not (B = -1, C = -1).
verify_s(A,B,C,D,E) :-
A = 2,
not B = 1,
not C = 1,
not (B = 2, C = 2),
not (B = 0, C = 0),
not D = 2, not D = 0,
not E = 2, not E = 0,
not (B = -1, C = -1), not (B = -1, D = -1), not (B = -1, E = -1),
not (C = -1, D = -1), not (C = -1, E = -1),
not (D = -1, E = -1).
verify_t(A,B,C,D,E) :-
C = -1,
not A = -1,
not B = -1,
not A = 1,
not B = 1,
not (A = 2, B = 2),
not (A = 0, B = 0),
D = 1,
E = 1.
% In this case ABCDE represent 1 of the 5 teams
verify_round(A,B,C,D,E) :-
% Not 2 sittings
not (A = -1, B = -1), not (A = -1, C = -1), not (A = -1, D = -1), not (A = -1, E = -1),
not (B = -1, C = -1), not (B = -1, D = -1), not (B = -1, E = -1),
not (C = -1, D = -1), not (C = -1, E = -1),
not (D = -1, E = -1),
% Not 3 wins
not (A = 2, B = 2, C = 2), not (A = 2, B = 2, D = 2), not (A = 2, B = 2, E = 2),
not (A = 2, C = 2, D = 2), not (A = 2, C = 2, E = 2), not (A = 2, D = 2, E = 2),
not (B = 2, C = 2, D = 2), not (B = 2, C = 2, E = 2), not (B = 2, D = 2, E = 2),
not (C = 2, D = 2, E = 2),
% Not 3 losses
not (A = 0, B = 0, C = 0), not (A = 0, B = 0, D = 0), not (A = 0, B = 0, E = 0),
not (A = 0, C = 0, D = 0), not (A = 0, C = 0, E = 0), not (A = 0, D = 0, E = 0),
not (B = 0, C = 0, D = 0), not (B = 0, C = 0, E = 0), not (B = 0, D = 0, E = 0),
not (C = 0, D = 0, E = 0),
% Not 5 draws
not (A = 1, B = 1, C = 1, D = 1, E = 1).
gen_list(A,B,C,D,E) :- dig(A), dig(B), dig(C), dig(D), dig(E),
not A = 1, not B = 1, not C = 1,
not D = 0, not D = 2,
not E = 0, not E = 2.
dig(-1). dig(0). dig(1). dig(2).
solve([[O1,O2,O3,O4,O5], [P1,P2,P3,P4,P5], [R1,R2,R3,R4,R5], [S1,S2,S3,S4,S5], [T1,T2,T3,T4,T5]]) :-
gen_list(O1,O2,O3,O4,O5),
verify_o(O1,O2,O3,O4,O5),
gen_list(P1,P2,P3,P4,P5),
verify_p(P1,P2,P3,P4,P5),
gen_list(R1,R2,R3,R4,R5),
verify_r(R1,R2,R3,R4,R5),
gen_list(S1,S2,S3,S4,S5),
verify_s(S1,S2,S3,S4,S5),
gen_list(T1,T2,T3,T4,T5),
verify_t(T1,T2,T3,T4,T5),
verify_round(O1,P1,R1,S1,T1),
verify_round(O2,P2,R2,S2,T2),
verify_round(O3,P3,R3,S3,T3),
verify_round(O4,P4,R4,S4,T4),
verify_round(O5,P5,R5,S5,T5).
print_solution([[O1,O2,O3,O4,O5], [P1,P2,P3,P4,P5], [R1,R2,R3,R4,R5], [S1,S2,S3,S4,S5], [T1,T2,T3,T4,T5]]) :-
write("Oakville"), nl, write_team_rounds(O1,O2,O3,O4,O5), team_total(O1,O2,O3,O4,O5,X), write("Total: "), write(X), nl,
write("Pickering"), nl, write_team_rounds(P1,P2,P3,P4,P5), team_total(P1,P2,P3,P4,P5,Y), write("Total: "), write(Y), nl,
write("Richmond Hill"), nl, write_team_rounds(R1,R2,R3,R4,R5), team_total(R1,R2,R3,R4,R5,Z), write("Total: "), write(Z), nl,
write("Scarborough"), nl, write_team_rounds(S1,S2,S3,S4,S5), team_total(S1,S2,S3,S4,S5,A), write("Total: "), write(A), nl,
write("Toronto"), nl, write_team_rounds(T1,T2,T3,T4,T5), team_total(T1,T2,T3,T4,T5,B), write("Total: "), write(B), nl.
write_team_rounds(A,B,C,D,E) :-
write("Round 1: "), write(A), nl,
write("Round 2: "), write(B), nl,
write("Round 3: "), write(C), nl,
write("Round 4: "), write(D), nl,
write("Round 5: "), write(E), nl.
team_total(A,B,C,D,E,X) :-
X is A + B + C + D + E + 1.
% ?- X is cputime, solve(List), print_solution(List), Y is cputime, Z is Y - X.
% X = 8.75
% List = [[2, 0, 2, -1, 1], [0, 2, 0, 1, -1], [-1, 0, 2, 1, 1], [2, -1, 0, 1, 1], [0, 2, -1, 1, 1]]
% Y = 8.75
% Z = 0.0
% Yes (0.00s cpu, solution 1, maybe more)
% No (0.02s cpu)
% Oakville
% Round 1: 2
% Round 2: 0
% Round 3: 2
% Round 4: -1
% Round 5: 1
% Total: 5
% Pickering
% Round 1: 0
% Round 2: 2
% Round 3: 0
% Round 4: 1
% Round 5: -1
% Total: 3
% Richmond Hill
% Round 1: -1
% Round 2: 0
% Round 3: 2
% Round 4: 1
% Round 5: 1
% Total: 4
% Scarborough
% Round 1: 2
% Round 2: -1
% Round 3: 0
% Round 4: 1
% Round 5: 1
% Total: 4
% Toronto
% Round 1: 0
% Round 2: 2
% Round 3: -1
% Round 4: 1
% Round 5: 1
% Total: 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment