Fall, 2021
Solve the Advent Of Code 2015 Day 6 puzzle.
You can use a 1000 x 1000 matrix to represent the grid of lights.
Here is some MATLAB code that you can use to load in the input file. In your MATLAB script, assume the input file is named AOC2015Day6_input.txt
), and process each line of the file, first splitting the instructions into a list of items:
fid = fopen('AOC2015Day6_input.txt');
tline = fgetl(fid);
while ischar(tline)
l = strsplit(tline, ' ');
% now process the line based on what the instruction is
% <insert your code here>
% ...
tline = fgetl(fid);
end
fclose(fid);
The other way of loading the file is to load it all at once, into a cell array of strings (one string for each line in the file), using the importdata()
function:
I = importdata('AOC2015Day6_input.txt', '%s');
You will need some if-statements to detemine which instruction it is, and how to act on it.
You can use the strcmp()
function in MATLAB to compare two strings (type help strcmp
at the MATLAB command line to see how it works).
You can use the ~
operator to toggle between 1 and 0. ~0
becomes 1
and ~1
becomes 0
.
Don’t forget that in MATLAB array indexing starts at 1 not at 0.
You can perform operations on a whole sub-range of a matrix at once. For example to add 1
to a 1000x1000 matrix L
from (x1,y1) = (3,4)
to (x2,y2) = (10,11)
you would write:
L(3:10,4:11) = L(3:10,4:11) + 1;