-
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
8 changed files
with
248 additions
and
42 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 |
---|---|---|
|
@@ -34,12 +34,23 @@ jobs: | |
working-directory: py | ||
- run: poetry run pytest --benchmark-skip | ||
working-directory: py | ||
- run: poetry build | ||
- run: poetry build -f sdist | ||
working-directory: py | ||
- uses: docker/setup-qemu-action@v2 | ||
with: | ||
platforms: all | ||
- uses: pypa/[email protected] | ||
with: | ||
package-dir: py | ||
env: | ||
CIBW_ARCHS_LINUX: aarch64 i686 ppc64le s390x x86_64 | ||
CIBW_BUILD: cp313-* | ||
CIBW_BUILD_FRONTEND: build | ||
- run: mv py/wheelhouse/*.whl py/dist/ | ||
- uses: actions/upload-artifact@v4 | ||
with: | ||
name: aoc2024-py | ||
path: py/dist/*.whl | ||
path: py/dist/* | ||
|
||
run: | ||
needs: [ get-inputs, build ] | ||
|
@@ -53,7 +64,7 @@ jobs: | |
- run: | | ||
python -m venv . | ||
. bin/activate | ||
pip install *.whl | ||
pip install *-cp313-manylinux*_$(uname -m).whl | ||
working-directory: aoc2024-py | ||
- run: aoc2024-py/bin/aoc2024 | ||
env: | ||
|
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
__pycache__/ | ||
build/ | ||
dist/ | ||
*.c | ||
*.pyd | ||
*.so | ||
*~ |
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,5 @@ | ||
cdef extern from "<stdatomic.h>": | ||
ctypedef unsigned int atomic_uint | ||
cdef unsigned int atomic_load_uint "atomic_load" (atomic_uint *obj) nogil | ||
cdef unsigned int atomic_fetch_add_uint "atomic_fetch_add" (atomic_uint *obj, unsigned int arg) nogil | ||
cdef unsigned int atomic_compare_exchange_weak_uint "atomic_compare_exchange_weak" (atomic_uint *obj, unsigned int *expected, unsigned int desired) nogil |
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,89 @@ | ||
# cython: boundscheck=False, wraparound=False, initializedcheck=False, embedsignature=True | ||
# ruff: noqa: F821 | ||
""" | ||
Day 22: Monkey Market | ||
""" | ||
|
||
import cython | ||
from cython.parallel import prange | ||
from cython.cimports.libc.string import memset | ||
|
||
|
||
@cython.cfunc | ||
@cython.nogil | ||
def _step(num: cython.uint) -> cython.uint: | ||
num = num ^ num << 6 & 16777215 | ||
num = num ^ num >> 5 & 16777215 | ||
num = num ^ num << 11 & 16777215 | ||
return num | ||
|
||
|
||
@cython.ccall | ||
@cython.nogil | ||
def part1(data: cython.uint[:]) -> cython.ulong: | ||
i: cython.int | ||
result: cython.ulong = 0 | ||
for i in prange(data.shape[0], nogil=True): | ||
j: cython.int | ||
secret: cython.uint = data[i] | ||
for j in range(2000): | ||
secret = _step(secret) | ||
result += secret | ||
return result | ||
|
||
|
||
@cython.ccall | ||
@cython.nogil | ||
def part2(data: cython.uint[:]) -> cython.uint: | ||
i: cython.int | ||
acc: cython.uint[19 * 19 * 19 * 19] | ||
memset(cython.address(acc[0]), 0, cython.sizeof(acc)) | ||
result: cython.uint = 0 | ||
for i in prange(data.shape[0]): | ||
j: cython.int | ||
secret: cython.uint = data[i] | ||
seen: cython.bint[19 * 19 * 19 * 19] | ||
memset(cython.address(seen[0]), 0, cython.sizeof(seen)) | ||
window: cython.uint[4] | ||
window[0] = secret % 10 | ||
best: cython.uint = 0 | ||
cur: cython.uint | ||
for j in range(1, 2001): | ||
secret = _step(secret) | ||
price: cython.uint = secret % 10 | ||
if j >= 4: | ||
p0: cython.uint = window[j % 4] | ||
p1: cython.uint = window[(j + 1) % 4] | ||
p2: cython.uint = window[(j + 2) % 4] | ||
p3: cython.uint = window[(j + 3) % 4] | ||
d1: cython.int = p0 - p1 | ||
d2: cython.int = p1 - p2 | ||
d3: cython.int = p2 - p3 | ||
d4: cython.int = p3 - price | ||
key: cython.uint = ( | ||
19 * (19 * (19 * (d1 + 9) + d2 + 9) + d3 + 9) + d4 + 9 | ||
) | ||
if not seen[key]: | ||
seen[key] = True | ||
cur = ( | ||
atomic_fetch_add_uint( | ||
cython.cast( | ||
cython.pointer(atomic_uint), cython.address(acc[key]) | ||
), | ||
price, | ||
) | ||
+ price | ||
) | ||
if best < cur: | ||
best = cur | ||
window[j % 4] = price | ||
cur = atomic_load_uint( | ||
cython.cast(cython.pointer(atomic_uint), cython.address(result)) | ||
) | ||
while cur < best: | ||
atomic_compare_exchange_weak_uint( | ||
cython.cast(cython.pointer(atomic_uint), cython.address(result)), | ||
cython.address(cur), | ||
best, | ||
) | ||
return result |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
import os | ||
import shutil | ||
from distutils.command.build_ext import build_ext | ||
from distutils.core import Distribution, Extension | ||
|
||
from Cython.Build import cythonize | ||
|
||
|
||
def build(): | ||
ext_modules = cythonize( | ||
module_list=[ | ||
Extension( | ||
name="*", | ||
sources=["aoc2024/*c.py"], | ||
), | ||
], | ||
compiler_directives={"language_level": 3}, | ||
) | ||
|
||
distribution = Distribution({"name": "extended", "ext_modules": ext_modules}) | ||
distribution.package_dir = "extended" | ||
|
||
cmd = build_ext(distribution) | ||
cmd.ensure_finalized() | ||
cmd.run() | ||
|
||
# Copy built extensions back to the project | ||
for output in cmd.get_outputs(): | ||
relative_extension = os.path.relpath(output, cmd.build_lib) | ||
shutil.copyfile(output, relative_extension) | ||
mode = os.stat(relative_extension).st_mode | ||
mode |= (mode & 0o444) >> 2 | ||
os.chmod(relative_extension, mode) | ||
|
||
|
||
if __name__ == "__main__": | ||
build() |
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