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.

Print program output to console

+27 -9
+27 -9
test.py
··· 9 9 import subprocess 10 10 11 11 12 + class bcolors: 13 + HEADER = '\033[95m' 14 + OKBLUE = '\033[94m' 15 + OKGREEN = '\033[92m' 16 + WARNING = '\033[93m' 17 + FAIL = '\033[91m' 18 + ENDC = '\033[0m' 19 + 20 + 12 21 def clock(): 13 22 return resource.getrusage(resource.RUSAGE_CHILDREN)[0] 14 23 15 24 16 25 def format_time(timespan): 17 26 """Formats the timespan in a human readable format""" 18 - if timespan >= 10.0: 19 - return '\033[91m%.3g s\033[0m' % timespan 20 - elif timespan >= 1.0: 21 - return '\033[93m%.3g s\033[0m' % timespan 27 + if timespan >= 1.0: 28 + return '{}{:.3g} s{}'.format( 29 + bcolors.FAIL if timespan >= 10 else bcolors.WARNING, 30 + timespan, 31 + bcolors.ENDC) 22 32 else: 23 - return '%.3g ms' % (timespan * 1e3) 33 + return '{:.3g} ms'.format(timespan * 1e3) 24 34 25 35 26 36 def check_solution(program, input_file, output_file): ··· 34 44 with open(output_file) as f: 35 45 for line in f: 36 46 if line.strip() not in stdout: 37 - return False, cpu_usr 47 + return False, stdout, cpu_usr 38 48 else: 39 - return True, cpu_usr 49 + return True, stdout, cpu_usr 40 50 41 51 42 52 def main(): ··· 59 69 output_file = '%s/outputs/%02i.txt' % (year, day) 60 70 61 71 if os.path.exists(output_file): 62 - valid, cpu_usr = check_solution(program, input_file, output_file) 63 - print '{} {} Day {:02} ({})'.format('✓' if valid else '✗', year, day, format_time(cpu_usr)) 72 + valid, stdout, cpu_usr = check_solution(program, input_file, output_file) 73 + print '{}{}{} {} Day {:02} ({})'.format( 74 + bcolors.OKGREEN if valid else bcolors.FAIL, 75 + '✓' if valid else '✗', 76 + bcolors.ENDC, 77 + year, 78 + day, 79 + format_time(cpu_usr), 80 + ) 81 + print stdout 64 82 65 83 if not valid: 66 84 exit_code = 1