From 23a2049dfa974cab54b005e7737ec23f3abda8db Mon Sep 17 00:00:00 2001 From: Tibor Bizjak Date: Mon, 27 Mar 2023 01:50:51 +0200 Subject: [PATCH] Added main.py. Runs all solutions from lowest to highest and formats the solution. --- main.py | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100755 main.py diff --git a/main.py b/main.py new file mode 100755 index 0000000..71fea44 --- /dev/null +++ b/main.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +import os +import subprocess +import re +import sys + +exs = ['py', 'exe'] +sol_pattern = re.compile(r'(\d+)_.+\.(?:{})'.format('|'.join(exs))) + +def print_bar(i, count): + step = 1 + while count // step > 80: + step += 1 + w = count // step + j = i // step + bar = '[{}{}] {}/{}'.format('#'*(j), '.'*(w - j), i, count) + print(bar, end='\r') + return len(bar) + +def run(fn, cmd=None): + if cmd == None: + ext = fn.split('.')[-1] + if ext == 'py': + with open(fn) as f: + shebang = f.read().lstrip().split('\n')[0] + if shebang[:2] != '#!': + cmd = 'pypy ' + else: + cmd = shebang[2:].replace('python', 'pypy') + ' ' + else: + cmd = './' + + proc = subprocess.run(cmd + fn, shell=True, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + if len(proc.stderr) > 0: + if 'pypy' in cmd: + print('warning : pypy failed. trying cpython', file=sys.stderr) + print(proc.stderr) + return run(fn, cmd = cmd.replace('pypy', 'python')) + print('error : error while running solution') + print(proc.stderr) + + return proc.stdout.decode('utf-8').rstrip().split('\n')[-1] + +def main(): + matches = map(sol_pattern.fullmatch, os.listdir('.')) + fs = {int(m.group(1)) : m.group(0) for m in matches if m != None} + solved = sorted(fs.keys()) + + + print('Num Solution') + print('---------------') + for i, x in enumerate(solved, start=1): + bar_len = print_bar(i, len(fs)) + + f = fs[x] + r = run(f) + fst_str = '{:03d} {}'.format(x, r) + print(fst_str, end='') + line = ' ' * (bar_len - len(fst_str)) + print(line) + +if __name__ == '__main__': + main()