Skip to content

Instantly share code, notes, and snippets.

@sairam4123
Created October 28, 2024 04:14
Show Gist options
  • Save sairam4123/c98d7b05bda3a81f1defc67e793c36c9 to your computer and use it in GitHub Desktop.
Save sairam4123/c98d7b05bda3a81f1defc67e793c36c9 to your computer and use it in GitHub Desktop.
import itertools
import random
logs = []
old_print = print
def new_print(*args, **kwargs):
logs.append((args, kwargs))
print = new_print
clubs = [f"Team {no+1}" for no in range(36)]
pots = list(itertools.batched(clubs, 6))
print("Clubs:", clubs)
def get_club_choices(clubs, club, matches):
available_clubs = [p_club for p_club in clubs if p_club != club]
for match in matches:
home_club, away_club, _ = match
if home_club in available_clubs:
available_clubs.remove(home_club)
elif away_club in available_clubs:
available_clubs.remove(away_club)
return available_clubs
def schedule_match(club, oppo_club, m_type, matches, flattened_matches):
count_club = 0
count_oppo_club = 0
for match in flattened_matches:
home_club, away_club, _ = match
if home_club == club and away_club == oppo_club:
print("Trying to schedule already scheduled match")
return False
if home_club == oppo_club and away_club == club:
print("Trying to schedule opposite match for already scheduled match")
return False
if home_club == club and m_type == "home":
count_club += 1
if away_club == club and m_type == "away":
count_club += 1
if home_club == oppo_club and m_type == "away":
count_oppo_club += 1
if away_club == oppo_club and m_type == "home":
count_oppo_club += 1
print(f" [DEBUG] Club Count: {count_club} Oppo Club Count: {count_oppo_club}")
if count_club < 3 and count_oppo_club < 3:
if m_type == "home":
print(f" Confirming match.. {club} vs {oppo_club} ({m_type})")
matches.append((club, oppo_club, "home"))
flattened_matches.append((club, oppo_club, "home"))
return True
elif m_type == "away":
print(f" Confirming match.. {oppo_club} vs {club} ({m_type})")
matches.append((oppo_club, club, "away"))
flattened_matches.append((oppo_club, club, "away"))
return True
else:
print(f" Rejecting match.. {club} vs {oppo_club} ({m_type})")
print(f" Reason: Club Count: {count_club} Oppo Club Count: {count_oppo_club}")
return False
def generate_matches():
unscheduled_matches = []
flattened_unscheduled_matches = []
for slot_idx in range(6):
matches = []
for club_idx, club in enumerate(clubs):
print(f"Generating for club: {club} for slot {slot_idx}.")
for index, pot in enumerate(pots):
# choose club from pot
oppo_clubs = get_club_choices(pot, club, matches)
if not oppo_clubs:
print(f" {club} has generated all matches. Ignoring...")
continue
scheduled = False
while not scheduled:
print(f" Available options: {oppo_clubs}")
if not oppo_clubs:
print(f" {club} has exhausted all options. Continuing...")
break
oppo_club = random.choice(oppo_clubs)
print(f" Oppo club: {oppo_club}")
if index % 2 == (club_idx >= 18):
scheduled = schedule_match(club, oppo_club, "home", matches, flattened_unscheduled_matches)
else:
scheduled = schedule_match(club, oppo_club, "away", matches, flattened_unscheduled_matches)
if not scheduled:
oppo_clubs.remove(oppo_club)
unscheduled_matches.append(matches)
return len(flattened_unscheduled_matches)
import time
len_list = []
before_time = time.time()
for _ in range(100):
len_list.append(generate_matches())
after_time = time.time()
old_print(f"Time taken: {after_time - before_time: .2f}")
old_print(f"Time per match gen: {(after_time - before_time) / 100: .2f}")
old_print("Average Match Gen Length: ",sum(len_list)/100)
old_print("Consistency: ", (sum(match_len == 108 for match_len in len_list) / 100) * 100)
# old_print(unscheduled_matches)
# old_print(f"Matches generated: {len(unscheduled_matches)}")
# old_print(f"Matches generated: {len(flattened_unscheduled_matches)}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment