Skip to content

Commit

Permalink
day22: coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-ong committed Dec 28, 2023
1 parent 3f87c89 commit 1a1979c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
40 changes: 24 additions & 16 deletions day22/lib/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ class BoxData:
init=False, repr=False, hash=False, default=None
)
supports: set["BoxData"] = field(
default_factory=set, hash=False, repr=False
default_factory=set, hash=False, repr=False, init=False
) # list of blocks we support
hats: set["BoxData"] = field(default_factory=set, hash=False, repr=False)
total_hats: set["BoxData"] = field(default_factory=set, hash=False, repr=False)
hats: set["BoxData"] = field(
default_factory=set, hash=False, repr=False, init=False
)
total_hats: set["BoxData"] = field(
default_factory=set, hash=False, repr=False, init=False
)

@property
def vpos(self) -> vpython.vector:
Expand All @@ -44,9 +48,6 @@ def width(self) -> float:
def height(self) -> float:
return float(self.end_pos.z - self.start_pos.z + 1)

def set_vbox(self, vbox: vpython.box) -> None:
self.vbox = vbox

@property
def z_val_bot(self) -> int:
"""return lowest z value (self.start_pos.z)"""
Expand All @@ -57,33 +58,40 @@ def z_val_top(self) -> int:
"""return maximum z value(self.end_pos.z)"""
return self.end_pos.z

####################################
# Visualisation calls (not ci'ed) #
####################################
def set_vbox(self, vbox: vpython.box) -> None: # pragma: no cover
self.vbox = vbox

def fall(self) -> None:
self.start_pos.z -= 1
self.end_pos.z -= 1
# vbox y == boxdata z
if self.vbox is not None:
if self.vbox is not None: # pragma: no cover
self.vbox.pos.y -= 1

def select(self) -> None:
if self.vbox is not None:
if self.vbox is not None: # pragma: no cover
self.vbox.pos.x += 30
self.vbox.pos.z -= 30

def unselect(self) -> None:
if self.vbox is not None:
if self.vbox is not None: # pragma: no cover
self.vbox.pos.x -= 30
self.vbox.pos.z += 30

################################################
# Calculations; supports, hats, recursive fall #
################################################

def set_supports(self, supports: set["BoxData"]) -> None:
"""blocks under us"""
self.supports = supports

def set_hats(self, hats: set["BoxData"]) -> None:
self.hats = hats

def __hash__(self) -> int:
return hash(f"{self.start_pos} | {self.end_pos}")

def recursive_fall(self, already_falling: set["BoxData"]) -> set["BoxData"]:
"""returns all boxes above us that fall if we fall"""
to_process: list[BoxData] = [] # items that will fall if this brick is removed
Expand All @@ -103,6 +111,9 @@ def recursive_fall(self, already_falling: set["BoxData"]) -> set["BoxData"]:

return result

def __hash__(self) -> int:
return hash(f"{self.start_pos} | {self.end_pos}")


class Matrix:
# z, x, y
Expand Down Expand Up @@ -131,10 +142,7 @@ def register_box(self, box: BoxData) -> None:
for x in range(box.start_pos.x, box.end_pos.x + 1):
for y in range(box.start_pos.y, box.end_pos.y + 1):
if self.layers[z][x][y] is not None:
print("collision at:", (x, y, z))
print(f"collision with: {self.layers[z][x][y]}")
print(f"trying to insert: {box}")
raise ValueError(":o verlap")
raise AssertionError("Overlap should not occur")

self.layers[z][x][y] = box

Expand Down
17 changes: 17 additions & 0 deletions day22/tests/test_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import TYPE_CHECKING

from day22.lib.classes import BoxData, Vector3

if TYPE_CHECKING:
import vpython


def test_box_data() -> None:
start: Vector3 = Vector3(1, 1, 1)
end: Vector3 = Vector3(1, 4, 1)
box: BoxData = BoxData("memes", start, end)
vec: vpython.vector = box.vpos
# our source z is vpython's y
assert vec.x == 1.5
assert vec.z == 3.0
assert vec.y == 1.5

0 comments on commit 1a1979c

Please sign in to comment.