This page contains the solution of Commander Lambda Python Problem

Commander Lambdas elite guards!!

The Question as follows:

Uh-oh - you’ve been cornered by one of Commander Lambdas elite guards! Fortunately, you grabbed a beam weapon from an abandoned guard post while you were running through the station, so you have a chance to fight your way out. But the beam weapon is potentially dangerous to you as well as to the elite guard: its beams reflect off walls, meaning you’ll have to be very careful where you shoot to avoid bouncing a shot toward yourself!

The given Constrains are

The room has integer dimensions $ [1 < x_dim <= 1250, 1 < y_dim <= 1250]$. You and the elite guard are both positioned on the integer lattice at different distinct positions $(x, y)$ inside the room such that $ [0 < x < x_dim, 0 < y < y_dim]$. Finally, the maximum distance that the beam can travel before becoming harmless will be given as an integer $1 < distance <= 10000$.

Explanation

If you and the elite guard were positioned in a room with dimensions $[3, 2]$, your_position $[1, 1]$, guard_position $[2, 1]$, and a maximum shot distance of 4, you could shoot in seven different directions to hit the elite guard (given as vector bearings from your location): $[1, 0], [1, 2], [1, -2], [3, 2], [3, -2], [-3, 2], and [-3, -2]$. As specific examples, the shot at bearing [1, 0] is the straight line horizontal shot of distance $1$, the shot at bearing $[-3, -2]$ bounces off the left wall and then the bottom wall before hitting the elite guard with a total shot distance of sqrt(13), and the shot at bearing $[1, 2]$ bounces off just the top wall before hitting the elite guard with a total shot distance of $sqrt(5)$.

Sample Input & Output

Input: solution.solution([300,275], [150,150], [185,100], 500) Output: 9

The Solution Part

from math import *

BOTTOM_LEFT = (0,0)
TOP_RIGHT   = (0,0)
SOURCE      = (0,0)
TARGET      = (0,0)
DISTANCE    = 0
BOTTOM_RIGHT    = (0,0)
TOP_LEFT    = (0,0)

def answer(tr, src, bp, dist):
    global BOTTOM_LEFT
    BOTTOM_LEFT = (0,0)
    global TOP_RIGHT
    TOP_RIGHT = tr
    global SOURCE
    SOURCE = src
    global TARGET
    TARGET = bp
    global DISTANCE
    DISTANCE = dist
    global BOTTOM_RIGHT
    BOTTOM_RIGHT = (TOP_RIGHT[0], BOTTOM_LEFT[1])
    global TOP_LEFT
    TOP_LEFT = (BOTTOM_LEFT[0], TOP_RIGHT[1])
    all_targets = get_targets(start_point = TARGET, distance = DISTANCE)
    CORNERS = [BOTTOM_LEFT, BOTTOM_RIGHT, TOP_LEFT, TOP_RIGHT]
    corner_angles = [4]
    i = 0
    while i < 4:
        ca = degrees(atan2(CORNERS[i][1] - SOURCE[1], CORNERS[i][0] - SOURCE[0]))
        corner_angles.append(ca)
        i = i+1
    valid_angles = set()
    for tgt in all_targets:
        pa = degrees(atan2(tgt[1] - SOURCE[1], tgt[0] - SOURCE[0]))
        if(tgt[0] - SOURCE[0] > 0 ):
            valid_angles.add(pa)
        elif(tgt[0] - SOURCE[0] < 0):
            if pa % 45 != 0 and pa not in corner_angles:
                valid_angles.add(pa)
        else:
            valid_angles.add(pa)
    if(src == bp):
        return 0
    else:
        return len(valid_angles)

def get_mirrored(point):
    ret = []
    x = point[0]
    y = point[1] - 2*(point[1] - TOP_RIGHT[1])
    ret.append((x,y))
    ret.append((point[0], point[1] - 2*(point[1] - BOTTOM_LEFT[1])))
    ret.append((point[0] - 2*(point[0] - BOTTOM_LEFT[0]), point[1]))
    ret.append((point[0] - 2*(point[0] - TOP_RIGHT[0]), point[1]))
    return ret

def get_targets(start_point, distance):
    targets = []
    targets.append(start_point)
    all_targets = set()
    all_targets.add((start_point[0],start_point[1]))
    last_targets = all_targets
    while True:
        new_level_targets = set()
        for tgt in last_targets:
            new_targets = get_mirrored(tgt)
            new_targets = set(t for t in new_targets if hypot(SOURCE[0] - t[0], SOURCE[1] - t[1]) <= DISTANCE)
            new_targets -= all_targets
            new_level_targets |= new_targets
        if not new_level_targets:
            break
        all_targets |= new_level_targets
        last_targets = new_level_targets
    return all_targets

print(answer((3,2), (1,1), (2,1), 4))
print(answer((300,275), (150,150), (185,100), 500))

Hope it was helpful for you!!


Don't Forget to visit @shadow-prince