%This is an implementation of the first step in the modelling of ACC '98
get_patterns(Pattern):-
	Pattern = [Home, Away, Bye], 

	Home = [H1,H2,H3,H4,H5,H6,H7,H8,H9,H10,H11,H12,H13,H14,H15,H16,H17,H18], 
	Away = [A1,A2,A3,A4,A5,A6,A7,A8,A9,A10,A11,A12,A13,A14,A15,A16,A17,A18],  
	Bye  = [B1,B2,B3,B4,B5,B6,B7,B8,B9,B10,B11,B12,B13,B14,B15,B16,B17,B18], 
	%M = [[1,8], [2,9], [3,12], [4,13], [5,14], [6,15], [7,16], [10,17], [11,18]],

	Home in [0..1], 
	Away in [0..1], 
	Bye  in [0..1], 

	%makes sure that each day a team only plays one game or bye
	one_game_day(Home, Away, Bye), 

	%makes sure that on the second leg days the games are the same as in the first leg corresponding days
	%second_leg(Home, Away, Bye, M), 
	%H1 #= A10,H2 #= A11,H3 #= A12,H4 #= A13,H5 #= A14,H6 #= A15,H7 #= A16,H8 #= A17,H9 #= A18,
	%H10 #= A1,H11 #= A2,H12 #= A3,H13 #= A4,H14 #= A5,H15 #= A6,H16 #= A7,H17 #= A8,H18 #= A9,	
	
	H1 #= A8,H2 #= A9,H3 #= A12,H4 #= A13,H5 #= A14,H6 #= A15,H7 #= A16,H8 #= A1,H9 #= A2,
	H10 #= A17,H11 #= A18,H12 #= A3,H13 #= A4,H14 #= A5,H15 #= A6,H16 #= A7,H17 #= A10,H18 #= A11,	
	
	%3
	H1 + H2 + H3 #>= 1,
	H16 + H17 + H18 #>= 1,
	A17 + A18 #< 2,

	%4
	consecutive_games(Home), 
	consecutive_games(Away), 
	consecutive_games5(Home, Bye), 
	consecutive_games4(Away, Bye),

	%5
	get_even_list(Home, HomeSat), 
	sum(HomeSat) #=4, 

	get_even_list(Away, AwaySat), 
	sum(AwaySat) #= 4, 

	get_even_list(Bye, ByeSat), 
	sum(ByeSat) #= 1, 

	%6
	sum([H2, H4, H6, H8, H10, B2, B4, B6, B8, B10]) #>=2, 

	%7
	B1 + A18 #< 2, 
	B1 + H17 #< 2, 
	B16 + A18 #< 2,

	labeling(Home), 
	labeling(Away), 
	labeling(Bye).
	%writeln(Home),
	%writeln(Away),
	%writeln(Bye), 
	

%makes sure that only one game is played every day
one_game_day([], [], []).
one_game_day([H|Home], [A|Away], [B|Bye]):-
	H + A + B #= 1, 
	one_game_day(Home, Away, Bye).

consecutive_games([_,_]).
consecutive_games([H1,H2,H3|Home]):-
	H1 + H2 + H3 #< 3, 
	consecutive_games([H2,H3|Home]).

consecutive_games5([_,_,_,_],[_,_,_,_]).
consecutive_games5([X1,X2,X3,X4,X5|L1], [Y1,Y2,Y3,Y4,Y5|L2]):-
	X1 + X2 + X3 + X4 + X5 + Y1 + Y2 +Y3 + Y4 + Y5 #<5,
	consecutive_games5([X2,X3,X4,X5|L1], [Y2,Y3,Y4,Y5|L2]).

consecutive_games4([_,_,_],[_, _, _]).
consecutive_games4([X1,X2,X3,X4|L1], [Y1,Y2,Y3,Y4|L2]):-
	X1 + X2 + X3 + X4 + Y1 + Y2 +Y3 + Y4 #<4,
	consecutive_games4([X2,X3,X4|L1], [Y2,Y3,Y4|L2]).
	
get_even_list([_,H2,_,H4,_,H6,_,H8,_,H10,_,H12,_,H14,_,H16,_,H18],[H2,H4,H6,H8,H10,H12,H14,H16,H18]).

