Guest User

Untitled

a guest
Dec 12th, 2020
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.02 KB | None | 0 0
  1. z   = importdata('input.txt');
  2. x   = 0;
  3. y   = 0;
  4. wp  = [1,10];
  5. for i=1:size(z,1)
  6.     inst =z{i}(1);
  7.     val  = str2double(z{i}(2:end));
  8.     switch inst
  9.         case 'N'
  10.             wp(1) = wp(1) + val;
  11.         case 'E'
  12.             wp(2) = wp(2) + val;
  13.         case 'S'
  14.             wp(1) = wp(1) - val;
  15.         case 'W'
  16.             wp(2) = wp(2) - val;
  17.         case 'L'
  18.             switch val
  19.                 case 90
  20.                     wp    = [wp(2),-wp(1)];
  21.                 case 180
  22.                     wp    = -wp;                    
  23.                 case 270
  24.                     wp    = [-wp(2),wp(1)];
  25.              end
  26.         case 'R'
  27.              switch val
  28.                  case 90
  29.                     wp    = [-wp(2),wp(1)];
  30.                 case 180
  31.                     wp    = -wp;                    
  32.                 case 270
  33.                     wp    = [wp(2),-wp(1)];
  34.             end
  35.         case 'F'
  36.             x = x + wp(2)*val;
  37.             y = y + wp(1)*val;
  38.     end
  39. end
  40. disp(abs(x)+abs(y))
Advertisement
Add Comment
Please, Sign In to add comment