-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday12.py
More file actions
36 lines (25 loc) · 1007 Bytes
/
day12.py
File metadata and controls
36 lines (25 loc) · 1007 Bytes
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
import cudf
import nvtx
@nvtx.annotate("Parse Input")
def parse() -> tuple[cudf.DataFrame, list[int]]:
with open("inputs/day12.in", "r") as f:
content = f.read()
sections = content.strip().split("\n\n")
shape_blocks = sections[:-1]
region_text = sections[-1]
shapes = [block.count("#") for block in shape_blocks]
lines = cudf.Series(region_text.strip().split("\n"))
parts = lines.str.split(": ", expand=True)
dims = parts[0].str.split("x", expand=True).astype(int)
counts = parts[1].str.split(" ")
regions = cudf.DataFrame({"area": dims[0] * dims[1], "counts": counts.list.astype(int)})
return regions, shapes
@nvtx.annotate("Part 1")
def part1(regions: cudf.DataFrame, shapes: list[int]) -> int:
required = sum(regions["counts"].list.get(i) * weight for i, weight in enumerate(shapes))
return int((required < regions["area"]).sum())
@nvtx.annotate("Day 12")
def main():
print(part1(*parse()))
if __name__ == "__main__":
main()