Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tower = []
- highest = 0
- shapes = list(patterns.keys())
- wind_pattern_count = len(line)
- iteration_count = 0
- cache = {}
- rock_height_cache = {}
- break_outer_loop = False
- rocks_to_drop = 1000000000000
- for rock_number in tqdm(range(rocks_to_drop)):
- to_add = 10 - (len(tower) - highest)
- for k in range(to_add):
- tower.append([0 for i in range(7)])
- rock = shapes[rock_number % 5]
- rock_pattern = patterns[rock]
- left_edge = left_edges[rock]
- right_edge = right_edges[rock]
- bottom_edge = bottom_edges[rock]
- rock_y = highest + 3
- rock_x = 2
- rock_height = len(rock_pattern)
- rock_width = len(rock_pattern[0])
- start_wind = iteration_count % wind_pattern_count
- while True:
- end_wind = iteration_count % wind_pattern_count
- wind_pattern = line[end_wind]
- wind_pattern = 1 if wind_pattern == '>' else -1
- iteration_count += 1
- is_safe = True
- if wind_pattern == 1:
- if rock_x + rock_width > 6:
- is_safe = False
- else:
- is_safe = not any(tower[rock_y + p][rock_x + x_offset] == 1 for p, x_offset in enumerate(right_edge))
- else:
- if rock_x - 1 < 0:
- is_safe = False
- else:
- is_safe = not any(tower[rock_y + p][rock_x + x_offset] == 1 for p, x_offset in enumerate(left_edge))
- if is_safe:
- rock_x += wind_pattern
- is_settled = False
- if rock_y == 0:
- is_settled = True
- else:
- is_settled = any(tower[rock_y + y_offset][rock_x + p] == 1 for p, y_offset in enumerate(bottom_edge))
- if is_settled:
- for i in range(rock_height):
- for j in range(rock_width):
- if rock_pattern[i][j] == 1:
- tower[i+rock_y][j+rock_x] = rock_pattern[i][j]
- highest = max(highest, rock_y + rock_height)
- if highest == rock_height + rock_y:
- if (rock, start_wind, end_wind) in cache:
- if (rock, start_wind, end_wind) in cache:
- cache_value = cache[(rock, start_wind, end_wind)]
- if len(cache_value) > 1:
- # stable loop
- if cache_value[-1] - cache_value[-2] == rock_number - cache_value[-1]:
- break_outer_loop = True
- break
- cache[(rock, start_wind, end_wind)] = cache.get((rock, start_wind, end_wind), [])
- cache[(rock, start_wind, end_wind)].append(rock_number)
- rock_height_cache[rock_number] = highest
- break
- else:
- rock_y -= 1
- if break_outer_loop:
- break
- if break_outer_loop:
- rocks_fallen = rock_number - earlier_rock_number
- height_of_loop = highest - rock_height_cache[earlier_rock_number]
- remaining = (rocks_to_drop - rock_number) % rocks_fallen
- highest = highest + ((rocks_to_drop - rock_number) // rocks_fallen) * height_of_loop + rock_height_cache[remaining + earlier_rock_number] - rock_height_cache[earlier_rock_number] - 1
- print(highest)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement