r/adventofcode • u/FreeMarx • Dec 11 '17
Visualization MATLAB true geometric path plot
Here is a script in MATLAB that plots a true geometric path and solution visualization and gives a concept for solving the problem.
Should also be possible to run in Octave. I will try to upload a PNG later.
%% generate random path
a= 0:60:300;
mm= [sind(a(1:end)); cosd(a(1:end))];
vv= mm(:, randi(6, 500, 1));
vvv= [[0; 0] cumsum(vv, 2)];
%% solution directions
a= 0:60:120;
m= [sind(a(1:end)); cosd(a(1:end))];
%% solutions along the three principal directions
c1= m(:, [1 2])\vvv(:, end);
c2= m(:, [1 3])\vvv(:, end);
c3= m(:, [2 3])\vvv(:, end);
%% select shortest
cc= [[c1; 0] [c2(1) 0 c2(2)]' [0; c3]];
[~, minidx]= min(rms(cc));
c= cc(:, minidx)
%% grid radius
r= max(rms(vvv));
%% solution path
d= [[0; 0] cumsum(m.*repmat(c', 2, 1), 2)];
plot(vvv(1, :), vvv(2, :), '.-', vvv(1, end), vvv(2, end), 'ro', 0, 0, 'mo', d(1, :), d(2, :), 'r', [m(1, :); m(1, :)].*repmat([-r r]', 1, 3), [m(2, :); m(2, :)].*repmat([-r r]', 1, 3), 'k--');
axis equal;
2
Upvotes
View all comments
Show parent comment
1
u/KnorbenKnutsen Dec 11 '17
Then it's evolved since I taught it, because I allowed both MATLAB and Octave submissions in my class, and the comments was the one thing that prevented me to run Octave code in MATLAB. Well that and Octave's better print functions.