% Advent of Code 2015 Day 2
% https://adventofcode.com/2015/day/2

% load the data from the file using importdata()
boxes = importdata('aoc201502_input.txt');
n_boxes = length(boxes);
fprintf('found %d boxes\n', n_boxes);

% PART 1
total_paper = 0;
for i=1:n_boxes
    a_box = boxes{i};
    sides = split(a_box, 'x');
    l = str2num(sides{1});
    w = str2num(sides{2});
    h = str2num(sides{3});
    smallest_side = min([l*w, l*h, w*h]);
    a_box_paper = 2*l*w + 2*w*h + 2*h*l + smallest_side;
    total_paper = total_paper + a_box_paper;
end
fprintf('elves should order %d sq feet of paper\n', total_paper);

% PART 2

total_ribbon = 0;
for i=1:n_boxes
    a_box = boxes{i};
    sides = split(a_box, 'x');
    l = str2num(sides{1});
    w = str2num(sides{2});
    h = str2num(sides{3});
    sides_sorted = sort([l, w, h]);
    a_box_ribbon = sides_sorted(1)*2 + sides_sorted(2)*2 + l*w*h;
    total_ribbon = total_ribbon + a_box_ribbon;
end
fprintf('elves should order %d feet of ribbon\n', total_ribbon);

