From 66d4af624909b60f2c922aea439fa65d3cf65b46 Mon Sep 17 00:00:00 2001 From: Sebastian Lukas Date: Fri, 23 Dec 2022 11:32:38 +0100 Subject: [PATCH] Solution for 2015 Day 2 part 2 (C++) Signed-off-by: Sebastian Lukas --- 2015/Day2/day2.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/2015/Day2/day2.cpp b/2015/Day2/day2.cpp index 257e9ce..9dd1f6d 100644 --- a/2015/Day2/day2.cpp +++ b/2015/Day2/day2.cpp @@ -22,23 +22,26 @@ int main() { std::string line; std::vector v_input; int wrapping_paper_sqfeet = 0; - int actual_box = 0; - int side_l_w, side_w_h, side_h_l = 0; + + int total_ribbon = 0; if (input.good()) { while (input >> line) { v_input = split(line, 'x'); - side_l_w = v_input[0] * v_input[1]; - side_w_h = v_input[1] * v_input[2]; - side_h_l = v_input[2] * v_input[0]; + int side_l_w = v_input[0] * v_input[1]; + int side_w_h = v_input[1] * v_input[2]; + int side_h_l = v_input[2] * v_input[0]; std::vector sides {side_l_w, side_w_h, side_h_l}; - actual_box = 2 * side_l_w + 2 * side_w_h + 2 * side_h_l + *std::min_element(sides.begin(), sides.end()); + wrapping_paper_sqfeet += 2 * side_l_w + 2 * side_w_h + 2 * side_h_l + *std::min_element(sides.begin(), sides.end()); - wrapping_paper_sqfeet += actual_box; + std::sort(v_input.begin(), v_input.end()); + total_ribbon += v_input[0] + v_input[0] + v_input[1] + v_input[1] + v_input[0] * v_input[1] * v_input[2]; + } } - std::cout << "Total sqaure feet of wrapping paper: " << wrapping_paper_sqfeet; + std::cout << "Total sqaure feet of wrapping paper: " << wrapping_paper_sqfeet << "\n"; + std::cout << "Total feet of ribbon: " << total_ribbon << "\n"; } \ No newline at end of file