My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add simple test runner

+60
+60
test.py
··· 1 + #!/usr/bin/env python 2 + # -*- coding: utf-8 -*- 3 + 4 + import os 5 + import re 6 + import sys 7 + import glob 8 + import math 9 + import resource 10 + import subprocess 11 + 12 + 13 + def clock(): 14 + return resource.getrusage(resource.RUSAGE_CHILDREN)[0] 15 + 16 + 17 + def human_time(timespan): 18 + """Formats the timespan in a human readable format""" 19 + if timespan >= 1.0: 20 + return '%.*g s' % (3, timespan * 1.0) 21 + else: 22 + return '%.*g ms' % (3, timespan * 1e3) 23 + 24 + 25 + def check_solution(program, input_file, output_file): 26 + cmd = ['python', program, input_file] 27 + start = clock() 28 + proc = subprocess.Popen(cmd, stdout=subprocess.PIPE) 29 + stdout = proc.communicate()[0] 30 + end = clock() 31 + cpu_usr = end - start 32 + 33 + with open(output_file) as f: 34 + for line in f: 35 + if line.strip() not in stdout: 36 + return False, cpu_usr 37 + else: 38 + return True, cpu_usr 39 + 40 + 41 + def main(): 42 + exit_code = 0 43 + 44 + for program in glob.glob('2016/day*.py'): 45 + day = int(re.findall(r'(\d+).py', program)[0]) 46 + input_file = '2016/inputs/%02i.txt' % day 47 + output_file = '2016/outputs/%02i.txt' % day 48 + 49 + if os.path.exists(output_file): 50 + valid, cpu_usr = check_solution(program, input_file, output_file) 51 + print '{} Day {:02} ({})'.format('✓' if valid else '✗', day, human_time(cpu_usr)) 52 + 53 + if not valid: 54 + exit_code = 1 55 + 56 + sys.exit(exit_code) 57 + 58 + 59 + if __name__ == '__main__': 60 + main()