day17 part1

This commit is contained in:
Tyrel Souza 2022-12-17 23:09:54 -05:00
parent 3e822d7f55
commit 9611bfedf4

View File

@ -32,9 +32,10 @@ SQUARE = (
(0,0),(0,1),
)
ORDER = (('-',FLAT), ('+',CROSS), ('j',J), ('I',I), ('',SQUARE))
ORDER = (('-',FLAT), ('+',CROSS), ('j',J), ('I',I), ('o',SQUARE))
OPS = {'>':operator.add, '<':operator.sub}
OFF = {'>': (0,1), '<':(0,-1)}
@dataclass
@ -49,7 +50,11 @@ class Shape:
def char(self):
if self.moving:
return '@'
return '#'
return self.at_rest_char
@property
def at_rest_char(self):
return ORDER[self.rock%5][0]
@property
def str(self):
@ -86,27 +91,30 @@ def all_coords(shapes):
coords.add(c)
return coords
def collision_at(shapes, row, col, shape):
if shape is None:
raise Exception("Please provide a list of coordinate offsets from Y,X to draw")
all_coords = set()
def collision_at(shapes, offset):
if len(shapes) == 1:
# Nothing to compare
return False
shape = shapes[-1]
# Take in all existing coordinates
for y,x in shape.coords:
if (y+row,x+col) in all_coords(shapes[:-1]):
breakpoint()
if (y+offset[0],x+offset[1]) in all_coords:
return True
return False
def out_of_bounds(row, col, height, width, shape=None):
def out_of_bounds(height, width, shapes, offset):
shape = shapes[-1]
if shape is None:
raise Exception("Please provide a list of coordinate offsets from Y,X to draw")
for y,x in shape:
for y,x in shape.coords:
#print(f"\t{row}+{y} > {height}\t", f"{col}+{x} > {width}\t", f"{col}+{x} < 0")
if row+y > height-1:
if y+offset[0] >= height:
return True
if col+x >= width:
if x+offset[1] >= width:
return True
if col+x < 0:
if x+offset[1] < 0:
return True
return False
@ -117,24 +125,28 @@ def out_of_bounds(row, col, height, width, shape=None):
def part1(rows):
print(rows)
instructions = [r for r in rows]
height = 8
#height = 2022*4+4
height = 20
height = 2022*4+4
width = 7
view = height - 20
rock = 0
shapes = []
spawning = True
while rock < 2:
while rock < 2022:
if spawning:
if shapes:
for c in shapes[-1].coords:
all_coords.add(c)
if rock % 1000 == 0:
print("Spawn rock #", rock)
X = 2
Y = find_highest(shapes, height) - 4
shape = Shape(rock=rock, y=Y, x=X, shape=ORDER[rock%len(ORDER)])
shapes.append(shape)
spawning = False
render(width, height, shapes)
print("~"*20)
#render(width, height, shapes)
#print("~"*20)
# loop through instructions
@ -146,26 +158,26 @@ def part1(rows):
# Try to move right/left
print("Jet of gas pushes rock", next_move)
#print(f"Jet of gas pushes {shapes[-1].at_rest_char} rock", next_move)
# TODO: make COLLISION_AT TAKE AN OFFSET
next_x = OPS[next_move](shapes[-1].x, 1) # Try Move left or right
if not out_of_bounds(shapes[-1].y, next_x, height, width, shapes[-1].offsets) and
not collision_at(shapes, shapes[-1].y, next_x, shape):
shapes[-1].x = next_x
next_offset = OFF[next_move]
if not out_of_bounds(height,width, shapes, (0, next_offset[1])) and not collision_at(shapes, next_offset):
shapes[-1].x += next_offset[1]
else:
print("but nothing happens")
pass
#print("but nothing happens")
# check if hit bottom
next_y = shapes[-1].y + 1
if out_of_bounds(next_y, next_x, height, width, shapes[-1].offsets):
print("rock comes to rest")
next_offset = (1, next_offset[1])
if out_of_bounds(height, width, shapes, (next_offset[0],0)):
#print("rock comes to rest")
# hit bottom dont move down
shapes[-1].moving = False
spawning = True
rock += 1
continue
if collision_at(shapes, next_y, next_x, shape):
if collision_at(shapes, (next_offset[0], 0)):
# Hit another Block dont move down
shapes[-1].moving = False
spawning = True
@ -173,10 +185,9 @@ def part1(rows):
else:
# can move down
shapes[-1].y += 1
print("rock falls one unit")
#print("rock falls one unit")
print("-"*15)
render(width, height, shapes)
#render(width, height, shapes)
print(shapes[0], height, shapes[0].coords)