3 ms·
Here is the pseudo-code for matrix rotation for comparison: def rotate(vector, angle): rotation_matrix = [ [cos(angle), -sin(angle)], [sin(an
by deckar01 10y ago
Here is the pseudo-code for matrix rotation for comparison:
def rotate(vector, angle):
rotation_matrix = [
[cos(angle), -sin(angle)],
[sin(angle), cos(angle)]
]
return rotation_matrix * vector
position = (0, 0)
velocity = (0, 1)
for instruction in [x.strip() for x in input.split(",")]:
direction = instruction[0]
distance = int(instruction[1:])
angle = 90 if direction == 'L' else -90
velocity = rotate(velocity, angle)
position += velocity * distance
print position
- andars 10y agoSince you only work with 90 degrees, you don't even need the full machinery. In my solution I used (a,b) -> (-b,a) and its dual.