diff options
| author | 2020-12-01 18:30:12 +0100 | |
|---|---|---|
| committer | 2020-12-01 18:30:12 +0100 | |
| commit | 210c564e1b6823617cb4f64ff5e33031dc61d487 (patch) | |
| tree | 80ce941d2a11cd143903ea57ab71794acfd50d84 /bot/exts | |
| parent | Clarify text of DM with Advent of Code join code (diff) | |
Add support for ignoring scores from specific days
I've added support for ignoring scores from specific days. A list of
days to ignore can be provided using the environment variable
`AOC_IGNORED_DAYS` as a comma-separated list.
This example would ignore day 1 and day 23:
AOC_IGNORED_DAYS=1,23
I've also added a helper function to sort the leaderboard not only on
the achieved score, but also on the number of stars an individual has
completed.
Diffstat (limited to 'bot/exts')
| -rw-r--r-- | bot/exts/christmas/advent_of_code/_helpers.py | 19 | 
1 files changed, 16 insertions, 3 deletions
| diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index 72c1ce20..e84348cb 100644 --- a/bot/exts/christmas/advent_of_code/_helpers.py +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -53,6 +53,17 @@ EST = pytz.timezone("EST")  _StarResult = collections.namedtuple("StarResult", "name completion_time") +def leaderboard_sorting_function(entry: typing.Tuple[str, dict]) -> typing.Tuple[int, int]: +    """ +    Provide a sorting value for our leaderboard. + +    The leaderboard is sorted primarily on the score someone has received and +    secondary on the number of stars someone has completed. +    """ +    result = entry[1] +    return result["score"], result["star_2_count"] + result["star_1_count"] + +  def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict:      """      Parse the leaderboard data received from the AoC website. @@ -98,13 +109,15 @@ def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict:      # participants per star, we can compute the rank-based scores each participant      # should get for that star.      max_score = len(leaderboard) -    for star in star_results.values(): -        for rank, star_result in enumerate(sorted(star, key=operator.itemgetter(1))): +    for(day, _star), results in star_results.items(): +        if day in AdventOfCode.ignored_days: +            continue +        for rank, star_result in enumerate(sorted(results, key=operator.itemgetter(1))):              leaderboard[star_result.name]["score"] += max_score - rank      # Since dictionaries now retain insertion order, let's use that      sorted_leaderboard = dict( -        sorted(leaderboard.items(), key=lambda t: t[1]["score"], reverse=True) +        sorted(leaderboard.items(), key=leaderboard_sorting_function, reverse=True)      )      # Create summary stats for the stars completed for each day of the event. | 
