Skip to content

Commit da0b4cf

Browse files
committed
πŸ§‘β€πŸ’» Simplify template
- 🚚️ extracted `main` function out of template, into `utils/general` - 🚚 extracted `EXPECTED_ANSWERS` and `TEST_CASES` to `utils/config` - 🚚 extracted `YEAR`, `DAY`, `PROBLEM_NUM` to `utils/config` - ✨ adds `@solution` decorator
1 parent 2c8749a commit da0b4cf

File tree

7 files changed

+84
-131
lines changed

7 files changed

+84
-131
lines changed

β€Žadventofcode/2022/05.py

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77
from collections import defaultdict
88
from dataclasses import dataclass
99

10-
# Third Party (PyPI) Imports
11-
import click
12-
1310
from utils import (
1411
BaseSolution,
15-
InputConfig,
1612
config,
1713
debug,
14+
main,
15+
solution,
1816
)
1917

2018

21-
EXPECTED_ANSWERS = ('LJSVLTWQM', 'BRQWDBBJM')
22-
TEST_CASES = {
19+
config.EXPECTED_ANSWERS = ('LJSVLTWQM', 'BRQWDBBJM')
20+
config.TEST_CASES = {
2321
'': ('CMZ', 'MCD'),
2422
}
2523

@@ -36,48 +34,12 @@
3634
config.INPUT_CONFIG.cell_func = None
3735

3836

39-
YEAR = int(pathlib.Path.cwd().parts[-1])
40-
DAY = int(pathlib.Path(__file__).stem)
41-
PROBLEM_NUM = str(DAY).zfill(2)
42-
43-
44-
@click.command()
45-
@click.option('--is_real', '--real', is_flag=True, default=False)
46-
@click.option('--submit', is_flag=True, default=False)
47-
@click.option('--is_debug', '--debug', is_flag=True, default=False)
48-
def main(is_real, submit, is_debug):
49-
config.TEST_MODE = not is_real
50-
config.DEBUGGING = is_debug
51-
52-
inputs = []
53-
54-
if config.TEST_MODE:
55-
for test_variant, expected_answers in TEST_CASES.items():
56-
57-
input_filename = f'{PROBLEM_NUM}{test_variant}.test.in'
58-
inputs.append((input_filename, expected_answers))
59-
else:
60-
input_filename = f'{PROBLEM_NUM}.in'
61-
expected_answers = EXPECTED_ANSWERS
62-
inputs.append((input_filename, expected_answers))
63-
64-
for input_filename, expected_answers in inputs:
65-
print(f'Running with input file: {input_filename}')
66-
67-
solution = Solution(
68-
input_filename,
69-
config.INPUT_CONFIG,
70-
expected_answers,
71-
year=YEAR,
72-
day=DAY,
73-
)
74-
75-
solution.solve()
76-
if submit:
77-
solution.submit(is_test=config.TEST_MODE)
78-
solution.report()
37+
config.YEAR = int(pathlib.Path.cwd().parts[-1])
38+
config.DAY = int(pathlib.Path(__file__).stem)
39+
config.PROBLEM_NUM = str(config.DAY).zfill(2)
7940

8041

42+
@solution
8143
class Solution(BaseSolution):
8244
def process_data(self):
8345
data = self.data

β€Žadventofcode/2022/07.py

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
InputConfig,
1414
config,
1515
debug,
16+
main,
17+
solution,
1618
)
1719

1820

19-
EXPECTED_ANSWERS = (1367870, 549173)
20-
TEST_CASES = {
21+
config.EXPECTED_ANSWERS = (1367870, 549173)
22+
config.TEST_CASES = {
2123
'': (95437, 24933642),
2224
}
2325

@@ -33,49 +35,12 @@
3335
config.INPUT_CONFIG.row_func = None
3436
config.INPUT_CONFIG.cell_func = None
3537

36-
37-
YEAR = int(Path.cwd().parts[-1])
38-
DAY = int(Path(__file__).stem)
39-
PROBLEM_NUM = str(DAY).zfill(2)
40-
41-
42-
@click.command()
43-
@click.option('--is_real', '--real', is_flag=True, default=False)
44-
@click.option('--submit', is_flag=True, default=False)
45-
@click.option('--is_debug', '--debug', is_flag=True, default=False)
46-
def main(is_real, submit, is_debug):
47-
config.TEST_MODE = not is_real
48-
config.DEBUGGING = is_debug
49-
50-
inputs = []
51-
52-
if config.TEST_MODE:
53-
for test_variant, expected_answers in TEST_CASES.items():
54-
55-
input_filename = f'{PROBLEM_NUM}{test_variant}.test.in'
56-
inputs.append((input_filename, expected_answers))
57-
else:
58-
input_filename = f'{PROBLEM_NUM}.in'
59-
expected_answers = EXPECTED_ANSWERS
60-
inputs.append((input_filename, expected_answers))
61-
62-
for input_filename, expected_answers in inputs:
63-
print(f'Running with input file: {input_filename}')
64-
65-
solution = Solution(
66-
input_filename,
67-
config.INPUT_CONFIG,
68-
expected_answers,
69-
year=YEAR,
70-
day=DAY,
71-
)
72-
73-
solution.solve()
74-
if submit:
75-
solution.submit(is_test=config.TEST_MODE)
76-
solution.report()
38+
config.YEAR = int(Path.cwd().parts[-1])
39+
config.DAY = int(Path(__file__).stem)
40+
config.PROBLEM_NUM = str(config.DAY).zfill(2)
7741

7842

43+
@solution
7944
class Solution(BaseSolution):
8045
def process_data(self):
8146
data = self.data

β€Žadventofcode/template.py

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
InputConfig,
1717
config,
1818
debug,
19+
main,
20+
solution,
1921
)
2022

