Refactored main. Added solution validation. Added .gitignore

master
Tibor Bizjak 2023-04-01 23:48:15 +02:00
parent 0181269a15
commit 4f8788bdee
2 changed files with 45 additions and 7 deletions

6
.gitignore vendored 100644
View File

@ -0,0 +1,6 @@
.*
broken
partial
*.exe
*.pyc
*.out

46
main.py
View File

@ -4,10 +4,24 @@ import os
import subprocess
import re
import sys
from argparse import ArgumentParser
SOLUTIONS = '.solutions.txt'
exs = ['py', 'exe']
sol_pattern = re.compile(r'(\d+)_.+\.(?:{})'.format('|'.join(exs)))
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())
try:
with open(SOLUTIONS) as f:
lines = (line.split() for line in f.read().rstrip().split('\n'))
solutions = {int(line[0]) : ' '.join(line[1:]) for line in lines}
except FileNotFoundError:
solutions = dict()
try:
subprocess.run(['pypy', '--version'],
stdout=subprocess.DEVNULL,
@ -78,13 +92,8 @@ def run(fn, cmd=None):
print(proc.stderr.decode('utf-8'), end='')
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())
def solve_all():
print('Num Solution')
print('---------------')
for i, x in enumerate(solved, start=1):
@ -92,7 +101,30 @@ def main():
f = fs[x]
r = run(f)
if x in solutions.keys() and r != solutions[x]:
wrong_err = 'error : wrong solution for {}, expected {}'
print(wrong_err.format(x, solutions[x]))
print('{:>3d} {}'.format(x, r))
parser = ArgumentParser()
parser.description = \
"""Solves all Project Euler problems with provided solutions.
A solution is an executable or python file in the same directory as this file,
with a name of the format XXX_decription.[exe|py], where XXX is the number of
the problem.
The problems are available at https://projecteuler.net/"""
parser.add_argument('--get-solutions',
action='store_true',
help='downloads solutions for user')
def main():
args = parser.parse_args()
if args.get_solutions:
print('Fetching solutions')
else:
solve_all()
if __name__ == '__main__':
main()