diff options
| author | 2022-11-28 13:52:27 +0100 | |
|---|---|---|
| committer | 2022-11-28 13:52:27 +0100 | |
| commit | a3de698aa6b7e15e9d0974d32dc566676383bd28 (patch) | |
| tree | d8d372b8adf7c7d2ab9b8d8b7d434945a88e7e9b | |
| download | 2022-a3de698aa6b7e15e9d0974d32dc566676383bd28.tar.gz 2022-a3de698aa6b7e15e9d0974d32dc566676383bd28.tar.bz2 2022-a3de698aa6b7e15e9d0974d32dc566676383bd28.zip | |
Initial code
| -rw-r--r-- | .gitignore | 234 | ||||
| -rw-r--r-- | aoc/__init__.py | 27 | ||||
| -rw-r--r-- | aoc/__main__.py | 47 | ||||
| -rw-r--r-- | aoc/test_init.py | 2 | ||||
| -rw-r--r-- | aoc/utils.py | 5 | ||||
| -rw-r--r-- | conftest.py | 32 | ||||
| -rw-r--r-- | requirements.txt | 2 |
7 files changed, 349 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f402d03 --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,234 @@ | |||
| 1 | |||
| 2 | # Created by https://www.toptal.com/developers/gitignore/api/python,pycharm+all | ||
| 3 | # Edit at https://www.toptal.com/developers/gitignore?templates=python,pycharm+all | ||
| 4 | |||
| 5 | ### PyCharm+all ### | ||
| 6 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider | ||
| 7 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | ||
| 8 | |||
| 9 | # User-specific stuff | ||
| 10 | .idea/**/workspace.xml | ||
| 11 | .idea/**/tasks.xml | ||
| 12 | .idea/**/usage.statistics.xml | ||
| 13 | .idea/**/dictionaries | ||
| 14 | .idea/**/shelf | ||
| 15 | |||
| 16 | # Generated files | ||
| 17 | .idea/**/contentModel.xml | ||
| 18 | |||
| 19 | # Sensitive or high-churn files | ||
| 20 | .idea/**/dataSources/ | ||
| 21 | .idea/**/dataSources.ids | ||
| 22 | .idea/**/dataSources.local.xml | ||
| 23 | .idea/**/sqlDataSources.xml | ||
| 24 | .idea/**/dynamic.xml | ||
| 25 | .idea/**/uiDesigner.xml | ||
| 26 | .idea/**/dbnavigator.xml | ||
| 27 | |||
| 28 | # Gradle | ||
| 29 | .idea/**/gradle.xml | ||
| 30 | .idea/**/libraries | ||
| 31 | |||
| 32 | # Gradle and Maven with auto-import | ||
| 33 | # When using Gradle or Maven with auto-import, you should exclude module files, | ||
| 34 | # since they will be recreated, and may cause churn. Uncomment if using | ||
| 35 | # auto-import. | ||
| 36 | # .idea/artifacts | ||
| 37 | # .idea/compiler.xml | ||
| 38 | # .idea/jarRepositories.xml | ||
| 39 | # .idea/modules.xml | ||
| 40 | # .idea/*.iml | ||
| 41 | # .idea/modules | ||
| 42 | # *.iml | ||
| 43 | # *.ipr | ||
| 44 | |||
| 45 | # CMake | ||
| 46 | cmake-build-*/ | ||
| 47 | |||
| 48 | # Mongo Explorer plugin | ||
| 49 | .idea/**/mongoSettings.xml | ||
| 50 | |||
| 51 | # File-based project format | ||
| 52 | *.iws | ||
| 53 | |||
| 54 | # IntelliJ | ||
| 55 | out/ | ||
| 56 | |||
| 57 | # mpeltonen/sbt-idea plugin | ||
| 58 | .idea_modules/ | ||
| 59 | |||
| 60 | # JIRA plugin | ||
| 61 | atlassian-ide-plugin.xml | ||
| 62 | |||
| 63 | # Cursive Clojure plugin | ||
| 64 | .idea/replstate.xml | ||
| 65 | |||
| 66 | # Crashlytics plugin (for Android Studio and IntelliJ) | ||
| 67 | com_crashlytics_export_strings.xml | ||
| 68 | crashlytics.properties | ||
| 69 | crashlytics-build.properties | ||
| 70 | fabric.properties | ||
| 71 | |||
| 72 | # Editor-based Rest Client | ||
| 73 | .idea/httpRequests | ||
| 74 | |||
| 75 | # Android studio 3.1+ serialized cache file | ||
| 76 | .idea/caches/build_file_checksums.ser | ||
| 77 | |||
| 78 | ### PyCharm+all Patch ### | ||
| 79 | # Ignores the whole .idea folder and all .iml files | ||
| 80 | # See https://github.com/joeblau/gitignore.io/issues/186 and https://github.com/joeblau/gitignore.io/issues/360 | ||
| 81 | |||
| 82 | .idea/ | ||
| 83 | |||
| 84 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023 | ||
| 85 | |||
| 86 | *.iml | ||
| 87 | modules.xml | ||
| 88 | .idea/misc.xml | ||
| 89 | *.ipr | ||
| 90 | |||
| 91 | # Sonarlint plugin | ||
| 92 | .idea/sonarlint | ||
| 93 | |||
| 94 | ### Python ### | ||
| 95 | # Byte-compiled / optimized / DLL files | ||
| 96 | __pycache__/ | ||
| 97 | *.py[cod] | ||
| 98 | *$py.class | ||
| 99 | |||
| 100 | # C extensions | ||
| 101 | *.so | ||
| 102 | |||
| 103 | # Distribution / packaging | ||
| 104 | .Python | ||
| 105 | build/ | ||
| 106 | develop-eggs/ | ||
| 107 | dist/ | ||
| 108 | downloads/ | ||
| 109 | eggs/ | ||
| 110 | .eggs/ | ||
| 111 | lib/ | ||
| 112 | lib64/ | ||
| 113 | parts/ | ||
| 114 | sdist/ | ||
| 115 | var/ | ||
| 116 | wheels/ | ||
| 117 | pip-wheel-metadata/ | ||
| 118 | share/python-wheels/ | ||
| 119 | *.egg-info/ | ||
| 120 | .installed.cfg | ||
| 121 | *.egg | ||
| 122 | MANIFEST | ||
| 123 | |||
| 124 | # PyInstaller | ||
| 125 | # Usually these files are written by a python script from a template | ||
| 126 | # before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
| 127 | *.manifest | ||
| 128 | *.spec | ||
| 129 | |||
| 130 | # Installer logs | ||
| 131 | pip-log.txt | ||
| 132 | pip-delete-this-directory.txt | ||
| 133 | |||
| 134 | # Unit test / coverage reports | ||
| 135 | htmlcov/ | ||
| 136 | .tox/ | ||
| 137 | .nox/ | ||
| 138 | .coverage | ||
| 139 | .coverage.* | ||
| 140 | .cache | ||
| 141 | nosetests.xml | ||
| 142 | coverage.xml | ||
| 143 | *.cover | ||
| 144 | *.py,cover | ||
| 145 | .hypothesis/ | ||
| 146 | .pytest_cache/ | ||
| 147 | pytestdebug.log | ||
| 148 | |||
| 149 | # Translations | ||
| 150 | *.mo | ||
| 151 | *.pot | ||
| 152 | |||
| 153 | # Django stuff: | ||
| 154 | *.log | ||
| 155 | local_settings.py | ||
| 156 | db.sqlite3 | ||
| 157 | db.sqlite3-journal | ||
| 158 | |||
| 159 | # Flask stuff: | ||
| 160 | instance/ | ||
| 161 | .webassets-cache | ||
| 162 | |||
| 163 | # Scrapy stuff: | ||
| 164 | .scrapy | ||
| 165 | |||
| 166 | # Sphinx documentation | ||
| 167 | docs/_build/ | ||
| 168 | doc/_build/ | ||
| 169 | |||
| 170 | # PyBuilder | ||
| 171 | target/ | ||
| 172 | |||
| 173 | # Jupyter Notebook | ||
| 174 | .ipynb_checkpoints | ||
| 175 | |||
| 176 | # IPython | ||
| 177 | profile_default/ | ||
| 178 | ipython_config.py | ||
| 179 | |||
| 180 | # pyenv | ||
| 181 | .python-version | ||
| 182 | |||
| 183 | # pipenv | ||
| 184 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. | ||
| 185 | # However, in case of collaboration, if having platform-specific dependencies or dependencies | ||
| 186 | # having no cross-platform support, pipenv may install dependencies that don't work, or not | ||
| 187 | # install all needed dependencies. | ||
| 188 | #Pipfile.lock | ||
| 189 | |||
| 190 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow | ||
| 191 | __pypackages__/ | ||
| 192 | |||
| 193 | # Celery stuff | ||
| 194 | celerybeat-schedule | ||
| 195 | celerybeat.pid | ||
| 196 | |||
| 197 | # SageMath parsed files | ||
| 198 | *.sage.py | ||
| 199 | |||
| 200 | # Environments | ||
| 201 | .env | ||
| 202 | .venv | ||
| 203 | env/ | ||
| 204 | venv/ | ||
| 205 | ENV/ | ||
| 206 | env.bak/ | ||
| 207 | venv.bak/ | ||
| 208 | pythonenv* | ||
| 209 | |||
| 210 | # Spyder project settings | ||
| 211 | .spyderproject | ||
| 212 | .spyproject | ||
| 213 | |||
| 214 | # Rope project settings | ||
| 215 | .ropeproject | ||
| 216 | |||
| 217 | # mkdocs documentation | ||
| 218 | /site | ||
| 219 | |||
| 220 | # mypy | ||
| 221 | .mypy_cache/ | ||
| 222 | .dmypy.json | ||
| 223 | dmypy.json | ||
| 224 | |||
| 225 | # Pyre type checker | ||
| 226 | .pyre/ | ||
| 227 | |||
| 228 | # pytype static type analyzer | ||
| 229 | .pytype/ | ||
| 230 | |||
| 231 | # profiling data | ||
| 232 | .prof | ||
| 233 | |||
| 234 | # End of https://www.toptal.com/developers/gitignore/api/python,pycharm+all | ||
diff --git a/aoc/__init__.py b/aoc/__init__.py new file mode 100644 index 0000000..b13489c --- /dev/null +++ b/aoc/__init__.py | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | import os | ||
| 2 | from abc import ABC | ||
| 3 | from typing import Generator, Any, Iterator | ||
| 4 | |||
| 5 | |||
| 6 | class BaseAssignment(ABC): | ||
| 7 | example_result = NotImplemented | ||
| 8 | def __init__(self, path): | ||
| 9 | self.path = path | ||
| 10 | |||
| 11 | def __str__(self): | ||
| 12 | return f'{self.__module__}.{self.__class__.__name__}' | ||
| 13 | |||
| 14 | def parse_item(self, item: str) -> Any: | ||
| 15 | return item | ||
| 16 | |||
| 17 | def read_input(self, example = False) -> Generator: | ||
| 18 | file = f'{self.path}/input.txt' | ||
| 19 | if example or not os.path.isfile(file): | ||
| 20 | file = f'{self.path}/example.txt' | ||
| 21 | |||
| 22 | with open(file, 'r') as input_file: | ||
| 23 | for line in input_file.readlines(): | ||
| 24 | yield self.parse_item(line.strip()) | ||
| 25 | |||
| 26 | def run(self, input: Iterator) -> Any: | ||
| 27 | raise NotImplementedError('Please implement run') \ No newline at end of file | ||
diff --git a/aoc/__main__.py b/aoc/__main__.py new file mode 100644 index 0000000..25a7026 --- /dev/null +++ b/aoc/__main__.py | |||
| @@ -0,0 +1,47 @@ | |||
| 1 | import argparse | ||
| 2 | import importlib | ||
| 3 | import os | ||
| 4 | from time import perf_counter | ||
| 5 | from typing import List | ||
| 6 | |||
| 7 | |||
| 8 | def day(assignment_part: str) -> str: | ||
| 9 | return { | ||
| 10 | '1': 'One', | ||
| 11 | '2': 'Two', | ||
| 12 | }[assignment_part] | ||
| 13 | |||
| 14 | def kwargs(kwarg: str) -> List: | ||
| 15 | return kwarg.split('=') | ||
| 16 | |||
| 17 | parser = argparse.ArgumentParser(description='Advent of Code') | ||
| 18 | |||
| 19 | parser.add_argument('day', type=str, help='Assignment day') | ||
| 20 | parser.add_argument( | ||
| 21 | '-p', '--part', type=day, nargs='?', default='1', | ||
| 22 | help='Assingment part. Defaults to one.' | ||
| 23 | ) | ||
| 24 | parser.add_argument('--example', default=False, action='store_true') | ||
| 25 | parser.add_argument('kwargs', type=kwargs, nargs='*') | ||
| 26 | |||
| 27 | |||
| 28 | if __name__ == '__main__': | ||
| 29 | args = parser.parse_args() | ||
| 30 | assignment_day = importlib.import_module(args.day) | ||
| 31 | |||
| 32 | Assignment = getattr(assignment_day, f'Assignment{args.part}') | ||
| 33 | assignment = Assignment( | ||
| 34 | path=os.path.dirname(assignment_day.__file__), | ||
| 35 | **dict(args.kwargs) | ||
| 36 | ) | ||
| 37 | |||
| 38 | start = perf_counter() | ||
| 39 | print( | ||
| 40 | assignment.run( | ||
| 41 | input=assignment.read_input(example=args.example), | ||
| 42 | ) | ||
| 43 | ) | ||
| 44 | end = perf_counter() | ||
| 45 | delta = end - start | ||
| 46 | print() | ||
| 47 | print(f'{(delta if delta > 1 else delta * 1000):.3f}{"s" if delta > 1 else "ms"}') | ||
diff --git a/aoc/test_init.py b/aoc/test_init.py new file mode 100644 index 0000000..8524086 --- /dev/null +++ b/aoc/test_init.py | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | def test_assingment_examples(assignment): | ||
| 2 | assert assignment.run(input=assignment.read_input(example=True)) == assignment.example_result | ||
diff --git a/aoc/utils.py b/aoc/utils.py new file mode 100644 index 0000000..6d794f4 --- /dev/null +++ b/aoc/utils.py | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | from typing import Callable | ||
| 2 | |||
| 3 | |||
| 4 | def bold(item: str, condition: Callable[[], bool]): | ||
| 5 | return f'\033[36m{item}\033[0m' if condition() else item | ||
diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..24884e2 --- /dev/null +++ b/conftest.py | |||
| @@ -0,0 +1,32 @@ | |||
| 1 | import importlib | ||
| 2 | import os | ||
| 3 | from pkgutil import walk_packages | ||
| 4 | |||
| 5 | from _pytest.python import Metafunc | ||
| 6 | |||
| 7 | from aoc.__main__ import day | ||
| 8 | |||
| 9 | dir_path = os.path.dirname(os.path.realpath(__file__)) | ||
| 10 | |||
| 11 | def pytest_generate_tests(metafunc: Metafunc): | ||
| 12 | if 'assignment' in metafunc.fixturenames: | ||
| 13 | packages = [ | ||
| 14 | importlib.import_module(package.name) | ||
| 15 | for package in walk_packages([dir_path]) | ||
| 16 | ] | ||
| 17 | |||
| 18 | assignments = [ | ||
| 19 | (getattr(package, f'Assignment{day(part)}', None), package) | ||
| 20 | for package in packages | ||
| 21 | for part in ['1', '2'] | ||
| 22 | ] | ||
| 23 | |||
| 24 | metafunc.parametrize( | ||
| 25 | argnames=f'assignment', | ||
| 26 | argvalues=[ | ||
| 27 | Assignment(path=package.__path__[0]) | ||
| 28 | for (Assignment, package) in assignments | ||
| 29 | if Assignment is not None and hasattr(package, '__path__') and Assignment.example_result != NotImplemented | ||
| 30 | ], | ||
| 31 | ids=lambda assignment: str(assignment) | ||
| 32 | ) | ||
diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..49ec960 --- /dev/null +++ b/requirements.txt | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | pytest | ||
| 2 | coverage | ||