2123

22-
EXPECTED_ANSWERS = (None, None)
23-
TEST_CASES = {
24+
config.EXPECTED_ANSWERS = (None, None)
25+
config.TEST_CASES = {
2426
'': (None, None),
2527
# 'b': (None, None),
2628
# 'c': (None, None),
@@ -39,48 +41,12 @@
3941
config.INPUT_CONFIG.cell_func = None
4042

4143

42-
YEAR = int(Path.cwd().parts[-1])
43-
DAY = int(Path(__file__).stem)
44-
PROBLEM_NUM = str(DAY).zfill(2)
45-
46-
47-
@click.command()
48-
@click.option('--is_real', '--real', is_flag=True, default=False)
49-
@click.option('--submit', is_flag=True, default=False)
50-
@click.option('--is_debug', '--debug', is_flag=True, default=False)
51-
def main(is_real, submit, is_debug):
52-
config.TEST_MODE = not is_real
53-
config.DEBUGGING = is_debug
54-
55-
inputs = []
56-
57-
if config.TEST_MODE:
58-
for test_variant, expected_answers in TEST_CASES.items():
59-
60-
input_filename = f'{PROBLEM_NUM}{test_variant}.test.in'
61-
inputs.append((input_filename, expected_answers))
62-
else:
63-
input_filename = f'{PROBLEM_NUM}.in'
64-
expected_answers = EXPECTED_ANSWERS
65-
inputs.append((input_filename, expected_answers))
66-
67-
for input_filename, expected_answers in inputs:
68-
print(f'Running with input file: {input_filename}')
69-
70-
solution = Solution(
71-
input_filename,
72-
config.INPUT_CONFIG,
73-
expected_answers,
74-
year=YEAR,
75-
day=DAY,
76-
)
77-
78-
solution.solve()
79-
if submit:
80-
solution.submit(is_test=config.TEST_MODE)
81-
solution.report()
44+
config.YEAR = int(Path.cwd().parts[-1])
45+
config.DAY = int(Path(__file__).stem)
46+
config.PROBLEM_NUM = str(config.DAY).zfill(2)
8247

8348

49+
@solution
8450
class Solution(BaseSolution):
8551
def process_data(self):
8652
data = self.data

β€Žadventofcode/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Local Imports
22
from . import config
3+
from .decorators import *
34
from .general import *
45
from .graphs import *
56
from .iterators import *

β€Žadventofcode/utils/config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66
TEST_MODE = True
77
DEBUGGING = False
88

9+
EXPECTED_ANSWERS = (None, None)
10+
TEST_CASES = {
11+
'': (None, None),
12+
# 'b': (None, None),
13+
# 'c': (None, None),
14+
}
15+
16+
YEAR = None
17+
DAY = None
18+
PROBLEM_NUM = None
19+
920

1021
@dataclass
1122
class InputConfig:
@@ -25,3 +36,4 @@ class InputConfig:
2536

2637

2738
INPUT_CONFIG = InputConfig()
39+
SOLUTION = None

β€Žadventofcode/utils/decorators.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Local Imports
2+
from . import config
3+
4+
5+
def solution(c):
6+
config.SOLUTION = c
7+
return c

β€Žadventofcode/utils/general.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Python Standard Library Imports
22
import json
33

4+
# Third Party (PyPI) Imports
5+
import click
6+
47
# Local Imports
58
from . import config
69
from .aoc_client import AOCClient
@@ -17,6 +20,43 @@ def debug(*args):
1720
pass
1821

1922

23+
@click.command()
24+
@click.option('--is_real', '--real', is_flag=True, default=False)
25+
@click.option('--submit', is_flag=True, default=False)
26+
@click.option('--is_debug', '--debug', is_flag=True, default=False)
27+
def main(is_real, submit, is_debug):
28+
config.TEST_MODE = not is_real
29+
config.DEBUGGING = is_debug
30+
31+
inputs = []
32+
33+
if config.TEST_MODE:
34+
for test_variant, expected_answers in config.TEST_CASES.items():
35+
36+
input_filename = f'{config.PROBLEM_NUM}{test_variant}.test.in'
37+
inputs.append((input_filename, expected_answers))
38+
else:
39+
input_filename = f'{config.PROBLEM_NUM}.in'
40+
expected_answers = config.EXPECTED_ANSWERS
41+
inputs.append((input_filename, expected_answers))
42+
43+
for input_filename, expected_answers in inputs:
44+
print(f'Running with input file: {input_filename}')
45+
46+
solution = config.SOLUTION(
47+
input_filename,
48+
config.INPUT_CONFIG,
49+
expected_answers,
50+
year=config.YEAR,
51+
day=config.DAY,
52+
)
53+
54+
solution.solve()
55+
if submit:
56+
solution.submit(is_test=config.TEST_MODE)
57+
solution.report()
58+
59+
2060
def copy_to_system_clipboard(x):
2161
try:
2262
import pyperclip

0 commit comments

Comments
Β (0)