% ============================================================
%  THE HARDWOOD SCHEDULER  ·  1975-76 Season  ·  clingo
%  An eight-team league built on the NBA's opponent formula,
%  shrunk down so a browser can solve it in a blink.
%
%  The program never says HOW to build a schedule. It lists
%  the games and the dates, lets clingo put games on dates,
%  and throws out any schedule that breaks a league bylaw.
% ============================================================

% ---------- the league ----------
team(njt;pit;car;ken;ind;den;san;uta).
div(east,njt). div(east,pit). div(east,car). div(east,ken).
div(west,ind). div(west,den). div(west,san). div(west,uta).

% Nov 1 to Dec 31, 1975. Day 55 is Christmas, day 61 is New Year's Eve.
#const days = 61.
day(1..days).
christmas(55).

% Bylaw 3 can be switched off from the command line: -c home_b2b=0
#const home_b2b = 1.

% Miles between arenas (great-circle, rounded).
miles(njt,pit,307). miles(njt,car,526). miles(njt,ken,641). miles(njt,ind,635). miles(njt,den,1619). miles(njt,san,1574). miles(njt,uta,1959).
miles(pit,car,363). miles(pit,ken,343). miles(pit,ind,329). miles(pit,den,1317). miles(pit,san,1291). miles(pit,uta,1664).
miles(car,ken,343). miles(car,ind,428). miles(car,den,1356). miles(car,san,1104). miles(car,uta,1724).
miles(ken,ind,107). miles(ken,den,1036). miles(ken,san,950). miles(ken,uta,1399).
miles(ind,den,998). miles(ind,san,1000). miles(ind,uta,1353).
miles(den,san,803). miles(den,uta,371).
miles(san,uta,1087).
miles(B,A,M) :- miles(A,B,M).

% ---------- who plays whom (the NBA formula, in miniature) ----------
% Division rivals: four meetings, two in each building.
% The other division: two meetings, one in each building.
% 3 rivals x 4 + 4 teams x 2 = 20 games a team, 80 in all.
rival(A,B) :- div(V,A), div(V,B), A != B.
game(H,A,1..2) :- rival(H,A).
game(H,A,1)    :- team(H), team(A), H != A, not rival(H,A).

% ---------- the one choice: every game gets exactly one date ----------
1 { on(H,A,K,D) : day(D) } 1 :- game(H,A,K).

home(H,D) :- on(H,_,_,D).
away(A,D) :- on(_,A,_,D).
plays(T,D) :- home(T,D).
plays(T,D) :- away(T,D).
meet(X,Y,D) :- on(X,Y,_,D), X < Y.
meet(X,Y,D) :- on(Y,X,_,D), X < Y.
games(D,N) :- day(D), N = #count{ H,A,K : on(H,A,K,D) }.

% The two copies of a rivalry home game are interchangeable; keep them in order.
:- on(H,A,1,D1), on(H,A,2,D2), D2 < D1.

% ============================================================
%  THE BYLAWS
% ============================================================

% 1. One game a night. Nobody plays twice on the same date.
:- team(T), day(D), 2 #count{ h,A,K : on(T,A,K,D); a,H,K : on(H,T,K,D) }.

% 2. No three games in three nights.
:- plays(T,D), plays(T,D+1), plays(T,D+2).

% 3. Back-to-backs happen at home. If a team plays two nights running,
%    both games are in its own building, so nobody plays, packs and flies.
:- home_b2b = 1, plays(T,D), plays(T,D+1), away(T,D).
:- home_b2b = 1, plays(T,D), plays(T,D+1), away(T,D+1).

% 4. No road trip longer than ten days: every ten-day stretch has a home game.
:- team(T), day(D), D+9 <= days, #count{ E : home(T,E), day(E), E >= D, E <= D+9 } = 0.

% 5. No homestand longer than twelve days: every twelve-day stretch has a road game.
:- team(T), day(D), D+11 <= days, #count{ E : away(T,E), day(E), E >= D, E <= D+11 } = 0.

% 6. Stay sharp: nobody goes a full week without a game.
:- team(T), day(D), D+6 <= days, #count{ E : plays(T,E), day(E), E >= D, E <= D+6 } = 0.

% 7. A week between rematches. The same two teams never meet twice within seven days.
:- meet(X,Y,D1), meet(X,Y,D2), D1 < D2, D2 - D1 < 7.

% 8. No more than three games a night, so the radio network can carry them all.
:- games(D,N), N > 3, D != 1, D != days.

% 9. Opening Night and New Year's Eve: all eight teams play.
:- games(1,N), N != 4.
:- games(days,N), N != 4.

% 10. Christmas doubleheader: exactly two games, both division rivalries.
:- christmas(D), games(D,N), N != 2.
:- christmas(D), on(H,A,_,D), not rival(H,A).

% ---------- travel (used by the mileage-cutting search) ----------
% A team is wherever it last played; the season starts at home.
at(T,0,T) :- team(T).
at(H,D,H) :- on(H,_,_,D).
at(A,D,H) :- on(H,A,_,D).
at(T,D,C) :- at(T,D-1,C), day(D), team(T), not plays(T,D).
trip(T,D,M) :- at(T,D-1,C1), at(T,D,C2), C1 != C2, miles(C1,C2,M).
% Everyone flies home when the season ends.
trip(T,days+1,M) :- at(T,days,C), C != T, miles(C,T,M).

#show on/4.
#show trip/3.
