Skip to content

Instantly share code, notes, and snippets.

@seppo0010
Last active November 28, 2022 14:49
Show Gist options
  • Save seppo0010/fbf25763a2753193201ac95356a091c9 to your computer and use it in GitHub Desktop.
Save seppo0010/fbf25763a2753193201ac95356a091c9 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

seppo0010 commented Nov 28, 2022

Source:

from copy import deepcopy

import re

print('strict digraph D {')
print('  rankdir = LR;')
def slugify(text):
    text = text.replace('๐Ÿ‡ต๐Ÿ‡น', 'pt')
    text = text.replace('๐Ÿ‡ฌ๐Ÿ‡ญ', 'gh')
    text = text.replace('๐Ÿ‡บ๐Ÿ‡พ', 'uy')
    text = text.replace('๐Ÿ‡ฐ๐Ÿ‡ท', 'kr')
    return re.sub(r'[\W_]+', '_', text.lower())

PORTUGAL = 'Portugal'
GHANA = 'Ghana'
URUGUAY = 'Uruguay'
KOREA = 'Corea del Sur'
abbr = {PORTUGAL: "๐Ÿ‡ต๐Ÿ‡น", GHANA: "๐Ÿ‡ฌ๐Ÿ‡ญ", URUGUAY: "๐Ÿ‡บ๐Ÿ‡พ", KOREA: "๐Ÿ‡ฐ๐Ÿ‡ท"}
initial = {
    PORTUGAL: {'points': 3, 'gd': ["+1"]},
    GHANA: {'points': 3, 'gd': ["-1"]},
    URUGUAY: {'points': 1, 'gd': []},
    KOREA: {'points': 1, 'gd': []},
}
counter = 0

for r1 in [
    # {'points': {KOREA: 3}, 'winner': KOREA},
    {'points': {GHANA: 3}, 'winner': GHANA},
    # {'points': {KOREA: 1, GHANA: 1}},
]:
    r1_teams = [KOREA, GHANA]
    for r2 in [
        {'points': {PORTUGAL: 3}, 'winner': PORTUGAL},
        {'points': {URUGUAY: 3}, 'winner': URUGUAY},
        {'points': {PORTUGAL: 1, URUGUAY: 1}},
    ]:
        r2_teams = [PORTUGAL, URUGUAY]
        for r3 in [
            {'points': {URUGUAY: 3}, 'winner': URUGUAY},
            {'points': {GHANA: 3}, 'winner': GHANA},
            {'points': {URUGUAY: 1, GHANA: 1}},
        ]:
            r3_teams = [URUGUAY, GHANA]
            for r4 in [
                {'points': {PORTUGAL: 3}, 'winner': PORTUGAL},
                {'points': {KOREA: 3}, 'winner': KOREA},
                {'points': {PORTUGAL: 1, KOREA: 1}},
            ]:
                r4_teams = [PORTUGAL, KOREA]
                points = deepcopy(initial)
                history = []
                for (r, teams) in ((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]]}')
                more = len([1 for (t, val) in points.items() if t != URUGUAY and val['points'] > points[URUGUAY]['points']])
                equal = len([1 for (t, val) in points.items() if t != URUGUAY and val['points'] == points[URUGUAY]['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