#!/usr/bin/env python3

# This script is used to determine the raffle prize winners fo the 5th ProgNova Programming Contest.
# https://prognova.io/2020

import random

# Prize count settings
NA_SOLVE4_RAFFLE_COUNT = 3
NA_SOLVE2_RAFFLE_COUNT = 5
OPEN_SOLVE4_RAFFLE_COUNT = 2
OPEN_SOLVE2_RAFFLE_COUNT = 3

# Random seed values that are dependent on the contestant standings at https://prognova20.kattis.com/standings.
# The sum of "solved" as it apears at bottom of the standings page.
# This is the total number of problems solved by all contestants.
na_total_solved = 51 + 42 + 29 + 8 + 6 + 6 + 23 + 1
open_total_solved = 11 + 13 + 12 + 3 + 2 + 2 + 9 + 1
# Sum up the solved counts in both divisions.
# Use this number with "prognova20_" prefix as the random seed.
random.seed('prognova20_%d' % (na_total_solved + open_total_solved))

# **updated: Filled in the contest results.
# The randomness does not depend on list element values.
# Below we use ranks instead of names as IDs to keep the code length short.

# List of contestant names who are the North America Division top 3 winners
na_top3 = ['na%d' % i for i in range(1, 3 + 1)]
# List of contestant names who solved >= 4 problems in the North America Division
na_solve4 = ['na%d' % i for i in range(1, 21 + 1)]
# List of contestant names who solved >= 2 and < 4 problems in the North America Division
na_solve2 = ['na%d' % i for i in range(22, 42 + 1)]
# List of contestant names who solved >= 4 problems in the Open Division
open_solve4 = ['open%d' % i for i in range(1, 8 + 1)]
# List of contestant names who solved >= 2 and < 4 problems in the Open Division
open_solve2 = ['open%d' % i for i in range(9, 13 + 1)]

def is_not_na_top3(contestant):
  return contestant not in na_top3

def is_eligible(contestant):
  """
  Returns true if the contestant is eligible to receive a prize.
  This only returns false when the ProgNova contest has determined that a contestant has registered for the North America Division incorrectly.
  If this happens, the contestant will not join the raffle of either divison.
  """
  return True

na_solve4 = filter(lambda contestant: is_not_na_top3(contestant) and is_eligible(contestant), na_solve4)
na_solve4 = list(na_solve4) # *updated
na_solve2 = filter(lambda contestant: is_not_na_top3(contestant) and is_eligible(contestant), na_solve2)
na_solve2 = list(na_solve2) # *updated

# Draw 3 North America Division 4-problem raffle winners.
random.shuffle(na_solve4)
na_solve4_winners = na_solve4[:NA_SOLVE4_RAFFLE_COUNT]

# Add those who solved 4 problems but did not win the 4-problem raffles to the 2-problem raffle pool.
na_solve2 += na_solve4[NA_SOLVE4_RAFFLE_COUNT:]
# Draw 5 North America Division 2-problem raffle winners.
random.shuffle(na_solve2) # *updated
na_solve2_winners = na_solve2[:NA_SOLVE2_RAFFLE_COUNT]

# Draw 2 Open Division 4-problem raffle winners.
random.shuffle(open_solve4)
open_solve4_winners = open_solve4[:OPEN_SOLVE4_RAFFLE_COUNT]

# Add those who solved 4 problems but did not win the 4-problem raffles to the 2-problem raffle pool.
open_solve2 += open_solve4[OPEN_SOLVE4_RAFFLE_COUNT:]
# Draw 3 Open Division 2-problem raffle winners.
random.shuffle(open_solve2) # *updated
open_solve2_winners = open_solve2[:OPEN_SOLVE2_RAFFLE_COUNT]

# Display results
print ('North America Division 4-problem raffle winners', na_solve4_winners)
print ('North America Division 2-problem raffle winners', na_solve2_winners)
print ('Open Division 4-problem raffle winners', open_solve4_winners)
print ('Open Division 2-problem raffle winners', open_solve2_winners)

# *updated: Fixed based on gultai4ukr, https://codeforces.com/blog/entry/73474?#comment-579020
