Skip to content

Commit

Permalink
Merge pull request #6 from ephemient/py/day1
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Dec 1, 2024
2 parents 093d315 + 5201cb8 commit 57df8d9
Show file tree
Hide file tree
Showing 12 changed files with 536 additions and 3 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/py-bench.yml
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 }}'
59 changes: 59 additions & 0 deletions .github/workflows/py.yml
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

Development occurs in language-specific directories:

|[Haskell](hs) ![Haskell CI](https://github.com/ephemient/aoc2024/workflows/Haskell%20CI/badge.svg)|[Kotlin](kt) ![Kotlin CI](https://github.com/ephemient/aoc2024/workflows/Kotlin%20CI/badge.svg)|
|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|
|[Haskell](hs) ![Haskell CI](https://github.com/ephemient/aoc2024/workflows/Haskell%20CI/badge.svg)|[Kotlin](kt) ![Kotlin CI](https://github.com/ephemient/aoc2024/workflows/Kotlin%20CI/badge.svg)|[Python](py) ![Python CI](https://github.com/ephemient/aoc2024/workflows/Python%20CI/badge.svg)|
|--:|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day1.kt)|[day1.py](py/aoc2024/day1.py)|
3 changes: 3 additions & 0 deletions py/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
dist/
*~
1 change: 1 addition & 0 deletions py/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.13.0
36 changes: 36 additions & 0 deletions py/README.md
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 added py/aoc2024/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions py/aoc2024/day1.py
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)
34 changes: 34 additions & 0 deletions py/aoc2024/main.py
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()
Loading

0 comments on commit 57df8d9

Please sign in to comment.