-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday04.py
More file actions
62 lines (50 loc) · 1.43 KB
/
day04.py
File metadata and controls
62 lines (50 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import cudf
import cupy as cp
import numpy as np
import nvtx
@nvtx.annotate("Parse Input")
def parse() -> tuple[cudf.DataFrame, int]:
with open("inputs/day04.in") as f:
raw = [line.strip() for line in f]
grid = np.array(
[[1 if c == "@" else 0 for c in row] for row in raw], dtype=np.uint8
)
padded = np.pad(grid, pad_width=1, mode="constant", constant_values=0)
_, W = padded.shape
flat = padded.ravel()
df = cudf.DataFrame({"v": cp.asarray(flat), "n": cp.zeros_like(flat)})
return df, W
@nvtx.annotate("Step")
def step(df, W) -> int:
v = df["v"]
n = v.shift(-W - 1, fill_value=0)
n += v.shift(-W + 0, fill_value=0)
n += v.shift(-W + 1, fill_value=0)
n += v.shift(0 - 1, fill_value=0)
n += v.shift(0 + 1, fill_value=0)
n += v.shift(W - 1, fill_value=0)
n += v.shift(W + 0, fill_value=0)
n += v.shift(W + 1, fill_value=0)
mask = (v == 1) & (n < 4)
df["v"] = v - mask
return mask.sum()
@nvtx.annotate("Part 1")
def part1(df: cudf.DataFrame, W: int) -> int:
return step(df, W)
@nvtx.annotate("Part 2")
def part2(df: cudf.DataFrame, W: int, start: int) -> int:
total = start
while True:
r = step(df, W)
if r == 0:
break
total += r
return total
@nvtx.annotate("Day 04")
def main():
df, W = parse()
p1 = part1(df, W)
p2 = part2(df, W, p1)
print(p1, p2)
if __name__ == "__main__":
main()