Skip to content

Instantly share code, notes, and snippets.

@katrinafyi
Created November 28, 2024 10:26
Show Gist options
  • Save katrinafyi/268e8c3a701781cc782cb31da06b57be to your computer and use it in GitHub Desktop.
Save katrinafyi/268e8c3a701781cc782cb31da06b57be to your computer and use it in GitHub Desktop.
codejam archive maker
#!/usr/bin/env python3
# vim: ts=2 sts=2 et sw=2
from collections import defaultdict
import os
import re
import sys
import json
'''
usage:
./make.py kaitrna_data.json 'UQCS CodeJam 2019' challenges2019.txt
where kaitrna_data.json is the archive export from hackerrank.
'''
data_file = sys.argv[1]
contest = sys.argv[2]
challenges_file = sys.argv[3]
def slug(value: str):
value = re.sub(r'[^\w\s-]', '', value).strip().lower()
return (re.sub(r'[-\s]+', '-', value))
with open(data_file) as f:
data = json.load(f)
# XXX: assume submissions are sorted chronologically
submissions = [
x for x in data['submissions'] if x['contest'] == contest
]
# print([x['challenge'] for x in (submissions)])
with open(challenges_file) as f:
(_,place), *rows = [x.strip().split('\t')[:2] for x in f.readlines()]
print(place)
print(rows)
challenges = [x[0] for x in rows]
os.mkdir(slug(contest))
os.chdir(slug(contest))
extensions = {
'python3': 'py',
'pypy3': 'py',
'visualbasic': 'vbs',
'haskell': 'hs',
'java15': 'java',
'c': 'c',
}
attempts = defaultdict(int)
def slugify(x):
chal = x['challenge']
i = challenges.index(chal)+1
s = slug(chal)
attempts[chal] += 1
fname = f'{i:0>2}_{s}_attempt{attempts[chal]}_score{x['score']}.{extensions[x['language']]}'
with open(fname, 'w') as f:
f.write(x['code'])
print([(x['challenge'], challenges.index(x['challenge'])) for x in (submissions)])
print([slugify(x) for x in (submissions)])
with open('README.md', 'w') as f:
f.write(f'# {slug(contest)}\n')
f.write(f'{place}\n\n')
f.write(f'In file names, "score" is a fraction of 1.0.\n\n')
# lens = [0,0]
# for r in rows:
# lens[0] = max(lens[0], len(r[0]))
# lens[1] = max(lens[1], len(r[1]))
f.write(f'| # | Challenge | Score (Time) |\n')
f.write(f'|---|-----------|--------------|\n')
for i,r in enumerate(rows):
f.write(f'| {i+1} | {r[0]} | {r[1]} |\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment