Advertisement
Guest User

Untitled

a guest
Dec 10th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. clc;clear;close all
  2.  
  3. tic
  4. disp('starting')
  5. fid = fopen('input.txt','r');
  6.  
  7.  
  8. % Check for type of values, number of robots and outputs: value > 0, 210 bots and 20 outputs
  9.  
  10. allbots=[];
  11. alloutputs=[];
  12. allvalues=[];
  13. instrList={};
  14.  
  15. while 1
  16. str = fgetl(fid);
  17. if str==-1
  18. break
  19. else
  20. instrList{end+1,1} = str;
  21. if strfind(str,'value')
  22. A = sscanf(str,'value %i goes to bot %i');
  23. allvalues=[allvalues,A(1)];
  24. allbots=[allbots,A(2)];
  25. else
  26. A1 = sscanf(str,'bot %i gives low to bot %i and high to bot %i');
  27. if length(A1)==3
  28. allbots=[allbots,A1(2)];
  29. allbots=[allbots,A1(3)];
  30. end
  31. A2 = sscanf(str,'bot %i gives low to output %i and high to bot %i');
  32. if length(A2)==3
  33. allbots=[allbots,A2(3)];
  34. alloutputs=[alloutputs,A2(2)];
  35. end
  36.  
  37. A3 = sscanf(str,'bot %i gives low to bot %i and high to output %i');
  38. if length(A3)==3
  39. allbots=[allbots,A3(2)];
  40. alloutputs=[alloutputs,A3(3)];
  41. end
  42.  
  43. A4 = sscanf(str,'bot %i gives low to output %i and high to output %i');
  44. if length(A4)==3
  45. alloutputs=[alloutputs,A4(2)];
  46. alloutputs=[alloutputs,A4(3)];
  47. end
  48.  
  49. end
  50. end
  51. end
  52.  
  53.  
  54. allvalues = unique(allvalues)';
  55. allbots = unique(allbots)' ;
  56. alloutputs = unique(alloutputs)' ;
  57.  
  58. B = zeros(length(allbots),2);
  59. O = zeros(length(alloutputs),1);
  60.  
  61. % Loop through instructions: 1st loop assign only values
  62. for i=1:size(instrList,1)
  63. str = instrList{i,1};
  64. if strfind(str,'value')
  65. A = sscanf(str,'value %i goes to bot %i');
  66. A(2)=A(2)+1; % zero indexing
  67. B = giveBotValue(B,A(2),A(1));
  68. else % fill other instruction list
  69. A = sscanf(str,'bot %i gives');
  70. giveInstr{A+1,1}=str;
  71. end
  72. end
  73.  
  74. % main loop:
  75. while any(sum(B>0,2)==2)
  76.  
  77. idx=find(sum(B>0,2)==2);
  78. idx=idx(1);
  79.  
  80. if B(idx,1)==17 && B(idx,2)==61
  81. disp('Bot found')
  82. idx-1 %answer part A
  83. end
  84. str = giveInstr{idx,1};
  85.  
  86. A1 = sscanf(str,'bot %i gives low to bot %i and high to bot %i');
  87. if length(A1)==3
  88. A1=A1+1; % zero indexing
  89. if B(A1(1),1) && B(A1(1),2) % do nothing if robot is not full
  90. lowval = B(A1(1),1);
  91. highval = B(A1(1),2);
  92. B(A1(1),1) = 0;
  93. B(A1(1),2) = 0;
  94. B = giveBotValue(B,A1(2),lowval);
  95. B = giveBotValue(B,A1(3),highval);
  96. end
  97. end
  98. A2 = sscanf(str,'bot %i gives low to output %i and high to bot %i');
  99. if length(A2)==3
  100. A2=A2+1; % zero indexing
  101. if B(A2(1),1) && B(A2(1),2) % do nothing if robot is not full
  102. lowval = B(A2(1),1);
  103. highval = B(A2(1),2);
  104. B(A2(1),1) = 0;
  105. B(A2(1),2) = 0;
  106. O(A2(2)) = lowval;
  107. B = giveBotValue(B,A2(3),highval);
  108. end
  109. end
  110.  
  111. A3 = sscanf(str,'bot %i gives low to bot %i and high to output %i');
  112. if length(A3)==3
  113. A3=A3+1; % zero indexing
  114. if B(A3(1),1) && B(A3(1),2) % do nothing if robot is not full
  115. lowval = B(A3(1),1);
  116. highval = B(A3(1),2);
  117. B(A3(1),1) = 0;
  118. B(A3(1),2) = 0;
  119. O(A3(3)) = highval;
  120. B = giveBotValue(B,A3(2),lowval);
  121. end
  122. end
  123.  
  124. A4 = sscanf(str,'bot %i gives low to output %i and high to output %i');
  125. if length(A4)==3
  126. A4=A4+1; % zero indexing
  127. if B(A4(1),1) && B(A4(1),2) % do nothing if robot is not full
  128. lowval = B(A4(1),1);
  129. highval = B(A4(1),2);
  130. B(A4(1),1) = 0;
  131. B(A4(1),2) = 0;
  132. O(A4(2)) = lowval;
  133. O(A4(3)) = highval;
  134. end
  135. end
  136. end
  137.  
  138. O(1)*O(2)*O(3) % %answer part B
  139. disp('finished')
  140. toc
  141.  
  142. function B = giveBotValue(B,idx,v)
  143.  
  144. if B(idx,1)>0
  145. if B(idx,2)>0
  146. error('Bot full, cannot assign new idxalue')
  147. else
  148. if v>=B(idx,1)
  149. B(idx,2)=v;
  150. else
  151. B(idx,2)=B(idx,1);
  152. B(idx,1)=v;
  153. end
  154. end
  155. else
  156. if B(idx,2)>0
  157. if v>=B(idx,2)
  158. B(idx,1)=B(idx,2);
  159. B(idx,2)=v;
  160. else
  161. B(idx,1)=v;
  162. end
  163. else
  164. B(idx,1)=v;
  165. end
  166. end
  167. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement