Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Simplify template
Browse files Browse the repository at this point in the history
- 🚚️ 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
  • Loading branch information
jontsai committed Dec 7, 2022
1 parent 2c8749a commit da0b4cf
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 131 deletions.
54 changes: 8 additions & 46 deletions adventofcode/2022/05.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@
from collections import defaultdict
from dataclasses import dataclass

# Third Party (PyPI) Imports
import click

from utils import (
BaseSolution,
InputConfig,
config,
debug,
main,
solution,
)


EXPECTED_ANSWERS = ('LJSVLTWQM', 'BRQWDBBJM')
TEST_CASES = {
config.EXPECTED_ANSWERS = ('LJSVLTWQM', 'BRQWDBBJM')
config.TEST_CASES = {
'': ('CMZ', 'MCD'),
}

Expand All @@ -36,48 +34,12 @@
config.INPUT_CONFIG.cell_func = None


YEAR = int(pathlib.Path.cwd().parts[-1])
DAY = int(pathlib.Path(__file__).stem)
PROBLEM_NUM = str(DAY).zfill(2)


@click.command()
@click.option('--is_real', '--real', is_flag=True, default=False)
@click.option('--submit', is_flag=True, default=False)
@click.option('--is_debug', '--debug', is_flag=True, default=False)
def main(is_real, submit, is_debug):
config.TEST_MODE = not is_real
config.DEBUGGING = is_debug

inputs = []

if config.TEST_MODE:
for test_variant, expected_answers in TEST_CASES.items():

input_filename = f'{PROBLEM_NUM}{test_variant}.test.in'
inputs.append((input_filename, expected_answers))
else:
input_filename = f'{PROBLEM_NUM}.in'
expected_answers = EXPECTED_ANSWERS
inputs.append((input_filename, expected_answers))

for input_filename, expected_answers in inputs:
print(f'Running with input file: {input_filename}')

solution = Solution(
input_filename,
config.INPUT_CONFIG,
expected_answers,
year=YEAR,
day=DAY,
)

solution.solve()
if submit:
solution.submit(is_test=config.TEST_MODE)
solution.report()
config.YEAR = int(pathlib.Path.cwd().parts[-1])
config.DAY = int(pathlib.Path(__file__).stem)
config.PROBLEM_NUM = str(config.DAY).zfill(2)


@solution
class Solution(BaseSolution):
def process_data(self):
data = self.data
Expand Down
51 changes: 8 additions & 43 deletions adventofcode/2022/07.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
InputConfig,
config,
debug,
main,
solution,
)


EXPECTED_ANSWERS = (1367870, 549173)
TEST_CASES = {
config.EXPECTED_ANSWERS = (1367870, 549173)
config.TEST_CASES = {
'': (95437, 24933642),
}

Expand All @@ -33,49 +35,12 @@
config.INPUT_CONFIG.row_func = None
config.INPUT_CONFIG.cell_func = None


YEAR = int(Path.cwd().parts[-1])
DAY = int(Path(__file__).stem)
PROBLEM_NUM = str(DAY).zfill(2)


@click.command()
@click.option('--is_real', '--real', is_flag=True, default=False)
@click.option('--submit', is_flag=True, default=False)
@click.option('--is_debug', '--debug', is_flag=True, default=False)
def main(is_real, submit, is_debug):
config.TEST_MODE = not is_real
config.DEBUGGING = is_debug

inputs = []

if config.TEST_MODE:
for test_variant, expected_answers in TEST_CASES.items():

input_filename = f'{PROBLEM_NUM}{test_variant}.test.in'
inputs.append((input_filename, expected_answers))
else:
input_filename = f'{PROBLEM_NUM}.in'
expected_answers = EXPECTED_ANSWERS
inputs.append((input_filename, expected_answers))

for input_filename, expected_answers in inputs:
print(f'Running with input file: {input_filename}')

solution = Solution(
input_filename,
config.INPUT_CONFIG,
expected_answers,
year=YEAR,
day=DAY,
)

solution.solve()
if submit:
solution.submit(is_test=config.TEST_MODE)
solution.report()
config.YEAR = int(Path.cwd().parts[-1])
config.DAY = int(Path(__file__).stem)
config.PROBLEM_NUM = str(config.DAY).zfill(2)


@solution
class Solution(BaseSolution):
def process_data(self):
data = self.data
Expand Down
50 changes: 8 additions & 42 deletions adventofcode/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
InputConfig,
config,
debug,
main,
solution,
)


