Guest User

Untitled

a guest
Dec 13th, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.17 KB | None | 0 0
  1. fid = fopen('input.txt');
  2. s={};
  3. while 1
  4.     str = fgetl(fid);
  5.     if str==-1; break;end
  6.     s(end+1,:) = {str};
  7. end
  8.  
  9. % extract cart positions
  10. c = []; % row from top, column from left,heading (N,E,S,W = 1,2,3,4),turnIdx (1,2,3),hasCrashed]
  11. for i=1:size(s,1)
  12.    
  13.     p1 = strfind(s{i},'^');
  14.     p2 = strfind(s{i},'v');
  15.     p3 = strfind(s{i},'<');
  16.     p4 = strfind(s{i},'>');
  17.    
  18.     for j=1:length(p1)
  19.         c = [c;[i,p1(j)],1,1,0];
  20.         s{i}(p1(j)) = '|';
  21.     end
  22.    
  23.     for j=1:length(p2)
  24.         c = [c;[i,p2(j)],3,1,0];
  25.         s{i}(p2(j)) = '|';
  26.     end
  27.    
  28.     for j=1:length(p3)
  29.         c = [c;[i,p3(j)],4,1,0];
  30.         s{i}(p3(j)) = '-';
  31.     end
  32.    
  33.     for j=1:length(p4)
  34.         c = [c;[i,p4(j)],2,1,0];
  35.         s{i}(p4(j)) = '-';
  36.     end
  37. end
  38.  
  39. % parse grid
  40. % no path   = 0
  41. % -         = 1
  42. % |         = 2
  43. % /         = 3
  44. % \         = 4
  45. % +         = 5
  46.  
  47. g = zeros(size(s,1),length(s{1}));
  48. for i=1:size(s,1)
  49.    
  50.     for j = 1:length(s{i})
  51.        
  52.         if strcmp(s{i}(j),'-')
  53.             g(i,j) = 1;
  54.         end
  55.         if strcmp(s{i}(j),'|')
  56.             g(i,j) = 2;
  57.         end
  58.         if strcmp(s{i}(j),'/')
  59.             g(i,j) = 3;
  60.         end
  61.         if strcmp(s{i}(j),'\')
  62.             g(i,j) = 4;
  63.         end
  64.         if strcmp(s{i}(j),'+')
  65.             g(i,j) = 5;
  66.         end
  67.        
  68.     end
  69.    
  70. end
  71.  
  72.  
  73. %main loop over ticks
  74. t = 0;
  75. crash  = false;
  76. while ~crash
  77.     t=t+1;
  78.    
  79.     % loop over cart in correct order
  80.     c = sortrows(c);
  81.     for i = 1:size(c,1)
  82.        
  83.         x = c(i,1);
  84.         y = c(i,2);
  85.         h = c(i,3);
  86.         t = c(i,4);
  87.        
  88.         % -         = 1
  89.         % |         = 2
  90.         % /         = 3
  91.         % \         = 4
  92.         % +         = 5
  93.        
  94.         switch g(x,y)
  95.             case 1
  96.                 if h==2
  97.                     c(i,2) = y+1;
  98.                 else
  99.                     c(i,2) = y-1;
  100.                 end
  101.             case 2
  102.                 if h==1
  103.                     c(i,1) = x-1;
  104.                 else
  105.                     c(i,1) = x+1;
  106.                 end
  107.             case 3 % /
  108.                 if h==1
  109.                     c(i,3) = 2;
  110.                     c(i,2) = c(i,2) + 1;
  111.                 end
  112.                 if h==2
  113.                     c(i,3) = 1;
  114.                     c(i,1) = c(i,1) - 1;
  115.                 end
  116.                 if h==3
  117.                     c(i,3) = 4;
  118.                     c(i,2) = c(i,2) - 1;
  119.                 end
  120.                 if h==4
  121.                     c(i,3) = 3;
  122.                     c(i,1) = c(i,1) + 1;
  123.                 end
  124.                
  125.             case 4 %\
  126.                 if h==1
  127.                     c(i,3) = 4;
  128.                     c(i,2) = c(i,2) - 1;
  129.                 end
  130.                 if h==2
  131.                     c(i,3) = 3;
  132.                     c(i,1) = c(i,1) + 1;
  133.                 end
  134.                 if h==3
  135.                     c(i,3) = 2;
  136.                     c(i,2) = c(i,2) + 1;
  137.                 end
  138.                 if h==4
  139.                     c(i,3) = 1;
  140.                     c(i,1) = c(i,1) - 1;
  141.                 end
  142.             case 5 %+
  143.                 if t==1
  144.                     %turn left
  145.                     c(i,3) = h-1;
  146.                     if c(i,3)==0
  147.                         c(i,3) = 4;
  148.                     end
  149.                    
  150.                     %update turn index
  151.                     c(i,4) = 2;
  152.                    
  153.                 elseif t==2
  154.                     %update turn index
  155.                     c(i,4) = 3;
  156.                    
  157.                 elseif t==3
  158.                     %turn right
  159.                     c(i,3) = h+1;
  160.                     if c(i,3)==5
  161.                         c(i,3) = 1;
  162.                     end
  163.                    
  164.                     %update turn index
  165.                     c(i,4) = 1;
  166.                 end
  167.                
  168.                 %take step in new direction
  169.                 if c(i,3) == 1
  170.                     c(i,1) = c(i,1)-1;
  171.                 elseif c(i,3) == 2
  172.                     c(i,2) = c(i,2)+1;
  173.                 elseif c(i,3) == 3
  174.                     c(i,1) = c(i,1)+1;
  175.                 elseif c(i,3) == 4
  176.                     c(i,2) = c(i,2)-1;
  177.                 end
  178.                
  179.                
  180.                
  181.             otherwise
  182.                 disp('we shouldnt be here')
  183.                
  184.                
  185.                
  186.         end
  187.         % check collision
  188.        
  189.         for m = 1:size(c,1)
  190.             for n = 1:size(c,1)
  191.                 if m~=n
  192.                     if c(m,1)==c(n,1) && c(m,2)==c(n,2) && c(m,5)==0 && c(n,5)==0
  193.                         %disp('collision')
  194.                         c(m,5)=1;
  195.                         c(n,5)=1;
  196.                         %disp([c(m,2)-1,c(m,1)-1])
  197.                        
  198.                         %crash = true;
  199.                         if sum(c(:,5))==16
  200.                             h = find(c(:,5)==0);
  201.                             disp([c(h,2)-1,c(h,1)-1])
  202.                             pause
  203.                         end
  204.                        
  205.                     end
  206.                 end
  207.             end
  208.         end
  209.        
  210.     end
  211.    
  212. end
Advertisement
Add Comment
Please, Sign In to add comment