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.

Show summary of puzzle runtimes

+37 -9
+37 -9
advent.py
··· 32 32 return resource.getrusage(resource.RUSAGE_CHILDREN)[0] 33 33 34 34 35 - def format_time(timespan): 35 + def color_time_str(str, timespan): 36 + if timespan >= 10: 37 + color = bcolors.FAIL 38 + elif timespan >= 1: 39 + color = bcolors.WARNING 40 + else: 41 + color = '' 42 + 43 + return '{}{}{}'.format(color, str, bcolors.ENDC) 44 + 45 + 46 + def format_time(timespan, padding=None): 36 47 """Formats the timespan in a human readable format""" 37 48 if timespan >= 1.0: 38 - return '{}{:.3g} s{}'.format( 39 - bcolors.FAIL if timespan >= 10 else bcolors.WARNING, 40 - timespan, 41 - bcolors.ENDC) 49 + time_str = '{:.3g} s'.format(timespan) 42 50 else: 43 - return '{:.3g} ms'.format(timespan * 1e3) 51 + time_str = '{:.3g} ms'.format(timespan * 1e3) 52 + 53 + if padding is not None: 54 + time_str = time_str.rjust(padding) 55 + 56 + return color_time_str(time_str, timespan) 44 57 45 58 46 59 def check_solution(program, day, input_file, output_file): ··· 77 90 else: 78 91 programs = glob.glob('%s/day*.py' % year) 79 92 80 - total_runtime = 0 93 + runtimes = [] 81 94 82 95 for program in programs: 83 96 day = int(re.findall(r'(\d+).py', program)[0]) ··· 86 99 87 100 if os.path.exists(output_file): 88 101 valid, stdout, cpu_usr = check_solution(program, day, input_file, output_file) 89 - total_runtime += cpu_usr 102 + runtimes.append(cpu_usr) 90 103 91 104 print '{}{}{} Day {:02} ({})'.format( 92 105 bcolors.OKGREEN if valid else bcolors.FAIL, ··· 101 114 exit_code = 1 102 115 103 116 if len(sys.argv) <= 2: 104 - print "Total runtime:", format_time(total_runtime) 117 + print "Total runtime:", format_time(sum(runtimes)) 118 + 119 + cutoffs = [ 120 + 0.025, 0.050, 0.075, 0.100, 0.125, 0.150, 121 + 0.200, 0.250, 0.300, 0.400, 0.500, 0.750, 122 + 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 123 + ] 124 + 125 + cutoffs.extend(range(5, 10)) 126 + cutoffs.extend(range(10, 20, 2)) 127 + cutoffs.extend(range(20, 120, 3)) 128 + 129 + for day, runtime in enumerate(runtimes, start=1): 130 + out = "Day {:02}: {}".format(day, format_time(runtime, padding=7)) 131 + bar_len = next(i + 1 for i, cutoff in enumerate(cutoffs) if runtime < cutoff) 132 + print out, color_time_str('■' * bar_len, runtime) 105 133 106 134 sys.exit(exit_code)