almost part 2
This commit is contained in:
parent
dcf50da61c
commit
50d0429fc4
@ -13,14 +13,17 @@ class Cube:
|
|||||||
x: int
|
x: int
|
||||||
y: int
|
y: int
|
||||||
z: int
|
z: int
|
||||||
neighbors: Set[Tuple[int,int,int]] = None
|
neighbor_coords: Set[Tuple[int,int,int]] = None
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def xyz(self):
|
def xyz(self):
|
||||||
return (self.x,self.y,self.z)
|
return (self.x,self.y,self.z)
|
||||||
|
|
||||||
def set_neighbors(self):
|
def set_neighbor_coords(self):
|
||||||
# Generate the six neighbors
|
self.neighbor_coords = get_neighbors(self.x,self.y,self.z)
|
||||||
|
|
||||||
|
def get_neighbors(x,y,z):
|
||||||
|
# Generate the six neighbor_coords
|
||||||
# Look at a die 6 to the left, 2 on top, one on right
|
# Look at a die 6 to the left, 2 on top, one on right
|
||||||
offsets = (
|
offsets = (
|
||||||
(1,0,0), # 1
|
(1,0,0), # 1
|
||||||
@ -30,9 +33,12 @@ class Cube:
|
|||||||
(0,0,-1),# 5
|
(0,0,-1),# 5
|
||||||
(-1,0,0) # 6
|
(-1,0,0) # 6
|
||||||
)
|
)
|
||||||
self.neighbors = set([(self.x+o[0], self.y+o[1], self.z+o[2]) for o in offsets])
|
neighbor_coords = set([(x+o[0], y+o[1], z+o[2]) for o in offsets])
|
||||||
|
return neighbor_coords
|
||||||
|
|
||||||
|
def count_neighbor_coords(x,y,z):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
# @shared.profile
|
||||||
@ -41,32 +47,77 @@ def part1(rows):
|
|||||||
for row in rows:
|
for row in rows:
|
||||||
x,y,z = row
|
x,y,z = row
|
||||||
cube = Cube(x=x,y=y,z=z)
|
cube = Cube(x=x,y=y,z=z)
|
||||||
cube.set_neighbors()
|
cube.set_neighbor_coords()
|
||||||
cubes.append(cube)
|
cubes.append(cube)
|
||||||
|
|
||||||
|
potential = len(cubes) * 6
|
||||||
|
for idx in range(len(cubes)):
|
||||||
|
cube = cubes[idx]
|
||||||
|
for other in cubes[:idx] + cubes[idx+1:]:
|
||||||
|
if other.xyz in cube.neighbor_coords:
|
||||||
|
potential -= 1
|
||||||
|
print(potential)
|
||||||
|
|
||||||
|
|
||||||
|
# @shared.profile
|
||||||
|
def part2(rows):
|
||||||
|
cubes = []
|
||||||
|
maxX,maxY,maxZ = 0,0,0
|
||||||
|
|
||||||
|
_lava = set()
|
||||||
|
for row in rows:
|
||||||
|
x,y,z = row
|
||||||
|
maxX = max(maxX,x)
|
||||||
|
maxY = max(maxY,y)
|
||||||
|
maxZ = max(maxZ,z)
|
||||||
|
cube = Cube(x=x,y=y,z=z)
|
||||||
|
cube.set_neighbor_coords()
|
||||||
|
cubes.append(cube)
|
||||||
|
_lava.add((x,y,z))
|
||||||
|
print(maxX,maxY,maxZ)
|
||||||
|
|
||||||
potential = len(cubes) * 6
|
potential = len(cubes) * 6
|
||||||
for idx in range(len(cubes)):
|
for idx in range(len(cubes)):
|
||||||
cube = cubes[idx]
|
cube = cubes[idx]
|
||||||
others = cubes[:idx] + cubes[idx+1:]
|
others = cubes[:idx] + cubes[idx+1:]
|
||||||
for other in others:
|
for other in others:
|
||||||
if other.xyz in cube.neighbors:
|
if other.xyz in cube.neighbor_coords:
|
||||||
potential -= 1
|
potential -= 1
|
||||||
print(potential)
|
|
||||||
|
|
||||||
|
air = []
|
||||||
|
for x in range(0,maxX+1):
|
||||||
|
for y in range(0,maxY+1):
|
||||||
|
for z in range(0,maxZ+1):
|
||||||
|
lava_count = 0
|
||||||
|
if (x,y,z) in _lava:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# is air
|
||||||
|
_ns = get_neighbors(x,y,z)
|
||||||
|
air_touching_lava = False
|
||||||
|
for _n in _ns:
|
||||||
|
if _n in _lava:
|
||||||
|
lava_count += 1
|
||||||
|
air_touching_lava = True
|
||||||
|
|
||||||
|
if air_touching_lava:
|
||||||
|
air.append((x,y,z))
|
||||||
|
|
||||||
|
if lava_count == 6:
|
||||||
|
potential -= 6
|
||||||
|
|
||||||
|
|
||||||
|
print(potential, air)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# @shared.profile
|
|
||||||
def part2(coords):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
rows = [map(int,row.split(",")) for row in shared.load_rows(18)]
|
rows = [map(int,row.split(",")) for row in shared.load_rows(18)]
|
||||||
with shared.elapsed_timer() as elapsed:
|
with shared.elapsed_timer() as elapsed:
|
||||||
part1(rows)
|
part1(rows)
|
||||||
print("🕒", elapsed())
|
print("🕒", elapsed())
|
||||||
|
rows = [map(int,row.split(",")) for row in shared.load_rows(18)]
|
||||||
with shared.elapsed_timer() as elapsed:
|
with shared.elapsed_timer() as elapsed:
|
||||||
part2(rows)
|
part2(rows)
|
||||||
print("🕒", elapsed())
|
print("🕒", elapsed())
|
||||||
|
Loading…
Reference in New Issue
Block a user