Skip to content

Instantly share code, notes, and snippets.

@seppo0010
Created November 26, 2022 13:18
Show Gist options
  • Save seppo0010/10e2d043213d956997750b1a6e0ffef9 to your computer and use it in GitHub Desktop.
Save seppo0010/10e2d043213d956997750b1a6e0ffef9 to your computer and use it in GitHub Desktop.
Combinaciones al 26/11/22, antes del partido de Polonia-Arabia Saudita
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):
    return re.sub(r'[\W_]+', '_', text.lower())

POLONIA = 'Polonia'
MEXICO = 'México'
ARGENTINA = 'Argentina'
ARABIA_SAUDITA = 'Arabia Saudita'
abbr = {POLONIA: "po", MEXICO: "me", ARGENTINA: "ar", ARABIA_SAUDITA: "as"}
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': {ARGENTINA: 1, MEXICO: 1}},
    ]:
        r2_teams = [ARGENTINA, MEXICO]
        for r3 in [
            {'points': {ARGENTINA: 3}, 'winner': ARGENTINA},
            {'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'{r["winner"]} le gana a {loser}')
                    else:
                        history.append(f'{teams[0]} empata con {teams[1]}')
                if points[ARGENTINA]['points'] != 4:
                    continue
                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>')
                            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>{t}</td><td>{val["points"]}</td><td>{gd}</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