Created
January 7, 2022 20:32
-
-
Save AdamStelmaszczyk/a5133808c4b1a905bcd43b09469859c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
import re | |
import os | |
from collections import defaultdict | |
PARTNER_PENALTY = 2 | |
OPPONENT_PENALTY = 1 | |
partners = defaultdict(list) | |
opponents = defaultdict(list) | |
p = re.compile(r'([0-9]+)([A-Z]+)([0-9]+)([A-Z]+)') | |
for line in sys.stdin: | |
if line != os.linesep: | |
game = p.match(line).groups() | |
partners[game[0]].append(game[1]) | |
partners[game[1]].append(game[0]) | |
partners[game[2]].append(game[3]) | |
partners[game[3]].append(game[2]) | |
opponents[game[0]].append(game[1]) | |
opponents[game[0]].append(game[2]) | |
opponents[game[0]].append(game[3]) | |
opponents[game[1]].append(game[0]) | |
opponents[game[1]].append(game[2]) | |
opponents[game[1]].append(game[3]) | |
opponents[game[2]].append(game[0]) | |
opponents[game[2]].append(game[1]) | |
opponents[game[2]].append(game[3]) | |
opponents[game[3]].append(game[0]) | |
opponents[game[3]].append(game[1]) | |
opponents[game[3]].append(game[2]) | |
#print('partners', partners) | |
#print('opponents', opponents) | |
def score_dict(d, penalty): | |
score = 0 | |
for key, value in d.items(): | |
counts = defaultdict(int) | |
for opponent in value: | |
counts[opponent] += 1 | |
for count in counts.values(): | |
score -= penalty * (count - 1) | |
print(key, counts) | |
return score | |
partner_score = score_dict(partners, PARTNER_PENALTY) | |
print('partner_score', partner_score) | |
opponents_score = score_dict(opponents, OPPONENT_PENALTY) | |
print('opponents_score', opponents_score) | |
print('final score', partner_score + opponents_score) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment