-
Notifications
You must be signed in to change notification settings - Fork 17
/
coord_compression.py
executable file
·53 lines (42 loc) · 1.55 KB
/
coord_compression.py
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
#!/usr/bin/env python3
#
# Day 22 (part 2 only) using coordinate compression.
# Runtime is pretty ridicolous at around 3m 30s for CPython 3.9.2 on my machine.
# PyPy 7.3.5 on the other hand runs this in a decent time of about 6.5 seconds.
#
import re
import sys
import resource
from bisect import bisect_left
# Limit memory usage to 8GB just to be sure... the `space` list below will be huge.
resource.setrlimit(resource.RLIMIT_AS, (8 * 2**30,) * 2)
# Open the first argument as input or use stdin if no arguments were given
fin = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
commands = []
xs, ys, zs = [], [], []
with fin:
for line in fin:
on = int(line.startswith('on'))
x1, x2, y1, y2, z1, z2 = tuple(map(int, re.findall(r'-?\d+', line)))
commands.append((on, x1, x2 + 1, y1, y2 + 1, z1, z2 + 1))
xs.extend((x1, x2 + 1))
ys.extend((y1, y2 + 1))
zs.extend((z1, z2 + 1))
xs.sort()
ys.sort()
zs.sort()
total = 0
space = [[[0] * len(zs) for _ in range(len(ys))] for _ in range(len(xs))]
for on, x1, x2, y1, y2, z1, z2 in commands:
rank_x1, rank_x2 = bisect_left(xs, x1), bisect_left(xs, x2)
rank_y1, rank_y2 = bisect_left(ys, y1), bisect_left(ys, y2)
rank_z1, rank_z2 = bisect_left(zs, z1), bisect_left(zs, z2)
for i in range(rank_x1, rank_x2):
for j in range(rank_y1, rank_y2):
for k in range(rank_z1, rank_z2):
space[i][j][k] = on
for i in range(len(xs) - 1):
for j in range(len(ys) - 1):
for k in range(len(zs) - 1):
total += space[i][j][k] * (xs[i + 1] - xs[i]) * (ys[j + 1] - ys[j]) * (zs[k + 1] - zs[k])
print('Part 2:', total)