# `aoc-core`
[![License][License Badge]][License]
[![Version][Version Badge]][Package]
[![Downloads][Downloads Badge]][Package]
[![Discord][Discord Badge]][Discord]
[![Documentation][Documentation Badge]][Documentation]
[![Check][Check Badge]][Actions]
[![Test][Test Badge]][Actions]
[![Coverage][Coverage Badge]][Coverage]
> *Advent of Code in Python.*
## Installing
**Python 3.8 or above is required.**
### pip
Installing the library with `pip` is quite simple:
```console
$ pip install aoc-core
```
Alternatively, the library can be installed from source:
```console
$ git clone https://github.com/nekitdev/aoc-core.git
$ cd aoc-core
$ python -m pip install .
```
### poetry
You can add `aoc-core` as a dependency with the following command:
```console
$ poetry add aoc-core
```
Or by directly specifying it in the configuration like so:
```toml
[tool.poetry.dependencies]
aoc-core = "^0.2.0"
```
Alternatively, you can add it directly from the source:
```toml
[tool.poetry.dependencies.aoc-core]
git = "https://github.com/nekitdev/aoc-core.git"
```
## Extras
`aoc-core` provides an extra `ext`, which installs modules like [`iters`][iters], [`funcs`][funcs]
and [`wraps`][wraps] which can help solving problems in functional style.
## Example
This example assumes `aoc` is installed and in `PATH`.
One also needs to install the `ext` extra to use the `iters` module.
We will be solving problem the first ever problem of Advent of Code, that is, [`2015-01`][2015-01].
Firstly, we need to make sure we have the token configured:
```console
$ aoc token print
token not found (path `/home/nekit/.aoc`)
```
Note that the token is placed in `~/.aoc` by default.
Heading over to the [Advent of Code][Advent of Code] website, we need to trace the requests
and find the `session` cookie. This is the token we need to add:
```console
$ aoc token set {TOKEN}
```
And now recheck:
```console
$ aoc token print
{TOKEN}
```
Secondly, we need to download the data for the problem:
```console
$ aoc download
Year: 2015
Day: 01
(... lots of brackets ...)
```
And we are met by our input data! In order to run the solutions, we need to save this.
```console
$ aoc download --year 2015 --day 01 --save
```
Or, if you want to be quicker:
```console
$ aoc download -y 2015 -d 01 -s
```
Now we have everything ready to solve the problem!
In order to define solutions, we need to figure out three types to use:
- `I`, the input type that we parse the data into;
- `T`, the answer type for the first part of the problem;
- `U`, the answer type for the second part of the problem.
Since the problem is about navigating the given string and we return integers,
our types will be: `I = str`, `T = int` and possibly `U = int`.
To define the solution, we need to derive from [`Solution`][aoc.solutions.Solution]:
```python
# example.py
from aoc.solutions import Solution
class Year2015Day01(Solution[str, int, int]):
...
```
Note the class name, `Year2015Day01`. This is the convention for naming solutions,
and all solutions must have their name in the `YearYYYYDayDD` format.
We also need to define three methods:
- `parse`, which takes the data string and returns `I`;
- `solve_one`, which takes `I` and returns `T`;
- `solve_two`, which takes `I` and returns `U`.
Part one is rather simple, we need to count the occurrences of `(` and `)`,
and subtract the latter from the former:
```python
from typing import Final
from aoc.solutions import Solution
UP: Final = "("
DOWN: Final = ")"
class Year2015Day01(Solution[str, int, int]):
def parse(self, data: str) -> str:
return data
def solve_one(self, input: str) -> int:
return input.count(UP) - input.count(DOWN)
def solve_two(self, input: str) -> int:
return 0
```
Since we don't yet know what part two is about, let's return `0` for now.
Time to run!
```console
$ aoc run example.py
result for `2015-01`
answer one: {ONE}
answer two: 0
parse time: 1.018us
solve one time: 41.401us
solve two time: 245.0ns
```
We have our answer for part one, let's submit it!
```console
$ aoc submit --year 2015 --day 01 --part 1 {ONE}
the answer is correct
```
By the way, we can submit the answer directly after running the solution,
using the `--submit (-s)` flag.
Now onto part two! We need to find the first position where the floor is `-1`.
Nothing too difficult, here is the solution for part two included:
```python
from typing import Final
from iters.iters import iter
from aoc.solutions import Solution
UP: Final = "("
DOWN: Final = ")"
NEVER_REACHED_BASEMENT: Final = "the basement was never reached"
class Year2015Day01(Solution[str, int, int]):
def parse(self, data: str) -> str:
return data
def solve_one(self, input: str) -> int:
return input.count(UP) - input.count(DOWN)
def solve_two(self, input: str) -> int:
up = UP
down = DOWN
floor = 0
for position, character in iter(input).enumerate_from(1).unwrap(): # one-based indexing
if character == up:
floor += 1
if character == down:
floor -= 1
if floor < 0:
return position
raise ValueError(NEVER_REACHED_BASEMENT)
```
And let's run the solution again:
```console
$ aoc run example.py
result for `2015-01`
answer one: {ONE}
answer two: {TWO}
parse time: 989.0ns
solve one time: 41.607us
solve two time: 59.411us
```
We have our answer for part two, let's submit it!
```console
$ aoc submit -y 2015 -d 01 -p 2 {TWO}
the answer is correct
```
And there we go, we have solved the first ever problem in the Advent of Code!
## Documentation
You can find the documentation [here][Documentation].
## Support
If you need support with the library, you can send an [email][Email]
or refer to the official [Discord server][Discord].
## Changelog
You can find the changelog [here][Changelog].
## Security Policy
You can find the Security Policy of `aoc-core` [here][Security].
## Contributing
If you are interested in contributing to `aoc-core`, make sure to take a look at the
[Contributing Guide][Contributing Guide], as well as the [Code of Conduct][Code of Conduct].
## License
`aoc-core` is licensed under the MIT License terms. See [License][License] for details.
[Email]: mailto:support@nekit.dev
[Discord]: https://nekit.dev/chat
[Actions]: https://github.com/nekitdev/aoc-core/actions
[Changelog]: https://github.com/nekitdev/aoc-core/blob/main/CHANGELOG.md
[Code of Conduct]: https://github.com/nekitdev/aoc-core/blob/main/CODE_OF_CONDUCT.md
[Contributing Guide]: https://github.com/nekitdev/aoc-core/blob/main/CONTRIBUTING.md
[Security]: https://github.com/nekitdev/aoc-core/blob/main/SECURITY.md
[License]: https://github.com/nekitdev/aoc-core/blob/main/LICENSE
[Package]: https://pypi.org/project/aoc-core
[Coverage]: https://codecov.io/gh/nekitdev/aoc-core
[Documentation]: https://nekitdev.github.io/aoc-core
[Discord Badge]: https://img.shields.io/discord/728012506899021874
[License Badge]: https://img.shields.io/pypi/l/aoc-core
[Version Badge]: https://img.shields.io/pypi/v/aoc-core
[Downloads Badge]: https://img.shields.io/pypi/dm/aoc-core
[Documentation Badge]: https://github.com/nekitdev/aoc-core/workflows/docs/badge.svg
[Check Badge]: https://github.com/nekitdev/aoc-core/workflows/check/badge.svg
[Test Badge]: https://github.com/nekitdev/aoc-core/workflows/test/badge.svg
[Coverage Badge]: https://codecov.io/gh/nekitdev/aoc-core/branch/main/graph/badge.svg
[iters]: https://github.com/nekitdev/iters
[funcs]: https://github.com/nekitdev/funcs
[wraps]: https://github.com/nekitdev/wraps
[Advent of Code]: https://adventofcode.com/
[2015-01]: https://adventofcode.com/2015/day/1
[aoc.solutions.Solution]: https://nekitdev.github.io/aoc-core/reference/solutions#aoc.solutions.Solution
Raw data
{
"_id": null,
"home_page": "https://github.com/nekitdev/aoc-core",
"name": "aoc-core",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "python, aoc",
"author": "nekitdev",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/52/ed/20365f7dab38e26d9fbd85dd08c1404c0b8a817eba64c0ff7d18a8365f68/aoc_core-0.2.0.tar.gz",
"platform": null,
"description": "# `aoc-core`\n\n[![License][License Badge]][License]\n[![Version][Version Badge]][Package]\n[![Downloads][Downloads Badge]][Package]\n[![Discord][Discord Badge]][Discord]\n\n[![Documentation][Documentation Badge]][Documentation]\n[![Check][Check Badge]][Actions]\n[![Test][Test Badge]][Actions]\n[![Coverage][Coverage Badge]][Coverage]\n\n> *Advent of Code in Python.*\n\n## Installing\n\n**Python 3.8 or above is required.**\n\n### pip\n\nInstalling the library with `pip` is quite simple:\n\n```console\n$ pip install aoc-core\n```\n\nAlternatively, the library can be installed from source:\n\n```console\n$ git clone https://github.com/nekitdev/aoc-core.git\n$ cd aoc-core\n$ python -m pip install .\n```\n\n### poetry\n\nYou can add `aoc-core` as a dependency with the following command:\n\n```console\n$ poetry add aoc-core\n```\n\nOr by directly specifying it in the configuration like so:\n\n```toml\n[tool.poetry.dependencies]\naoc-core = \"^0.2.0\"\n```\n\nAlternatively, you can add it directly from the source:\n\n```toml\n[tool.poetry.dependencies.aoc-core]\ngit = \"https://github.com/nekitdev/aoc-core.git\"\n```\n\n## Extras\n\n`aoc-core` provides an extra `ext`, which installs modules like [`iters`][iters], [`funcs`][funcs]\nand [`wraps`][wraps] which can help solving problems in functional style.\n\n## Example\n\nThis example assumes `aoc` is installed and in `PATH`.\n\nOne also needs to install the `ext` extra to use the `iters` module.\n\nWe will be solving problem the first ever problem of Advent of Code, that is, [`2015-01`][2015-01].\n\nFirstly, we need to make sure we have the token configured:\n\n```console\n$ aoc token print\ntoken not found (path `/home/nekit/.aoc`)\n```\n\nNote that the token is placed in `~/.aoc` by default.\n\nHeading over to the [Advent of Code][Advent of Code] website, we need to trace the requests\nand find the `session` cookie. This is the token we need to add:\n\n```console\n$ aoc token set {TOKEN}\n```\n\nAnd now recheck:\n\n```console\n$ aoc token print\n{TOKEN}\n```\n\nSecondly, we need to download the data for the problem:\n\n```console\n$ aoc download\nYear: 2015\nDay: 01\n(... lots of brackets ...)\n```\n\nAnd we are met by our input data! In order to run the solutions, we need to save this.\n\n```console\n$ aoc download --year 2015 --day 01 --save\n```\n\nOr, if you want to be quicker:\n\n```console\n$ aoc download -y 2015 -d 01 -s\n```\n\nNow we have everything ready to solve the problem!\n\nIn order to define solutions, we need to figure out three types to use:\n\n- `I`, the input type that we parse the data into;\n- `T`, the answer type for the first part of the problem;\n- `U`, the answer type for the second part of the problem.\n\nSince the problem is about navigating the given string and we return integers,\nour types will be: `I = str`, `T = int` and possibly `U = int`.\n\nTo define the solution, we need to derive from [`Solution`][aoc.solutions.Solution]:\n\n```python\n# example.py\n\nfrom aoc.solutions import Solution\n\n\nclass Year2015Day01(Solution[str, int, int]):\n ...\n```\n\nNote the class name, `Year2015Day01`. This is the convention for naming solutions,\nand all solutions must have their name in the `YearYYYYDayDD` format.\n\nWe also need to define three methods:\n\n- `parse`, which takes the data string and returns `I`;\n- `solve_one`, which takes `I` and returns `T`;\n- `solve_two`, which takes `I` and returns `U`.\n\nPart one is rather simple, we need to count the occurrences of `(` and `)`,\nand subtract the latter from the former:\n\n```python\nfrom typing import Final\n\nfrom aoc.solutions import Solution\n\nUP: Final = \"(\"\nDOWN: Final = \")\"\n\n\nclass Year2015Day01(Solution[str, int, int]):\n def parse(self, data: str) -> str:\n return data\n\n def solve_one(self, input: str) -> int:\n return input.count(UP) - input.count(DOWN)\n\n def solve_two(self, input: str) -> int:\n return 0\n```\n\nSince we don't yet know what part two is about, let's return `0` for now.\n\nTime to run!\n\n```console\n$ aoc run example.py\nresult for `2015-01`\n answer one: {ONE}\n answer two: 0\n parse time: 1.018us\n solve one time: 41.401us\n solve two time: 245.0ns\n```\n\nWe have our answer for part one, let's submit it!\n\n```console\n$ aoc submit --year 2015 --day 01 --part 1 {ONE}\nthe answer is correct\n```\n\nBy the way, we can submit the answer directly after running the solution,\nusing the `--submit (-s)` flag.\n\nNow onto part two! We need to find the first position where the floor is `-1`.\n\nNothing too difficult, here is the solution for part two included:\n\n```python\nfrom typing import Final\n\nfrom iters.iters import iter\n\nfrom aoc.solutions import Solution\n\nUP: Final = \"(\"\nDOWN: Final = \")\"\n\nNEVER_REACHED_BASEMENT: Final = \"the basement was never reached\"\n\n\nclass Year2015Day01(Solution[str, int, int]):\n def parse(self, data: str) -> str:\n return data\n\n def solve_one(self, input: str) -> int:\n return input.count(UP) - input.count(DOWN)\n\n def solve_two(self, input: str) -> int:\n up = UP\n down = DOWN\n\n floor = 0\n\n for position, character in iter(input).enumerate_from(1).unwrap(): # one-based indexing\n if character == up:\n floor += 1\n\n if character == down:\n floor -= 1\n\n if floor < 0:\n return position\n\n raise ValueError(NEVER_REACHED_BASEMENT)\n```\n\nAnd let's run the solution again:\n\n```console\n$ aoc run example.py\nresult for `2015-01`\n answer one: {ONE}\n answer two: {TWO}\n parse time: 989.0ns\n solve one time: 41.607us\n solve two time: 59.411us\n```\n\nWe have our answer for part two, let's submit it!\n\n```console\n$ aoc submit -y 2015 -d 01 -p 2 {TWO}\nthe answer is correct\n```\n\nAnd there we go, we have solved the first ever problem in the Advent of Code!\n\n## Documentation\n\nYou can find the documentation [here][Documentation].\n\n## Support\n\nIf you need support with the library, you can send an [email][Email]\nor refer to the official [Discord server][Discord].\n\n## Changelog\n\nYou can find the changelog [here][Changelog].\n\n## Security Policy\n\nYou can find the Security Policy of `aoc-core` [here][Security].\n\n## Contributing\n\nIf you are interested in contributing to `aoc-core`, make sure to take a look at the\n[Contributing Guide][Contributing Guide], as well as the [Code of Conduct][Code of Conduct].\n\n## License\n\n`aoc-core` is licensed under the MIT License terms. See [License][License] for details.\n\n[Email]: mailto:support@nekit.dev\n\n[Discord]: https://nekit.dev/chat\n\n[Actions]: https://github.com/nekitdev/aoc-core/actions\n\n[Changelog]: https://github.com/nekitdev/aoc-core/blob/main/CHANGELOG.md\n[Code of Conduct]: https://github.com/nekitdev/aoc-core/blob/main/CODE_OF_CONDUCT.md\n[Contributing Guide]: https://github.com/nekitdev/aoc-core/blob/main/CONTRIBUTING.md\n[Security]: https://github.com/nekitdev/aoc-core/blob/main/SECURITY.md\n\n[License]: https://github.com/nekitdev/aoc-core/blob/main/LICENSE\n\n[Package]: https://pypi.org/project/aoc-core\n[Coverage]: https://codecov.io/gh/nekitdev/aoc-core\n[Documentation]: https://nekitdev.github.io/aoc-core\n\n[Discord Badge]: https://img.shields.io/discord/728012506899021874\n[License Badge]: https://img.shields.io/pypi/l/aoc-core\n[Version Badge]: https://img.shields.io/pypi/v/aoc-core\n[Downloads Badge]: https://img.shields.io/pypi/dm/aoc-core\n\n[Documentation Badge]: https://github.com/nekitdev/aoc-core/workflows/docs/badge.svg\n[Check Badge]: https://github.com/nekitdev/aoc-core/workflows/check/badge.svg\n[Test Badge]: https://github.com/nekitdev/aoc-core/workflows/test/badge.svg\n[Coverage Badge]: https://codecov.io/gh/nekitdev/aoc-core/branch/main/graph/badge.svg\n\n[iters]: https://github.com/nekitdev/iters\n[funcs]: https://github.com/nekitdev/funcs\n[wraps]: https://github.com/nekitdev/wraps\n\n[Advent of Code]: https://adventofcode.com/\n\n[2015-01]: https://adventofcode.com/2015/day/1\n\n[aoc.solutions.Solution]: https://nekitdev.github.io/aoc-core/reference/solutions#aoc.solutions.Solution\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Advent of Code in Python.",
"version": "0.2.0",
"project_urls": {
"Chat": "https://nekit.dev/chat",
"Documentation": "https://nekitdev.github.io/aoc-core",
"Funding": "https://nekit.dev/funding",
"Homepage": "https://github.com/nekitdev/aoc-core",
"Issues": "https://github.com/nekitdev/aoc-core/issues",
"Repository": "https://github.com/nekitdev/aoc-core"
},
"split_keywords": [
"python",
" aoc"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "96e174564e055ea0a4d5b247fe1ff32418b0a07ba81369364c0e7cae208fca19",
"md5": "285a2966ea90a4bc1650834cfde5749f",
"sha256": "44293e1f1588e39d7fa32a44a088fc719bf9261c58485f4d2c1f6afbdcecec90"
},
"downloads": -1,
"filename": "aoc_core-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "285a2966ea90a4bc1650834cfde5749f",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 22074,
"upload_time": "2024-04-23T12:57:52",
"upload_time_iso_8601": "2024-04-23T12:57:52.121877Z",
"url": "https://files.pythonhosted.org/packages/96/e1/74564e055ea0a4d5b247fe1ff32418b0a07ba81369364c0e7cae208fca19/aoc_core-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "52ed20365f7dab38e26d9fbd85dd08c1404c0b8a817eba64c0ff7d18a8365f68",
"md5": "0cf119f1768407e503618472d309e5d3",
"sha256": "381a7926c59de6bc4c02811047e0770640165aba7636554fb4ae0ff691853eab"
},
"downloads": -1,
"filename": "aoc_core-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "0cf119f1768407e503618472d309e5d3",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 19686,
"upload_time": "2024-04-23T12:57:53",
"upload_time_iso_8601": "2024-04-23T12:57:53.372807Z",
"url": "https://files.pythonhosted.org/packages/52/ed/20365f7dab38e26d9fbd85dd08c1404c0b8a817eba64c0ff7d18a8365f68/aoc_core-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-23 12:57:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "nekitdev",
"github_project": "aoc-core",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "aoc-core"
}