finish extra/bringing-a-gun-to-a-trainer-fight #9

Merged
seongbeom_park merged 11 commits from extra/bringing-a-gun-to-a-trainer-fight into main 2022-04-05 04:54:05 +00:00
Showing only changes of commit efeed5bf55 - Show all commits

View File

@ -2,7 +2,7 @@ import math
def solution(dimentions, your_position, trainer_position, distance): def solution(dimentions, your_position, trainer_position, distance):
nearest_target = {} nearest_target = {}
for position, is_trainer in generate_reflected_positions(dimentions, your_position, trainer_position, distance): for position, is_trainer in generate_positions(dimentions, your_position, trainer_position, distance):
direction = calc_direction(your_position, position) direction = calc_direction(your_position, position)
if direction in nearest_target: if direction in nearest_target:
prev_position, prev_is_trainer = nearest_target[direction] prev_position, prev_is_trainer = nearest_target[direction]
@ -10,7 +10,7 @@ def solution(dimentions, your_position, trainer_position, distance):
nearest_target[direction] = (position, is_trainer) nearest_target[direction] = (position, is_trainer)
else: else:
nearest_target[direction] = (position, is_trainer) nearest_target[direction] = (position, is_trainer)
return len([0 for d, (position, is_trainer) in nearest_target.items() if is_trainer and line_length(your_position, position) <= distance]) return sum(1 for (position, is_trainer) in nearest_target.values() if is_trainer and line_length(your_position, position) <= distance)
def generate_room_id(dimentions, your_position, distance): def generate_room_id(dimentions, your_position, distance):
memo = [] memo = []
@ -27,7 +27,7 @@ def generate_room_id(dimentions, your_position, distance):
yield room_x, - room_y - 1 yield room_x, - room_y - 1
yield - room_x - 1, - room_y - 1 yield - room_x - 1, - room_y - 1
def generate_reflected_positions(dimentions, your_position, trainer_position, distance): def generate_positions(dimentions, your_position, trainer_position, distance):
room_width, room_height = dimentions room_width, room_height = dimentions
for room_x, room_y in generate_room_id(dimentions, your_position, distance): for room_x, room_y in generate_room_id(dimentions, your_position, distance):
x_reflected = room_x % 2 == 1 x_reflected = room_x % 2 == 1
@ -53,8 +53,7 @@ def generate_reflected_positions(dimentions, your_position, trainer_position, di
yield (reflected_your_x_position, reflected_your_y_position), False yield (reflected_your_x_position, reflected_your_y_position), False
yield (reflected_trainer_x_position, reflected_trainer_y_position), True yield (reflected_trainer_x_position, reflected_trainer_y_position), True
def abs_gcd(x, y): def gcd(m, n):
m, n = abs(x), abs(y)
while n != 0: while n != 0:
t = m % n t = m % n
m, n = n, t m, n = n, t
@ -68,8 +67,8 @@ def reduce_direction(direction):
return 0, (1 if y > 0 else -1) return 0, (1 if y > 0 else -1)
elif y == 0: elif y == 0:
return (1 if x > 0 else -1), 0 return (1 if x > 0 else -1), 0
gcd = abs_gcd(x, y) g = gcd(abs(x), abs(y))
return x/gcd, y/gcd return x/g, y/g
def calc_direction(origin, target): def calc_direction(origin, target):
return reduce_direction((target[0] - origin[0], target[1] - origin[1])) return reduce_direction((target[0] - origin[0], target[1] - origin[1]))
@ -82,7 +81,8 @@ tests = [
(([300,275], [150,150], [185,100], 500), 9), (([300,275], [150,150], [185,100], 500), 9),
(([3, 2], [1, 1], [2, 1], 2), 1), (([3, 2], [1, 1], [2, 1], 2), 1),
(([300,275], [150,150], [185,100], 2), 0), (([300,275], [150,150], [185,100], 2), 0),
# (([3, 2], [1, 1], [2, 1], 10000), 1), (([3, 2], [1, 1], [2, 1], 1000), 397845),
(([30, 20], [10, 10], [20, 10], 10000), 397845),
] ]
for i, o in tests: for i, o in tests: