Refactored main. Added solution validation. Added .gitignore
parent
0181269a15
commit
4f8788bdee
|
@ -0,0 +1,6 @@
|
||||||
|
.*
|
||||||
|
broken
|
||||||
|
partial
|
||||||
|
*.exe
|
||||||
|
*.pyc
|
||||||
|
*.out
|
46
main.py
46
main.py
|
@ -4,10 +4,24 @@ import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
from argparse import ArgumentParser
|
||||||
|
|
||||||
|
SOLUTIONS = '.solutions.txt'
|
||||||
|
|
||||||
exs = ['py', 'exe']
|
exs = ['py', 'exe']
|
||||||
sol_pattern = re.compile(r'(\d+)_.+\.(?:{})'.format('|'.join(exs)))
|
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:
|
try:
|
||||||
subprocess.run(['pypy', '--version'],
|
subprocess.run(['pypy', '--version'],
|
||||||
stdout=subprocess.DEVNULL,
|
stdout=subprocess.DEVNULL,
|
||||||
|
@ -78,13 +92,8 @@ def run(fn, cmd=None):
|
||||||
print(proc.stderr.decode('utf-8'), end='')
|
print(proc.stderr.decode('utf-8'), end='')
|
||||||
|
|
||||||
return proc.stdout.decode('utf-8').rstrip().split('\n')[-1]
|
return proc.stdout.decode('utf-8').rstrip().split('\n')[-1]
|
||||||
|
|
||||||
def main():
|
def solve_all():
|
||||||
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('Num Solution')
|
||||||
print('---------------')
|
print('---------------')
|
||||||
for i, x in enumerate(solved, start=1):
|
for i, x in enumerate(solved, start=1):
|
||||||
|
@ -92,7 +101,30 @@ def main():
|
||||||
|
|
||||||
f = fs[x]
|
f = fs[x]
|
||||||
r = run(f)
|
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))
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue