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