optimizations

This commit is contained in:
Tyrel Souza 2022-12-17 23:40:05 -05:00
parent 9611bfedf4
commit 58f2b05590

View File

@ -1,7 +1,8 @@
import matrix import matrix
import shared import shared
from dataclasses import dataclass from dataclasses import dataclass, field
from typing import Tuple from functools import cached_property
from typing import Tuple, List
import operator import operator
@ -66,30 +67,20 @@ class Shape:
@property @property
def coords(self): def coords(self):
actual_coords = [] return [ (y+self.y,x+self.x) for y,x in self.shape[1]]
for y,x in self.shape[1]:
actual_coords.append((y+self.y,x+self.x))
return actual_coords
# TODO: check left/right movement into an object
def find_highest(shapes, default_height): def find_highest(shapes, default_height):
if not shapes: if not shapes:
return default_height return default_height
all_y = [] #return min([y for s in shapes for y,_ in s.coords])
for s in shapes: return min([s.coords[0][0] for s in shapes[-20:]])
for y,_ in s.coords: #all_y = []
all_y.append(y) #for s in shapes[-20:]:
return min(all_y) # all_y.append(s.coords[0][0])
#return min(all_y)
def all_coords(shapes):
coords = set()
for s in shapes:
for c in s.coords:
coords.add(c)
return coords
all_coords = set() all_coords = set()
@ -119,11 +110,8 @@ def out_of_bounds(height, width, shapes, offset):
return False return False
@shared.profile
# @shared.profile
def part1(rows): def part1(rows):
print(rows)
instructions = [r for r in rows] instructions = [r for r in rows]
height = 20 height = 20
height = 2022*4+4 height = 2022*4+4
@ -133,13 +121,12 @@ def part1(rows):
rock = 0 rock = 0
shapes = [] shapes = []
spawning = True spawning = True
while rock < 2022: while rock < 2022: #1_000_000_000_000:
if spawning: if spawning:
# Add last rock's coords to all coords
if shapes: if shapes:
for c in shapes[-1].coords: for c in shapes[-1].coords:
all_coords.add(c) all_coords.add(c)
if rock % 1000 == 0:
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)])
@ -158,19 +145,14 @@ def part1(rows):
# Try to move right/left # Try to move right/left
#print(f"Jet of gas pushes {shapes[-1].at_rest_char} rock", next_move)
next_offset = OFF[next_move] next_offset = OFF[next_move]
if not out_of_bounds(height,width, shapes, (0, next_offset[1])) and not collision_at(shapes, next_offset): 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] shapes[-1].x += next_offset[1]
else:
pass
#print("but nothing happens")
# check if hit bottom # check if hit bottom
next_offset = (1, next_offset[1]) next_offset = (1, next_offset[1])
if out_of_bounds(height, width, shapes, (next_offset[0],0)): if out_of_bounds(height, width, shapes, (next_offset[0],0)):
#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
@ -185,13 +167,8 @@ def part1(rows):
else: else:
# can move down # can move down
shapes[-1].y += 1 shapes[-1].y += 1
#print("rock falls one unit")
#render(width, height, shapes) #render(width, height, shapes)
print(shapes[0], height, shapes[0].coords)
print("LAST ROCK COUNT", rock+1)
print(height - find_highest(shapes, height)) print(height - find_highest(shapes, height))