77 lines
1.6 KiB
Python
77 lines
1.6 KiB
Python
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()
|