-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc2018_23_part2_nogolf_try2.py
61 lines (49 loc) · 1.71 KB
/
aoc2018_23_part2_nogolf_try2.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
54
55
56
57
58
59
60
61
# attempt of a solution using octahedron endpoint refinement
# DOES NOT WORK!
import re
pts = [map(int,re.findall('-?\d+',x)) for x in open("input.txt")]
_cache = {}
def inrange(x,y,z):
try:
return _cache[(x,y,z)]
except KeyError:
d = sum(abs(x-u) + abs(y-v) + abs(z-w) <=r for u,v,w,r in pts)
_cache[(x,y,z)] = d
return d
class Octahedron(object):
def __init__(self, x,y,z, size):
self.x = x
self.y = y
self.z = z
self.size = size
self.score = inrange(x,y,z)
self.dist = abs(x) + abs(y) + abs(z)
def __cmp__(self, other):
return cmp(self.score, other.score) or cmp(other.dist, self.dist)
def __str__(self):
return "%d near <%d, %d, %d, size %d> (dist=%d)" % (self.score, self.x, self.y, self.z, self.size, self.dist)
def subdivide(self):
if self.size < 1:
return [self]
ss = self.size / 2
return [
Octahedron(self.x, self.y, self.z, ss),
Octahedron(self.x + self.size, self.y, self.z, ss),
Octahedron(self.x - self.size, self.y, self.z, ss),
Octahedron(self.x, self.y + self.size, self.z, ss),
Octahedron(self.x, self.y - self.size, self.z, ss),
Octahedron(self.x, self.y, self.z + self.size, ss),
Octahedron(self.x, self.y, self.z - self.size, ss),
]
cells = [Octahedron(*p) for p in pts]
keep_best = 100
print "starting subdivisions with keep_best=%d ..." % keep_best
while any(c.size for c in cells):
cells.sort(reverse=True)
del cells[keep_best:]
print cells[0]
newcells = []
for c in cells:
newcells += c.subdivide()
cells = newcells
print max(cells)