home

Scientific Computing (Psychology 9040a)

Fall, 2021

Advent of Code 2015 Day 2


Solve the Advent Of Code 2015 Day 2 puzzle.

Your code should assume that the input is in a file named AOC2015Day2_input.txt

Hint: In Part 1 and in Part 2 you will need to find the smallest of three quantities. You can use the built-in function min() to do this:

% in Part 1:
slack = min([l*w,l*h,w*h]);

% in Part 2:
ribbon = min([l+l+w+w,l+l+h+h,w+w+h+h]);

Hint: To load in the input file line by line there are many ways to do it, but here is one way:

fid = fopen('AOC2015Day2_input.txt');
tline = fgetl(fid);
while ischar(tline)
    s = strsplit(tline, 'x');
    l = str2num(s{1});
    w = str2num(s{2});
    h = str2num(s{3});
    % <insert your code here to process the data>
    % ...
end
fclose(fid);

Here is another way that will use the MATLAB function importdata() to load in the entire file into one big cell array of character strings:

a4 = importdata('AOC2015Day2_input.txt');
>> disp(a4{1})
4x23x21

Then you can process each element of the cell array using the strsplit() and str2num() code as shown above.

There are other ways to read in the file as well. Whatever works for you is fine.