Skip to content

Commit

Permalink
Merge pull request #217 from ephemient/py/day20
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient authored Jan 7, 2025
2 parents a67a47b + cb1682a commit 7d783c5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
22 changes: 12 additions & 10 deletions py/aoc2024/day20.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Day 20: Race Condition
"""

from array import array
from itertools import chain

from aoc2024.day20c import solve as _solve

SAMPLE_INPUT = """
Expand All @@ -23,25 +26,24 @@
"""


def _getpath(data: str) -> list[tuple[tuple[int, int], int]]:
def _getpath(data: str) -> array[int]:
data = data.splitlines()
pos = next(
y, x = next(
(y, x)
for y, line in enumerate(data)
for x, char in enumerate(line)
if char == "S"
)
path = [(pos, 0)]
path = [(y, x, 0)]
last = None
while True:
y, x = pos
if data[y][x] == "E":
return sorted(path)
for pos2 in ((y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)):
y, x = pos2
if data[y][x] != "#" and pos2 != last:
path.append((pos2, len(path)))
last, pos = pos, pos2
return array("i", chain.from_iterable(sorted(path)))
pos = y, x
for y, x in ((y - 1, x), (y, x - 1), (y, x + 1), (y + 1, x)):
if data[y][x] != "#" and (y, x) != last:
path.append((y, x, len(path)))
last = pos
break
else:
return None
Expand Down
22 changes: 9 additions & 13 deletions py/aoc2024/day20c.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
# cython: boundscheck=False, wraparound=False, initializedcheck=False, embedsignature=True
import cython
from collections.abc import Sequence


@cython.ccall
def solve(
path: Sequence[tuple[tuple[int, int], int]], cheats: cython.int, time: cython.int
) -> cython.int:
count: cython.int = len(path)
@cython.nogil
def solve(path: cython.int[:], cheats: cython.int, time: cython.int) -> cython.int:
count: cython.int = len(path) // 3
i: cython.int
result: cython.int = 0
for i in range(count):
y1: cython.int
x1: cython.int
t1: cython.int
(y1, x1), t1 = path[i]
y1: cython.int = path[3 * i]
x1: cython.int = path[3 * i + 1]
t1: cython.int = path[3 * i + 2]
j: cython.int
for j in range(i + 1, count):
y2: cython.int
x2: cython.int
t2: cython.int
(y2, x2), t2 = path[j]
y2: cython.int = path[3 * j]
x2: cython.int = path[3 * j + 1]
t2: cython.int = path[3 * j + 2]
if y2 > y1 + cheats or y2 == y1 + cheats and x2 > x1:
break
distance: cython.int = abs(y2 - y1) + abs(x2 - x1)
Expand Down
6 changes: 4 additions & 2 deletions py/aoc2024/day22c.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def _step(num: cython.uint) -> cython.uint:
@cython.nogil
def part1(data: cython.uint[:]) -> cython.ulong:
i: cython.int
n: cython.int = len(data)
result: cython.ulong = 0
for i in prange(data.shape[0], nogil=True):
for i in prange(n):
j: cython.int
secret: cython.uint = data[i]
for j in range(2000):
Expand All @@ -34,8 +35,9 @@ 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))
n: cython.int = len(data)
result: cython.uint = 0
for i in prange(data.shape[0]):
for i in prange(n):
j: cython.int
secret: cython.uint = data[i]
seen: cython.bint[19 * 19 * 19 * 19]
Expand Down

0 comments on commit 7d783c5

Please sign in to comment.