-
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.
Merge pull request #6 from ephemient/py/day1
- Loading branch information
Showing
12 changed files
with
536 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,44 @@ | ||
name: Python benchmarks | ||
|
||
on: | ||
workflow_dispatch: | ||
|
||
permissions: | ||
contents: write | ||
|
||
jobs: | ||
get-inputs: | ||
uses: ephemient/aoc2024/.github/workflows/get-inputs.yml@main | ||
secrets: | ||
SESSION: ${{ secrets.SESSION }} | ||
|
||
build: | ||
needs: [ get-inputs ] | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/checkout@v4 | ||
with: | ||
ref: gh-docs | ||
path: gh-docs | ||
- uses: actions/download-artifact@v4 | ||
with: | ||
name: inputs | ||
path: inputs | ||
- uses: snok/install-poetry@v1 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: 3.13 | ||
cache: poetry | ||
- run: poetry install --no-interaction | ||
working-directory: py | ||
- run: poetry run pytest --benchmark-enable --benchmark-only --benchmark-histogram=${{ github.workspace }}/gh-docs/benchmark | ||
env: | ||
AOC2024_DATADIR: ${{ github.workspace }}/inputs | ||
working-directory: py | ||
- uses: EndBug/add-and-commit@v9 | ||
with: | ||
cwd: gh-docs | ||
add: benchmark.svg | ||
message: 'pytest-benchmark ${{ github.sha }}' |
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,59 @@ | ||
name: Python CI | ||
|
||
on: | ||
push: | ||
branches: [ main ] | ||
paths: [ py/** ] | ||
pull_request: | ||
branches: [ main ] | ||
paths: [ py/** ] | ||
|
||
workflow_dispatch: | ||
|
||
jobs: | ||
get-inputs: | ||
uses: ephemient/aoc2024/.github/workflows/get-inputs.yml@main | ||
secrets: | ||
SESSION: ${{ secrets.SESSION }} | ||
|
||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: snok/install-poetry@v1 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: 3.13 | ||
cache: poetry | ||
- run: poetry install --no-interaction | ||
working-directory: py | ||
- run: poetry run ruff check | ||
working-directory: py | ||
- run: poetry run pytest --benchmark-skip | ||
working-directory: py | ||
- run: poetry build | ||
working-directory: py | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: aoc2024-py | ||
path: py/dist/*.whl | ||
|
||
run: | ||
needs: [ get-inputs, build ] | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/download-artifact@v4 | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: 3.13 | ||
- run: | | ||
python -m venv . | ||
. bin/activate | ||
pip install *.whl | ||
working-directory: aoc2024-py | ||
- run: aoc2024-py/bin/aoc2024 | ||
env: | ||
AOC2024_DATADIR: inputs | ||
PYTHON_HOME: aoc2024-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 @@ | ||
3.13.0 |
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,36 @@ | ||
# [Advent of Code 2024](https://adventofcode.com/2024) | ||
### my answers in [Python](https://www.python.org/) ![Python CI](https://github.com/ephemient/aoc2024/workflows/Python%20CI/badge.svg) | ||
|
||
This project builds with [Poetry](https://python-poetry.org/). | ||
|
||
Setup: | ||
|
||
```sh | ||
pipx install poetry | ||
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 aoc2024 | ||
``` | ||
|
||
Lint and format code with [Ruff](https://docs.astral.sh/ruff/): | ||
|
||
```sh | ||
poetry run ruff check --fix | ||
poetry run ruff format | ||
``` |
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,48 @@ | ||
""" | ||
Day 1: Historial Hysteria | ||
""" | ||
|
||
from collections import Counter | ||
|
||
SAMPLE_INPUT = """ | ||
3 4 | ||
4 3 | ||
2 5 | ||
1 3 | ||
3 9 | ||
3 3 | ||
""" | ||
|
||
|
||
def _parse(data: str) -> tuple[list[int], list[int]]: | ||
left, right = [], [] | ||
for line in data.splitlines(): | ||
line = line.strip() | ||
if not line: | ||
continue | ||
first, second = line.split() | ||
left.append(int(first)) | ||
right.append(int(second)) | ||
return left, right | ||
|
||
|
||
def part1(data: str) -> int: | ||
""" | ||
>>> part1(SAMPLE_INPUT) | ||
11 | ||
""" | ||
left, right = _parse(data) | ||
return sum(abs(x - y) for (x, y) in zip(sorted(left), sorted(right))) | ||
|
||
|
||
def part2(data: str) -> int: | ||
""" | ||
>>> part2(SAMPLE_INPUT) | ||
31 | ||
""" | ||
left, right = _parse(data) | ||
right = Counter(right) | ||
return sum(x * right[x] for x in left) | ||
|
||
|
||
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,34 @@ | ||
""" | ||
Advent of Code 2024 - 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="aoc2024.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("AOC2024_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.