advent-of-code/2022/python/day18.py

77 lines
1.6 KiB
Python
Raw Normal View History

2022-12-18 05:28:24 +00:00
import matrix
import shared
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from dataclasses import dataclass
from typing import Set, Tuple
from functools import cached_property
@dataclass
class Cube:
x: int
y: int
z: int
neighbors: Set[Tuple[int,int,int]] = None
@cached_property
def xyz(self):
return (self.x,self.y,self.z)
def set_neighbors(self):
# Generate the six neighbors
# Look at a die 6 to the left, 2 on top, one on right
offsets = (
(1,0,0), # 1
(0,0,1), # 2
(0,-1,0),# 3
(0,1,0), # 4
(0,0,-1),# 5
(-1,0,0) # 6
)
self.neighbors = set([(self.x+o[0], self.y+o[1], self.z+o[2]) for o in offsets])
# @shared.profile
def part1(rows):
cubes = []
for row in rows:
x,y,z = row
cube = Cube(x=x,y=y,z=z)
cube.set_neighbors()
cubes.append(cube)
potential = len(cubes) * 6
for idx in range(len(cubes)):
cube = cubes[idx]
others = cubes[:idx] + cubes[idx+1:]
for other in others:
if other.xyz in cube.neighbors:
potential -= 1
print(potential)
# @shared.profile
def part2(coords):
pass
def main():
rows = [map(int,row.split(",")) for row in shared.load_rows(18)]
with shared.elapsed_timer() as elapsed:
part1(rows)
print("🕒", elapsed())
with shared.elapsed_timer() as elapsed:
part2(rows)
print("🕒", elapsed())
if __name__ == "__main__":
main()