Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clc;clear;close all
- tic
- disp('starting')
- fid = fopen('input.txt','r');
- % Check for type of values, number of robots and outputs: value > 0, 210 bots and 20 outputs
- allbots=[];
- alloutputs=[];
- allvalues=[];
- instrList={};
- while 1
- str = fgetl(fid);
- if str==-1
- break
- else
- instrList{end+1,1} = str;
- if strfind(str,'value')
- A = sscanf(str,'value %i goes to bot %i');
- allvalues=[allvalues,A(1)];
- allbots=[allbots,A(2)];
- else
- A1 = sscanf(str,'bot %i gives low to bot %i and high to bot %i');
- if length(A1)==3
- allbots=[allbots,A1(2)];
- allbots=[allbots,A1(3)];
- end
- A2 = sscanf(str,'bot %i gives low to output %i and high to bot %i');
- if length(A2)==3
- allbots=[allbots,A2(3)];
- alloutputs=[alloutputs,A2(2)];
- end
- A3 = sscanf(str,'bot %i gives low to bot %i and high to output %i');
- if length(A3)==3
- allbots=[allbots,A3(2)];
- alloutputs=[alloutputs,A3(3)];
- end
- A4 = sscanf(str,'bot %i gives low to output %i and high to output %i');
- if length(A4)==3
- alloutputs=[alloutputs,A4(2)];
- alloutputs=[alloutputs,A4(3)];
- end
- end
- end
- end
- allvalues = unique(allvalues)';
- allbots = unique(allbots)' ;
- alloutputs = unique(alloutputs)' ;
- B = zeros(length(allbots),2);
- O = zeros(length(alloutputs),1);
- % Loop through instructions: 1st loop assign only values
- for i=1:size(instrList,1)
- str = instrList{i,1};
- if strfind(str,'value')
- A = sscanf(str,'value %i goes to bot %i');
- A(2)=A(2)+1; % zero indexing
- B = giveBotValue(B,A(2),A(1));
- else % fill other instruction list
- A = sscanf(str,'bot %i gives');
- giveInstr{A+1,1}=str;
- end
- end
- % main loop:
- while any(sum(B>0,2)==2)
- idx=find(sum(B>0,2)==2);
- idx=idx(1);
- if B(idx,1)==17 && B(idx,2)==61
- disp('Bot found')
- idx-1 %answer part A
- end
- str = giveInstr{idx,1};
- A1 = sscanf(str,'bot %i gives low to bot %i and high to bot %i');
- if length(A1)==3
- A1=A1+1; % zero indexing
- if B(A1(1),1) && B(A1(1),2) % do nothing if robot is not full
- lowval = B(A1(1),1);
- highval = B(A1(1),2);
- B(A1(1),1) = 0;
- B(A1(1),2) = 0;
- B = giveBotValue(B,A1(2),lowval);
- B = giveBotValue(B,A1(3),highval);
- end
- end
- A2 = sscanf(str,'bot %i gives low to output %i and high to bot %i');
- if length(A2)==3
- A2=A2+1; % zero indexing
- if B(A2(1),1) && B(A2(1),2) % do nothing if robot is not full
- lowval = B(A2(1),1);
- highval = B(A2(1),2);
- B(A2(1),1) = 0;
- B(A2(1),2) = 0;
- O(A2(2)) = lowval;
- B = giveBotValue(B,A2(3),highval);
- end
- end
- A3 = sscanf(str,'bot %i gives low to bot %i and high to output %i');
- if length(A3)==3
- A3=A3+1; % zero indexing
- if B(A3(1),1) && B(A3(1),2) % do nothing if robot is not full
- lowval = B(A3(1),1);
- highval = B(A3(1),2);
- B(A3(1),1) = 0;
- B(A3(1),2) = 0;
- O(A3(3)) = highval;
- B = giveBotValue(B,A3(2),lowval);
- end
- end
- A4 = sscanf(str,'bot %i gives low to output %i and high to output %i');
- if length(A4)==3
- A4=A4+1; % zero indexing
- if B(A4(1),1) && B(A4(1),2) % do nothing if robot is not full
- lowval = B(A4(1),1);
- highval = B(A4(1),2);
- B(A4(1),1) = 0;
- B(A4(1),2) = 0;
- O(A4(2)) = lowval;
- O(A4(3)) = highval;
- end
- end
- end
- O(1)*O(2)*O(3) % %answer part B
- disp('finished')
- toc
- function B = giveBotValue(B,idx,v)
- if B(idx,1)>0
- if B(idx,2)>0
- error('Bot full, cannot assign new idxalue')
- else
- if v>=B(idx,1)
- B(idx,2)=v;
- else
- B(idx,2)=B(idx,1);
- B(idx,1)=v;
- end
- end
- else
- if B(idx,2)>0
- if v>=B(idx,2)
- B(idx,1)=B(idx,2);
- B(idx,2)=v;
- else
- B(idx,1)=v;
- end
- else
- B(idx,1)=v;
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement