Skip to content

Instantly share code, notes, and snippets.

@seppo0010
Created November 26, 2022 14:18
Show Gist options
  • Save seppo0010/57d0d3f46bd715fa3da5f9c3d80e4d0c to your computer and use it in GitHub Desktop.
Save seppo0010/57d0d3f46bd715fa3da5f9c3d80e4d0c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@seppo0010
Copy link
Author

Source:

from copy import deepcopy

import re

print('strict digraph D {')
print('  rankdir = LR;')
def slugify(text):
    text = text.replace('🇸🇦', 'sa')
    text = text.replace('🇦🇷', 'ar')
    text = text.replace('🇲🇽', 'mx')
    text = text.replace('🇵🇱', 'pl')
    return re.sub(r'[\W_]+', '_', text.lower())

POLONIA = 'Polonia'
MEXICO = 'México'
ARGENTINA = 'Argentina'
ARABIA_SAUDITA = 'Arabia Saudita'
abbr = {POLONIA: "🇵🇱", MEXICO: "🇲🇽", ARGENTINA: "🇦🇷", ARABIA_SAUDITA: "🇸🇦"}
initial = {
    POLONIA: {'points': 1, 'gd': []},
    MEXICO: {'points': 1, 'gd': []},
    ARGENTINA: {'points': 0, 'gd': ["-1"]},
    ARABIA_SAUDITA: {'points': 3, 'gd': ["1"]},
}
counter = 0

# Polonia - Arabia Saudita
# Argentina - México
# Polonia - Argentina
# México - Arabia Saudita

for r1 in [
    {'points': {POLONIA: 3}, 'winner': POLONIA},
    {'points': {ARABIA_SAUDITA: 3}, 'winner': ARABIA_SAUDITA},
    {'points': {POLONIA: 1, ARABIA_SAUDITA: 1}},
]:
    r1_teams = [POLONIA, ARABIA_SAUDITA]
    for r2 in [
        {'points': {ARGENTINA: 3}, 'winner': ARGENTINA},
        {'points': {MEXICO: 3}, 'winner': MEXICO},
        {'points': {ARGENTINA: 1, MEXICO: 1}},
    ]:
        r2_teams = [ARGENTINA, MEXICO]
        for r3 in [
            {'points': {ARGENTINA: 3}, 'winner': ARGENTINA},
            {'points': {POLONIA: 3}, 'winner': POLONIA},
            {'points': {ARGENTINA: 1, POLONIA: 1}},
        ]:
            r3_teams = [ARGENTINA, POLONIA]
            for r4 in [
                {'points': {MEXICO: 3}, 'winner': MEXICO},
                {'points': {ARABIA_SAUDITA: 3}, 'winner': ARABIA_SAUDITA},
                {'points': {MEXICO: 1, ARABIA_SAUDITA: 1}},
            ]:
                r4_teams = [MEXICO, ARABIA_SAUDITA]
                points = deepcopy(initial)
                history = []
                for (r, teams) in ((r1, r1_teams), (r2, r2_teams), (r3, r3_teams), (r4, r4_teams)):
                    for (t, p) in r['points'].items():
                        points[t]['points'] += p
                    if 'winner' in r:
                        loser = [t for t in teams if t != r['winner']][0]
                        points[r['winner']]['gd'].append(f'+g_{abbr[r["winner"]]}_{abbr[loser]}')
                        points[loser]['gd'].append(f'-g_{abbr[r["winner"]]}_{abbr[loser]}')
                        history.append(f'{abbr[r["winner"]]} > {abbr[loser]}')
                    else:
                        history.append(f'{abbr[teams[0]]} = {abbr[teams[1]]}')
                #if points[ARGENTINA]['points'] == 2:
                #    continue
                more = len([1 for (t, val) in points.items() if t != ARGENTINA and val['points'] > points[ARGENTINA]['points']])
                equal = len([1 for (t, val) in points.items() if t != ARGENTINA and val['points'] == points[ARGENTINA]['points']])
                if more >= 2: bgcolor = 'red'
                elif more + equal >= 2: bgcolor = 'yellow'
                else: bgcolor = 'green'
                for i in range(len(history)):
                    breadcrumb_prev = slugify('_'.join((slugify(history[j]) for j in range(i))))
                    breadcrumb = slugify('_'.join((slugify(history[j]) for j in range(i+1))))
                    if breadcrumb_prev == "":
                        print(f'  {breadcrumb}[label="{history[i]}"];')
                    else:
                        print(f'  {breadcrumb_prev} -> {breadcrumb};')
                        if i == len(history) - 1:
                            print(f'  {breadcrumb}[label="{history[i]}"];')
                            print(f'  {breadcrumb} -> {breadcrumb}_table;')
                            print(f'  {breadcrumb}_table[shape=none label=<')
                            print(f'    <table bgcolor="{bgcolor}">')
                            for (t, val) in sorted(points.items(), key=lambda v: -v[1]['points']):
                                gd = "".join(val['gd'])
                                if gd == "":
                                    gd = "0"
                                print(f'<tr><td>{abbr[t]}</td><td>{val["points"]}</td></tr>')
                            print(f'    </table>')
                            print(f'  >]')
                        else:
                            print(f'  {breadcrumb}[label="{history[i]}"];')
print('}')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment