-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
669 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
name: Python CI | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
paths: [ py/** ] | ||
pull_request: | ||
branches: [ main ] | ||
paths: [ py/** ] | ||
|
||
workflow_dispatch: | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: snok/install-poetry@v1 | ||
- uses: actions/setup-python@v4 | ||
with: | ||
python-version: 3.12 | ||
cache: poetry | ||
- run: poetry run pylint aoc2023 | ||
working-directory: py | ||
- run: poetry run black . --check | ||
working-directory: py | ||
- run: poetry run isort . --check | ||
working-directory: py | ||
- run: poetry run pytest --benchmark-skip | ||
working-directory: py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
__pycache__/ | ||
dist/ | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
Copyright Daniel Lin (c) 2023 | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
|
||
* Neither the name of Daniel Lin nor the names of other | ||
contributors may be used to endorse or promote products derived | ||
from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# [Advent of Code 2023](https://adventofcode.com/2023) | ||
### my answers in [Python](https://www.python.org/) ![Python CI](https://github.com/ephemient/aoc2023/workflows/Python%20CI/badge.svg) | ||
|
||
This project builds with [Poetry](https://python-poetry.org/). | ||
|
||
Setup: | ||
|
||
```sh | ||
curl -sSL https://install.python-poetry.org | python3 - | ||
poetry install | ||
``` | ||
|
||
Run the test suite: | ||
|
||
```sh | ||
poetry run pytest | ||
``` | ||
|
||
Run the benchmarks: | ||
|
||
```sh | ||
poetry run pytest --benchmark-enable | ||
``` | ||
|
||
Print solutions for the inputs provided in local data files: | ||
|
||
```sh | ||
poetry run aoc2023 | ||
``` | ||
|
||
Lint and format code with [Black](https://black.readthedocs.io/), [pylint](https://github.com/PyCQA/pylint), and [isort](https://pycqa.github.io/isort/): | ||
|
||
```sh | ||
poetry run black . | ||
poetry run isort . | ||
poetry run pylint aoc2023 | ||
``` |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
""" | ||
Day 1: Trebuchet?! | ||
""" | ||
|
||
SAMPLE_INPUT_1 = """ | ||
1abc2 | ||
pqr3stu8vwx | ||
a1b2c3d4e5f | ||
treb7uchet | ||
""" | ||
SAMPLE_INPUT_2 = """ | ||
two1nine | ||
eightwothree | ||
abcone2threexyz | ||
xtwone3four | ||
4nineeightseven2 | ||
zoneight234 | ||
7pqrstsixteen | ||
""" | ||
|
||
|
||
def _solve(data, mapping): | ||
acc = 0 | ||
for line in data.splitlines(): | ||
subset = {key: value for key, value in mapping.items() if key in line} | ||
if not subset: | ||
continue | ||
# pylint: disable=cell-var-from-loop | ||
x = min(subset.items(), key=lambda item: line.index(item[0]))[1] | ||
y = max(subset.items(), key=lambda item: line.rindex(item[0]))[1] | ||
acc += 10 * x + y | ||
return acc | ||
|
||
|
||
DIGITS = {str(d): d for d in range(10)} | ||
EXTENDED_DIGITS = DIGITS | { | ||
"one": 1, | ||
"two": 2, | ||
"three": 3, | ||
"four": 4, | ||
"five": 5, | ||
"six": 6, | ||
"seven": 7, | ||
"eight": 8, | ||
"nine": 9, | ||
} | ||
|
||
|
||
def part1(data): | ||
""" | ||
>>> part1(SAMPLE_INPUT_1) | ||
142 | ||
""" | ||
return _solve(data, DIGITS) | ||
|
||
|
||
def part2(data): | ||
""" | ||
>>> part2(SAMPLE_INPUT_2) | ||
281 | ||
""" | ||
return _solve(data, EXTENDED_DIGITS) | ||
|
||
|
||
parts = (part1, part2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
""" | ||
Advent of Code 2023 - my answers in Python | ||
""" | ||
|
||
import os | ||
import sys | ||
from importlib import metadata | ||
from pathlib import Path | ||
|
||
from natsort import natsorted | ||
|
||
|
||
def main(): | ||
# pylint: disable=missing-function-docstring | ||
names = sys.argv[1:] | ||
days = metadata.entry_points().select(group="aoc2023.days") | ||
for entry in natsorted(days, key=lambda entry: entry.name): | ||
day = "".join(c for c in entry.name if c.isdigit()) | ||
if names and entry.name.removeprefix("day") not in names: | ||
continue | ||
print(f"Day {entry.name.removeprefix('day')}") | ||
with Path(os.environ.get("AOC2023_DATADIR") or ".").joinpath( | ||
f"day{day}.txt" | ||
).open(encoding="utf-8") as file: | ||
data = file.read() | ||
for part in entry.load(): | ||
print(part(data)) | ||
print() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.