Skip to content

Commit

Permalink
Day 1: Trebuchet?!
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 2, 2023
1 parent b180e53 commit 3519c52
Show file tree
Hide file tree
Showing 11 changed files with 671 additions and 3 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/py.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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 install --no-interaction
working-directory: py
- 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
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/aoc2023/workflows/Haskell%20CI/badge.svg)|[Kotlin](kt) ![Kotlin CI](https://github.com/ephemient/aoc2023/workflows/Kotlin%20CI/badge.svg)|
|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day1.kt)|
|[Haskell](hs) ![Haskell CI](https://github.com/ephemient/aoc2023/workflows/Haskell%20CI/badge.svg)|[Kotlin](kt) ![Kotlin CI](https://github.com/ephemient/aoc2023/workflows/Kotlin%20CI/badge.svg)|[Python](py) ![Python CI](https://github.com/ephemient/aoc2023/workflows/Python%20CI/badge.svg)|
|--:|--:|--:|
|[Day1.hs](hs/src/Day1.hs)|[Day1.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day1.kt)|[day1.py](py/aoc2023/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/
*~
30 changes: 30 additions & 0 deletions py/LICENSE
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.
37 changes: 37 additions & 0 deletions py/README.md
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 added py/aoc2023/__init__.py
Empty file.
65 changes: 65 additions & 0 deletions py/aoc2023/day1.py
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)
32 changes: 32 additions & 0 deletions py/aoc2023/main.py
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()
Loading

0 comments on commit 3519c52

Please sign in to comment.