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