day17 - small optimizations
This commit is contained in:
parent
71c4075d48
commit
553705dc49
21
day17.py
21
day17.py
@ -36,16 +36,10 @@ def collide_hor(chamber: list, rock_shape:list, rock_x: int, rock_y: int) -> boo
|
||||
|
||||
def settles(chamber: list, rock_shape: list, rock_x: int, rock_y: int) -> bool:
|
||||
for i, c in enumerate(rock_shape[-1]):
|
||||
if c == ' ':
|
||||
continue
|
||||
if rock_y >= -1 and chamber[rock_y + 1][i + rock_x] == '#':
|
||||
if c != ' ' and rock_y >= -1 and chamber[rock_y + 1][i + rock_x] == '#':
|
||||
return True
|
||||
|
||||
if rock_shape[-1] == ' # ':
|
||||
for i, c in enumerate(rock_shape[-2]):
|
||||
if c == ' ':
|
||||
continue
|
||||
if rock_y > -1 and chamber[rock_y][i + rock_x] == '#':
|
||||
if rock_shape[-1] == ' # ' and rock_y >= 0 and (chamber[rock_y][rock_x] == '#' or chamber[rock_y][rock_x + 2] == '#'):
|
||||
return True
|
||||
|
||||
return False
|
||||
@ -109,12 +103,10 @@ class Day(AOCDay):
|
||||
return len(chamber) - 1
|
||||
|
||||
def part2(self) -> Any:
|
||||
drop_count = 1_000_000_000_000
|
||||
chamber = Chamber(['#######'])
|
||||
jet = self.get_jet_pattern()
|
||||
jet_index = drop_rock(chamber, ROCK_SHAPES[0], jet, 0)
|
||||
rock_index = 1
|
||||
cycle = 1
|
||||
cycle_cache = {}
|
||||
jet_index, rock_index, cycle, cycle_cache = 0, 0, 0, {}
|
||||
while (jet_index, rock_index, chamber.hash()) not in cycle_cache:
|
||||
cycle_cache[(jet_index, rock_index, chamber.hash())] = (cycle, len(chamber) - 1)
|
||||
jet_index = drop_rock(chamber, ROCK_SHAPES[rock_index], jet, jet_index)
|
||||
@ -128,13 +120,12 @@ class Day(AOCDay):
|
||||
chamber = Chamber(['#######'])
|
||||
jet_index = 0
|
||||
rock_index = 0
|
||||
bottom_part = 1_000_000_000_000 % cycle_len
|
||||
for _ in range(cycle_len + bottom_part):
|
||||
for _ in range(cycle_len + drop_count % cycle_len):
|
||||
jet_index = drop_rock(chamber, ROCK_SHAPES[rock_index % 5], jet, jet_index)
|
||||
rock_index += 1
|
||||
cur_len = len(chamber) - 1 - cycle_height
|
||||
|
||||
return (1_000_000_000_000 // cycle_len * cycle_height) + cur_len
|
||||
return (drop_count // cycle_len * cycle_height) + cur_len
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Loading…
Reference in New Issue
Block a user