|
1 | 1 | #!/usr/bin/env python
|
2 | 2 |
|
3 | 3 | import os
|
| 4 | +import time |
4 | 5 | from argparse import ArgumentParser
|
5 | 6 |
|
6 | 7 | script_path = os.path.dirname(os.path.realpath(__file__))
|
7 | 8 | input_path = os.path.join(script_path, 'inputs')
|
8 | 9 |
|
9 |
| -if __name__ == '__main__': |
10 |
| - parser = ArgumentParser() |
11 |
| - parser.add_argument('--day', '-d', type=int, help='Day of the puzzle to run') |
12 |
| - parser.add_argument('--part', '-p', type=int, help='Part of the puzzle of the day to run') |
13 |
| - args = parser.parse_args() |
14 |
| - |
15 |
| - puzzle_file = "day{}".format(args.day) |
16 |
| - puzzle_part_func = "part{}".format(args.part) |
17 |
| - input_file = os.path.join(input_path, puzzle_file) |
18 |
| - |
19 |
| - puzzle = __import__(name=puzzle_file) |
| 10 | +def get_input(day): |
| 11 | + input_file = os.path.join(input_path, "day{}".format(day)) |
20 | 12 | puzzle_input = None
|
21 | 13 | with open(input_file, 'r') as f:
|
22 | 14 | puzzle_input = f.read()
|
| 15 | + return puzzle_input |
| 16 | + |
| 17 | +def get_solver(day, part): |
| 18 | + puzzle_file = "day{}".format(day) |
| 19 | + puzzle_part_func = "part{}".format(part) |
23 | 20 |
|
| 21 | + puzzle = __import__(name=puzzle_file) |
24 | 22 | solve_func = getattr(puzzle, puzzle_part_func)
|
25 |
| - solution = solve_func(puzzle_input) |
| 23 | + return solve_func |
| 24 | + |
| 25 | +def timeit(func, arg): |
| 26 | + start = time.time() |
| 27 | + result = func(arg) |
| 28 | + end = time.time() |
| 29 | + elapsed = (end - start) * 1000 |
| 30 | + return result, elapsed |
| 31 | + |
| 32 | +def get_message(day, part, solution, elapsed): |
| 33 | + title = "Day {} - part {}".format(day, part) |
| 34 | + title = "{}\n".format(title) + "=" * len(title) |
| 35 | + result = "solution : {}".format(solution) |
| 36 | + time = "time : {0:.4f} ms".format(elapsed) |
| 37 | + |
| 38 | + message = "{}\n\n{}\n{}\n".format(title, result, time) |
| 39 | + return message |
| 40 | + |
| 41 | +if __name__ == '__main__': |
| 42 | + parser = ArgumentParser() |
| 43 | + parser.add_argument('--day', '-d', type=int, help='Day of the puzzle to run') |
| 44 | + parser.add_argument('--part', '-p', type=int, help='Part of the puzzle of the day to run') |
| 45 | + args = parser.parse_args() |
26 | 46 |
|
27 |
| - message = "Day {} part {} solution : {}".format(args.day, args.part, solution) |
| 47 | + solve_func = get_solver(args.day, args.part) |
| 48 | + puzzle_input = get_input(args.day) |
| 49 | + solution, elapsed = timeit(solve_func, puzzle_input) |
| 50 | + |
| 51 | + message = get_message(args.day, args.part, solution, elapsed) |
28 | 52 | print(message)
|
0 commit comments