EXPECTED_ANSWERS = (None, None)
TEST_CASES = {
config.EXPECTED_ANSWERS = (None, None)
config.TEST_CASES = {
'': (None, None),
# 'b': (None, None),
# 'c': (None, None),
Expand All @@ -39,48 +41,12 @@
config.INPUT_CONFIG.cell_func = None


YEAR = int(Path.cwd().parts[-1])
DAY = int(Path(__file__).stem)
PROBLEM_NUM = str(DAY).zfill(2)


@click.command()
@click.option('--is_real', '--real', is_flag=True, default=False)
@click.option('--submit', is_flag=True, default=False)
@click.option('--is_debug', '--debug', is_flag=True, default=False)
def main(is_real, submit, is_debug):
config.TEST_MODE = not is_real
config.DEBUGGING = is_debug

inputs = []

if config.TEST_MODE:
for test_variant, expected_answers in TEST_CASES.items():

input_filename = f'{PROBLEM_NUM}{test_variant}.test.in'
inputs.append((input_filename, expected_answers))
else:
input_filename = f'{PROBLEM_NUM}.in'
expected_answers = EXPECTED_ANSWERS
inputs.append((input_filename, expected_answers))

for input_filename, expected_answers in inputs:
print(f'Running with input file: {input_filename}')

solution = Solution(
input_filename,
config.INPUT_CONFIG,
expected_answers,
year=YEAR,
day=DAY,
)

solution.solve()
if submit:
solution.submit(is_test=config.TEST_MODE)
solution.report()
config.YEAR = int(Path.cwd().parts[-1])
config.DAY = int(Path(__file__).stem)
config.PROBLEM_NUM = str(config.DAY).zfill(2)


@solution
class Solution(BaseSolution):
def process_data(self):
data = self.data
Expand Down
1 change: 1 addition & 0 deletions adventofcode/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Local Imports
from . import config
from .decorators import *
from .general import *
from .graphs import *
from .iterators import *
Expand Down
12 changes: 12 additions & 0 deletions adventofcode/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
TEST_MODE = True
DEBUGGING = False

EXPECTED_ANSWERS = (None, None)
TEST_CASES = {
'': (None, None),
# 'b': (None, None),
# 'c': (None, None),
}

YEAR = None
DAY = None
PROBLEM_NUM = None


@dataclass
class InputConfig:
Expand All @@ -25,3 +36,4 @@ class InputConfig:


INPUT_CONFIG = InputConfig()
SOLUTION = None
7 changes: 7 additions & 0 deletions adventofcode/utils/decorators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Local Imports
from . import config


def solution(c):
config.SOLUTION = c
return c
40 changes: 40 additions & 0 deletions adventofcode/utils/general.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Python Standard Library Imports
import json

# Third Party (PyPI) Imports
import click

# Local Imports
from . import config
from .aoc_client import AOCClient
Expand All @@ -17,6 +20,43 @@ def debug(*args):
pass


@click.command()
@click.option('--is_real', '--real', is_flag=True, default=False)
@click.option('--submit', is_flag=True, default=False)
@click.option('--is_debug', '--debug', is_flag=True, default=False)
def main(is_real, submit, is_debug):
config.TEST_MODE = not is_real
config.DEBUGGING = is_debug

inputs = []

if config.TEST_MODE:
for test_variant, expected_answers in config.TEST_CASES.items():

input_filename = f'{config.PROBLEM_NUM}{test_variant}.test.in'
inputs.append((input_filename, expected_answers))
else:
input_filename = f'{config.PROBLEM_NUM}.in'
expected_answers = config.EXPECTED_ANSWERS
inputs.append((input_filename, expected_answers))

for input_filename, expected_answers in inputs:
print(f'Running with input file: {input_filename}')

solution = config.SOLUTION(
input_filename,
config.INPUT_CONFIG,
expected_answers,
year=config.YEAR,
day=config.DAY,
)

solution.solve()
if submit:
solution.submit(is_test=config.TEST_MODE)
solution.report()


def copy_to_system_clipboard(x):
try:
import pyperclip
Expand Down

0 comments on commit da0b4cf

Please sign in to comment.