diff --git a/day7/day7Part1 b/day7/day7Part1 new file mode 100755 index 0000000..f0ef98c Binary files /dev/null and b/day7/day7Part1 differ diff --git a/day7/day7Part1.cpp b/day7/day7Part1.cpp new file mode 100644 index 0000000..410f2ee --- /dev/null +++ b/day7/day7Part1.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +int main (void) +{ + int tempInt = 0; + string rawInput; + vector currentPosition; + int maxPosition = 0; + int bestPosition = 0; + int fuelCost = 0; + int bestFuelCost = 999999999; + ifstream readFile ("data/testInput.txt", ios::in); + + // CSV Handling; creates vector of ints + while(getline(readFile, rawInput, ',')) + { + tempInt = stoi(rawInput); + currentPosition.push_back(tempInt); + } + + // Find and mark max Position + maxPosition = *max_element(currentPosition.begin(), currentPosition.end()); + + // Iterate through all possible final locations + for (int testFinalPosition = 0; testFinalPosition < maxPosition; testFinalPosition++) + { + fuelCost = 0; + // Iterate through all crabs + for (int i = 0; i < currentPosition.size(); i++) + { + // Add fuel cost for each crab + fuelCost += abs( currentPosition.at(i) - testFinalPosition ); + } + + if (fuelCost < bestFuelCost) + { + bestFuelCost = fuelCost; + bestPosition = testFinalPosition; + } + } + cout << "Best Fuel cost: "<< bestFuelCost << "\t"; + cout << "Pos. of Best Fuel cost: " << bestPosition << endl; + return 0; +} diff --git a/day7/day7Part2 b/day7/day7Part2 new file mode 100755 index 0000000..abbf1d3 Binary files /dev/null and b/day7/day7Part2 differ diff --git a/day7/day7Part2.cpp b/day7/day7Part2.cpp new file mode 100644 index 0000000..b643d3f --- /dev/null +++ b/day7/day7Part2.cpp @@ -0,0 +1,58 @@ +#include +#include +#include +#include +#include +#include +using namespace std; + +int main (void) +{ + int tempInt = 0; + string rawInput; + vector currentPosition; + int maxPosition = 0; + int bestPosition = 0; + long int fuelCost = 0; + int fuelAdd = 0; + long int bestFuelCost = 9999999999999; + ifstream readFile ("data/input.txt", ios::in); + + // CSV Handling; creates vector of ints + while(getline(readFile, rawInput, ',')) + { + tempInt = stoi(rawInput); + currentPosition.push_back(tempInt); + } + + // Find and mark max Position + maxPosition = *max_element(currentPosition.begin(), currentPosition.end()); + + // Iterate through all possible final locations + for (int testFinalPosition = 0; testFinalPosition < maxPosition; testFinalPosition++) + { + fuelCost = 0; + tempInt = 0; + // Iterate through all crabs + for (int i = 0; i < currentPosition.size(); i++) + { + fuelAdd = 0; + // Add fuel cost for each crab + tempInt = abs( currentPosition.at(i) - testFinalPosition ); + for (int j = 0; j <= tempInt; j++) + { + fuelAdd += j; + } + fuelCost += fuelAdd; + } + + if (fuelCost < bestFuelCost) + { + bestFuelCost = fuelCost; + bestPosition = testFinalPosition; + } + } + cout << "Best Fuel cost: "<< bestFuelCost << "\t"; + cout << "Pos. of Best Fuel cost: " << bestPosition << endl; + return 0; +}