Hacker News new | ask | show | jobs
by deckar01 3490 days 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
1 comments

Since 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.