reduce sqrt calculation
Verifying solution... Test 1 passed! Test 2 passed! Test 3 failed [Hidden] Test 4 passed! [Hidden] Test 5 passed! [Hidden] Test 6 passed! [Hidden] Test 7 passed! [Hidden] Test 8 passed! [Hidden] Test 9 passed! [Hidden] Test 10 passed! [Hidden]
This commit is contained in:
parent
bbb6e56815
commit
26d04b4f08
@ -2,21 +2,14 @@ import math
|
||||
|
||||
def solution(dimentions, your_position, trainer_position, distance):
|
||||
direction_target_map = {}
|
||||
count = 0
|
||||
for reflected_position, direction, length, is_trainer in generate_reflected_positions(dimentions, your_position, trainer_position, distance):
|
||||
for position, direction, is_trainer in generate_reflected_positions(dimentions, your_position, trainer_position, distance):
|
||||
if direction in direction_target_map:
|
||||
prev_length, prev_is_trainer = direction_target_map[direction]
|
||||
if length < prev_length:
|
||||
if is_trainer and ~prev_is_trainer:
|
||||
count += 1
|
||||
elif ~is_trainer and prev_is_trainer:
|
||||
count -= 1
|
||||
direction_target_map[direction] = (length, is_trainer)
|
||||
prev_is_trainer, prev_position = direction_target_map[direction]
|
||||
if abs(position[0] - your_position[0]) < abs(prev_position[0] - your_position[0]):
|
||||
direction_target_map[direction] = (is_trainer, position)
|
||||
else:
|
||||
if is_trainer:
|
||||
count += 1
|
||||
direction_target_map[direction] = (length, is_trainer)
|
||||
return count
|
||||
direction_target_map[direction] = (is_trainer, position)
|
||||
return len([0 for d, (is_trainer, position) in direction_target_map.items() if is_trainer and line_length(your_position, position) <= distance])
|
||||
|
||||
def generate_room_id(dimentions, your_position, distance):
|
||||
memo = []
|
||||
@ -53,15 +46,11 @@ def generate_reflected_positions(dimentions, your_position, trainer_position, di
|
||||
reflected_your_position = x_base + your_x_offset, y_base + your_y_offset
|
||||
your_direction = calc_direction(your_position, reflected_your_position)
|
||||
if your_direction not in memo:
|
||||
reflected_your_distance = line_length(your_position, reflected_your_position)
|
||||
if reflected_your_distance <= distance:
|
||||
yield reflected_your_position, your_direction, reflected_your_distance, False
|
||||
yield reflected_your_position, your_direction, False
|
||||
reflected_trainer_position = x_base + trainer_x_offset, y_base + trainer_y_offset
|
||||
trainer_direction = calc_direction(your_position, reflected_trainer_position)
|
||||
if trainer_direction not in memo:
|
||||
reflected_trainer_distance = line_length(your_position, reflected_trainer_position)
|
||||
if reflected_trainer_distance <= distance:
|
||||
yield reflected_trainer_position, trainer_direction, reflected_trainer_distance, True
|
||||
yield reflected_trainer_position, trainer_direction, True
|
||||
memo[your_direction] = 0
|
||||
memo[trainer_direction] = 0
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user