Day 7 complete
This commit is contained in:
parent
6603f4a904
commit
ec73ef5cde
4 changed files with 108 additions and 0 deletions
BIN
day7/day7Part1
Executable file
BIN
day7/day7Part1
Executable file
Binary file not shown.
50
day7/day7Part1.cpp
Normal file
50
day7/day7Part1.cpp
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <cmath>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main (void)
|
||||||
|
{
|
||||||
|
int tempInt = 0;
|
||||||
|
string rawInput;
|
||||||
|
vector<int> 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;
|
||||||
|
}
|
BIN
day7/day7Part2
Executable file
BIN
day7/day7Part2
Executable file
Binary file not shown.
58
day7/day7Part2.cpp
Normal file
58
day7/day7Part2.cpp
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <cmath>
|
||||||
|
#include <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main (void)
|
||||||
|
{
|
||||||
|
int tempInt = 0;
|
||||||
|
string rawInput;
|
||||||
|
vector<int> 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;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue