finish extra/bringing-a-gun-to-a-trainer-fight
This commit is contained in:
parent
c154de2065
commit
3f6a2428ff
10
README.md
10
README.md
@ -28,6 +28,12 @@
|
|||||||
* Completed in: 6 hrs, 49 mins, 26 secs.
|
* Completed in: 6 hrs, 49 mins, 26 secs.
|
||||||
* DP & Optimization
|
* DP & Optimization
|
||||||
|
|
||||||
|
## Encrypted message
|
||||||
|
CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY=
|
||||||
|
|
||||||
|
## Extra
|
||||||
|
* Requested problems after level 5 clear
|
||||||
|
|
||||||
### dodge-the-laser
|
### dodge-the-laser
|
||||||
* Completed in: 2 days, 20 hrs, 22 mins, 39 secs.
|
* Completed in: 2 days, 20 hrs, 22 mins, 39 secs.
|
||||||
* References
|
* References
|
||||||
@ -42,6 +48,6 @@
|
|||||||
* Reference
|
* Reference
|
||||||
* [Burnside's lemma: counting up to symmetries, Youtube](https://www.youtube.com/watch?v=D0d9bYZ_qDY)
|
* [Burnside's lemma: counting up to symmetries, Youtube](https://www.youtube.com/watch?v=D0d9bYZ_qDY)
|
||||||
|
|
||||||
## Encrypted message
|
### bringing-a-gun-to-a-trainer-fight
|
||||||
CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY=
|
* Completed in: 1 day, 23 hrs, 49 mins, 54 secs.
|
||||||
|
|
||||||
|
|||||||
47
extra/bringing-a-gun-to-a-trainer-fight/readme.txt
Normal file
47
extra/bringing-a-gun-to-a-trainer-fight/readme.txt
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
Bringing a Gun to a Trainer Fight
|
||||||
|
=================================
|
||||||
|
|
||||||
|
Uh-oh -- you've been cornered by one of Commander Lambdas elite bunny trainers! Fortunately, you grabbed a beam weapon from an abandoned storeroom 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 bunny trainers: its beams reflect off walls, meaning you'll have to be very careful where you shoot to avoid bouncing a shot toward yourself!
|
||||||
|
|
||||||
|
Luckily, the beams can only travel a certain maximum distance before becoming too weak to cause damage. You also know that if a beam hits a corner, it will bounce back in exactly the same direction. And of course, if the beam hits either you or the bunny trainer, it will stop immediately (albeit painfully).
|
||||||
|
|
||||||
|
Write a function solution(dimensions, your_position, trainer_position, distance) that gives an array of 2 integers of the width and height of the room, an array of 2 integers of your x and y coordinates in the room, an array of 2 integers of the trainer's x and y coordinates in the room, and returns an integer of the number of distinct directions that you can fire to hit the elite trainer, given the maximum distance that the beam can travel.
|
||||||
|
|
||||||
|
The room has integer dimensions [1 < x_dim <= 1250, 1 < y_dim <= 1250]. You and the elite trainer 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.
|
||||||
|
|
||||||
|
For example, if you and the elite trainer were positioned in a room with dimensions [3, 2], your_position [1, 1], trainer_position [2, 1], and a maximum shot distance of 4, you could shoot in seven different directions to hit the elite trainer (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 trainer 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 trainer with a total shot distance of sqrt(5).
|
||||||
|
|
||||||
|
Languages
|
||||||
|
=========
|
||||||
|
|
||||||
|
To provide a Java solution, edit Solution.java
|
||||||
|
To provide a Python solution, edit solution.py
|
||||||
|
|
||||||
|
Test cases
|
||||||
|
==========
|
||||||
|
Your code should pass the following test cases.
|
||||||
|
Note that it may also be run against hidden test cases not shown here.
|
||||||
|
|
||||||
|
-- Java cases --
|
||||||
|
Input:
|
||||||
|
Solution.solution([3,2], [1,1], [2,1], 4)
|
||||||
|
Output:
|
||||||
|
7
|
||||||
|
|
||||||
|
Input:
|
||||||
|
Solution.solution([300,275], [150,150], [185,100], 500)
|
||||||
|
Output:
|
||||||
|
9
|
||||||
|
|
||||||
|
-- Python cases --
|
||||||
|
Input:
|
||||||
|
solution.solution([3,2], [1,1], [2,1], 4)
|
||||||
|
Output:
|
||||||
|
7
|
||||||
|
|
||||||
|
Input:
|
||||||
|
solution.solution([300,275], [150,150], [185,100], 500)
|
||||||
|
Output:
|
||||||
|
9
|
||||||
|
|
||||||
|
Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.
|
||||||
92
extra/bringing-a-gun-to-a-trainer-fight/solution.py
Normal file
92
extra/bringing-a-gun-to-a-trainer-fight/solution.py
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
|
def solution(dimentions, your_position, trainer_position, distance):
|
||||||
|
nearest_target = {}
|
||||||
|
for position, is_trainer in generate_positions(dimentions, your_position, trainer_position, distance):
|
||||||
|
direction = calc_direction(your_position, position)
|
||||||
|
if direction in nearest_target:
|
||||||
|
prev_position, prev_is_trainer = nearest_target[direction]
|
||||||
|
if abs(position[0] - your_position[0]) < abs(prev_position[0] - your_position[0]) or abs(position[1] - your_position[1]) < abs(prev_position[1] - your_position[1]):
|
||||||
|
nearest_target[direction] = (position, is_trainer)
|
||||||
|
else:
|
||||||
|
nearest_target[direction] = (position, is_trainer)
|
||||||
|
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):
|
||||||
|
room_width = dimentions[0]
|
||||||
|
room_height = dimentions[1]
|
||||||
|
x_range = distance / room_width + 1
|
||||||
|
y_range = distance / room_height + 1
|
||||||
|
boundary = {0: y_range + 1}
|
||||||
|
for x in range(1, x_range):
|
||||||
|
while line_length(your_position, (x*room_width, y_range*room_height)) > distance and y_range > 0:
|
||||||
|
y_range -= 1
|
||||||
|
boundary[x] = y_range + 1
|
||||||
|
for x in range(x_range):
|
||||||
|
for y in range(boundary[x]):
|
||||||
|
yield x, y
|
||||||
|
yield x, - y - 1
|
||||||
|
yield - x - 1, y
|
||||||
|
yield - x - 1, - y - 1
|
||||||
|
|
||||||
|
def generate_positions(dimentions, your_position, trainer_position, distance):
|
||||||
|
room_width, room_height = dimentions
|
||||||
|
for room_x, room_y in generate_room_id(dimentions, your_position, distance):
|
||||||
|
x_reflected = room_x % 2 == 1
|
||||||
|
y_reflected = room_y % 2 == 1
|
||||||
|
x_base = room_x*room_width
|
||||||
|
y_base = room_y*room_height
|
||||||
|
your_x_offset = your_position[0]
|
||||||
|
your_y_offset = your_position[1]
|
||||||
|
trainer_x_offset = trainer_position[0]
|
||||||
|
trainer_y_offset = trainer_position[1]
|
||||||
|
if (x_reflected):
|
||||||
|
reflected_your_x_position = x_base + room_width - your_x_offset
|
||||||
|
reflected_trainer_x_position = x_base + room_width - trainer_x_offset
|
||||||
|
else:
|
||||||
|
reflected_your_x_position = x_base + your_x_offset
|
||||||
|
reflected_trainer_x_position = x_base + trainer_x_offset
|
||||||
|
if (y_reflected):
|
||||||
|
reflected_your_y_position = y_base + room_height - your_y_offset
|
||||||
|
reflected_trainer_y_position = y_base + room_height - trainer_y_offset
|
||||||
|
else:
|
||||||
|
reflected_your_y_position = y_base + your_y_offset
|
||||||
|
reflected_trainer_y_position = y_base + trainer_y_offset
|
||||||
|
yield (reflected_your_x_position, reflected_your_y_position), False
|
||||||
|
yield (reflected_trainer_x_position, reflected_trainer_y_position), True
|
||||||
|
|
||||||
|
def gcd(m, n):
|
||||||
|
while n != 0:
|
||||||
|
t = m % n
|
||||||
|
m, n = n, t
|
||||||
|
return m
|
||||||
|
|
||||||
|
def reduce_direction(direction):
|
||||||
|
x, y = direction
|
||||||
|
if x == 0 and y == 0:
|
||||||
|
return 0, 0
|
||||||
|
elif x == 0:
|
||||||
|
return 0, (1 if y > 0 else -1)
|
||||||
|
elif y == 0:
|
||||||
|
return (1 if x > 0 else -1), 0
|
||||||
|
g = gcd(abs(x), abs(y))
|
||||||
|
return x/g, y/g
|
||||||
|
|
||||||
|
def calc_direction(origin, target):
|
||||||
|
return reduce_direction((target[0] - origin[0], target[1] - origin[1]))
|
||||||
|
|
||||||
|
def line_length(origin, target):
|
||||||
|
return math.sqrt((target[0] - origin[0])**2 + (target[1] - origin[1])**2)
|
||||||
|
|
||||||
|
tests = [
|
||||||
|
(([3, 2], [1, 1], [2, 1], 4), 7),
|
||||||
|
(([300,275], [150,150], [185,100], 500), 9),
|
||||||
|
(([3, 2], [1, 1], [2, 1], 2), 1),
|
||||||
|
(([300,275], [150,150], [185,100], 2), 0),
|
||||||
|
(([3, 2], [1, 1], [2, 1], 1000), 397845),
|
||||||
|
(([30, 20], [10, 10], [20, 10], 10000), 397845),
|
||||||
|
]
|
||||||
|
|
||||||
|
for i, o in tests:
|
||||||
|
result = solution(*i)
|
||||||
|
print(i, result == o, result, o)
|
||||||
21
extra/dodge-the-lasers/constraints.txt
Normal file
21
extra/dodge-the-lasers/constraints.txt
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
Java
|
||||||
|
====
|
||||||
|
Your code will be compiled using standard Java 8. All tests will be run by calling the solution() method inside the Solution class
|
||||||
|
|
||||||
|
Execution time is limited.
|
||||||
|
|
||||||
|
Wildcard imports and some specific classes are restricted (e.g. java.lang.ClassLoader). You will receive an error when you verify your solution if you have used a blacklisted class.
|
||||||
|
|
||||||
|
Third-party libraries, input/output operations, spawning threads or processes and changes to the execution environment are not allowed.
|
||||||
|
|
||||||
|
Your solution must be under 32000 characters in length including new lines and and other non-printing characters.
|
||||||
|
|
||||||
|
Python
|
||||||
|
======
|
||||||
|
Your code will run inside a Python 2.7.13 sandbox. All tests will be run by calling the solution() function.
|
||||||
|
|
||||||
|
Standard libraries are supported except for bz2, crypt, fcntl, mmap, pwd, pyexpat, select, signal, termios, thread, time, unicodedata, zipimport, zlib.
|
||||||
|
|
||||||
|
Input/output operations are not allowed.
|
||||||
|
|
||||||
|
Your solution must be under 32000 characters in length including new lines and and other non-printing characters.
|
||||||
Loading…
x
Reference in New Issue
Block a user