From f5a1295fb67152c07dd0e3b708ff586312b53783 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gladin=C3=A9?= Date: Mon, 25 Nov 2024 06:19:39 +0100 Subject: [PATCH] task: calculate correct values --- Elves.h | 31 +++++++++++++++++++++++++++---- Santa.cpp | 23 +++++++++++++++-------- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/Elves.h b/Elves.h index 93eeeb7..96bfa23 100644 --- a/Elves.h +++ b/Elves.h @@ -7,17 +7,30 @@ class Elves { - int calculateBox(int length ,int height, int width); + static int getCubicFeetInVolume(int length, int height, int width); + static int calculateRibbonToWrap(int length, int height, int width); +public: + static int calculateBox(int length ,int height, int width); + static int calculateRibbon(int length ,int height, int width); + }; -int Elves::calculateBox(int length, int height, int width) { +inline int Elves::getCubicFeetInVolume(int length, int height, int width) { + return (length * width * height); +} + +inline int Elves::calculateRibbon(int length ,int height, int width) { + return calculateRibbonToWrap(length,width,height) + getCubicFeetInVolume(length, height, width); +} + +inline int Elves::calculateBox(int length, int height, int width) { int slack = 0; int SIDE_ONE = length * width; int SIDE_TW0 = width * height; int SIDE_THREE = height * length; - if (SIDE_THREE > SIDE_ONE && SIDE_THREE > SIDE_TW0) { + if (SIDE_THREE < SIDE_ONE && SIDE_THREE < SIDE_TW0) { slack = SIDE_THREE; - } else if (SIDE_TW0 > SIDE_ONE) { + } else if (SIDE_TW0 < SIDE_ONE) { slack = SIDE_TW0; } else { slack = SIDE_ONE; @@ -25,5 +38,15 @@ int Elves::calculateBox(int length, int height, int width) { return (2 * SIDE_ONE) + (2 *SIDE_TW0) + (2 * SIDE_THREE) + slack; } +inline int Elves::calculateRibbonToWrap(int length, int height, int width) { + if(length >= width && length >= height) { + return (width + width + height + height); + } else if(width >= height) { + return (length + length + height + height); + } else { + return (width + width + length + length); + } +} + #endif //ELVESCALCULATE_H diff --git a/Santa.cpp b/Santa.cpp index c9a41c5..9ac36e9 100644 --- a/Santa.cpp +++ b/Santa.cpp @@ -23,19 +23,26 @@ void doQuestionOne() { } void doQuestionTwo() { - Elves elves; string myText; ifstream MyReadFile("/Users/rmdy/development/AdventOfCode/params.txt"); + int squareFeetOfWrappingPaper = 0; + int squareFeetOfWrappingForRibbon = 0; while (getline(MyReadFile, myText)) { - string length = myText.substr(0, myText.find('x')); - string width = myText.substr(myText.find_first_of('x') + 1, myText.find_last_of('x') - myText.find_first_of('x') - 1); - string height = myText.substr(myText.find_last_of('x') + 1); - cout << "Length " + length << "\n"; - cout << "Width " + width << "\n"; - cout << "Height " + height << "\n"; - std::cout << myText << "\n"; + string lengthS = myText.substr(0, myText.find('x')); + string widthS = myText.substr(myText.find_first_of('x') + 1, myText.find_last_of('x') - myText.find_first_of('x') - 1); + string heightS = myText.substr(myText.find_last_of('x') + 1); + int lenght = stoi(lengthS); + int width = stoi(widthS); + int height = stoi(heightS); + squareFeetOfWrappingPaper += Elves::calculateBox(lenght,height,width); + squareFeetOfWrappingForRibbon += Elves::calculateRibbon(lenght,height,width); } MyReadFile.close(); + //cout << + squareFeetOfWrappingPaper << "\n"; + cout << + squareFeetOfWrappingForRibbon << "\n"; + cout << + Elves::calculateRibbon(2,3,4) << "\n"; + cout << + Elves::calculateRibbon(1,1,10) << "\n"; + } int main() {