I gave up on vectors... but it works now!
This commit is contained in:
parent
f17c7848be
commit
d6d77b269f
2 changed files with 102 additions and 60 deletions
BIN
day3/day3Part2
BIN
day3/day3Part2
Binary file not shown.
|
@ -7,6 +7,7 @@
|
||||||
#include <array>
|
#include <array>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
int byteSize = 12;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
char zeroChar = '0';
|
char zeroChar = '0';
|
||||||
|
|
||||||
|
@ -22,129 +23,170 @@ bool bitIsTrue(string binString)
|
||||||
|
|
||||||
int main (void)
|
int main (void)
|
||||||
{
|
{
|
||||||
vector<string> inputValues;
|
string inputValues;
|
||||||
vector<string> mostCommonList;
|
string mostCommonList;
|
||||||
vector<string>::iterator mostCommonListIt;
|
string mostCommonWorking;
|
||||||
vector<string> leastCommonList;
|
string leastCommonList;
|
||||||
vector<string>::iterator leastCommonListIt;
|
string leastCommonWorking;
|
||||||
string currentLine;
|
string currentLine;
|
||||||
string mostCommonFinal;
|
string mostCommonFinal;
|
||||||
string leastCommonFinal;
|
string leastCommonFinal;
|
||||||
int mostBitTrue[5] = {};
|
|
||||||
int mostBitFalse[5] = {};
|
|
||||||
int leastBitTrue[5] = {};
|
|
||||||
int leastBitFalse[5] = {};
|
|
||||||
int oxygenGenRatingBin[5] = {};
|
|
||||||
int coScrubRatingBin[5] = {};
|
|
||||||
int oxygenGenRatingDec = 0;
|
int oxygenGenRatingDec = 0;
|
||||||
int coScrubRatingDec = 0;
|
int coScrubRatingDec = 0;
|
||||||
int lifeSupport = 0;
|
int lifeSupport = 0;
|
||||||
ifstream readFile ("data/testInput.txt", ios::in);
|
ifstream readFile ("data/input.txt", ios::in);
|
||||||
|
|
||||||
//file Read
|
//file Read
|
||||||
while(getline(readFile, currentLine))
|
while(getline(readFile, currentLine))
|
||||||
{
|
{
|
||||||
inputValues.emplace_back(currentLine);
|
byteSize = currentLine.length();
|
||||||
|
inputValues.append(currentLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int mostBitTrue[byteSize] = {};
|
||||||
|
int mostBitFalse[byteSize] = {};
|
||||||
|
int leastBitTrue[byteSize] = {};
|
||||||
|
int leastBitFalse[byteSize] = {};
|
||||||
|
int oxygenGenRatingBin[byteSize] = {};
|
||||||
|
int coScrubRatingBin[byteSize] = {};
|
||||||
mostCommonList = inputValues;
|
mostCommonList = inputValues;
|
||||||
|
mostCommonWorking = mostCommonList;
|
||||||
leastCommonList = inputValues;
|
leastCommonList = inputValues;
|
||||||
|
leastCommonWorking = leastCommonList;
|
||||||
|
|
||||||
//most Common filter
|
//most Common filter
|
||||||
//Iterate through all bits in line
|
//Iterate through all bits in line
|
||||||
for(i=0;i<5;i++)
|
for(i=0;i<byteSize;i++)
|
||||||
{
|
{
|
||||||
//exit loop if list only has one string
|
//exit loop if list only has one byte
|
||||||
if (mostCommonList.size() == 1) break;
|
if (mostCommonList.size() == byteSize) break;
|
||||||
|
|
||||||
//iterate through list
|
//iterate through string
|
||||||
for (int j=0;j<mostCommonList.size();j++)
|
for (int j=0;j<(mostCommonList.length() / byteSize);j++)
|
||||||
{
|
{
|
||||||
//increment counter
|
//increment counter
|
||||||
cout << mostCommonList[j][i];
|
if (mostCommonList[j*byteSize + i] == '0') mostBitFalse[i]++;
|
||||||
if (mostCommonList[j][i] == '0') mostBitFalse[i]++;
|
else if (mostCommonList[j*byteSize + i] == '1') mostBitTrue[i]++;
|
||||||
else if (mostCommonList[j][i] == '1') mostBitTrue[i]++;
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout << "Error!" << endl;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Favor 1s over 0s
|
||||||
|
if(mostBitTrue[i] == mostBitFalse[i]) mostBitTrue[i]++;
|
||||||
|
|
||||||
//if same, true has priority
|
//If more 1s than 0s
|
||||||
if (mostBitTrue[i] == mostBitFalse[i]) mostBitTrue[i]++;
|
|
||||||
|
|
||||||
if (mostBitTrue[i] > mostBitFalse[i])
|
if (mostBitTrue[i] > mostBitFalse[i])
|
||||||
{
|
{
|
||||||
mostCommonListIt = remove_if(mostCommonList.begin(), mostCommonList.end(), bitIsFalse);
|
//iterate through string
|
||||||
|
for (int j=(mostCommonList.length() / byteSize);j>=0;j--)
|
||||||
|
{
|
||||||
|
//remove byte with indexed bit of 0
|
||||||
|
if (mostCommonList[j*byteSize + i] == '0') mostCommonWorking.erase(j*byteSize, byteSize);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
//If more 0s than 1s
|
||||||
if (mostBitFalse[i] > mostBitTrue[i])
|
if (mostBitFalse[i] > mostBitTrue[i])
|
||||||
{
|
{
|
||||||
mostCommonListIt = remove_if(mostCommonList.begin(), mostCommonList.end(), bitIsTrue);
|
//iterate through string
|
||||||
|
for (int j=(mostCommonList.length() / byteSize);j>=0;j--)
|
||||||
|
{
|
||||||
|
// remove byte with indexed bit of 1
|
||||||
|
if (mostCommonList[j*byteSize + i] == '1') mostCommonWorking.erase(j*byteSize, byteSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
mostCommonList = mostCommonWorking;
|
||||||
|
|
||||||
|
//testing block
|
||||||
|
for (int j=0;j<(mostCommonList.length() / byteSize);j++)
|
||||||
|
{
|
||||||
|
cout << mostCommonList[j*byteSize] << mostCommonList[j*byteSize + 1] << mostCommonList[j*byteSize + 2] << mostCommonList[j*byteSize + 3] << mostCommonList[j*byteSize + 4] << endl;
|
||||||
|
}
|
||||||
|
cout << endl << "******************************" << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
mostCommonFinal = *mostCommonList.begin();
|
cout << "Final Most Common: ";
|
||||||
|
mostCommonFinal = mostCommonList;
|
||||||
|
cout << mostCommonFinal << endl;
|
||||||
//*******************************************************************
|
//*******************************************************************
|
||||||
//least Common filter
|
//least Common filter
|
||||||
//Iterate through all bits in line
|
//Iterate through all bits in line
|
||||||
for(i=0;i<5;i++)
|
for(i=0;i<byteSize;i++)
|
||||||
{
|
{
|
||||||
//exit loop if list only has one string
|
//exit loop if list only has one byte
|
||||||
if (leastCommonList.size() == 1) break;
|
if (leastCommonList.size() == byteSize) break;
|
||||||
|
|
||||||
//iterate through list
|
//iterate through string
|
||||||
for (auto it = leastCommonList.begin(); it != leastCommonList.end(); ++it)
|
for (int j=0;j<(leastCommonList.length() / byteSize);j++)
|
||||||
{
|
{
|
||||||
//make index of list usable
|
|
||||||
currentLine = *it;
|
|
||||||
|
|
||||||
//increment counter
|
//increment counter
|
||||||
if (currentLine[i] == 0) leastBitFalse[i]++;
|
if (leastCommonList[j*byteSize + i] == '0') leastBitFalse[i]++;
|
||||||
else if (currentLine[i] == 1) leastBitTrue[i]++;
|
else if (leastCommonList[j*byteSize + i] == '1') leastBitTrue[i]++;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout << "Error!" << endl;
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//Favor 1s over 0s
|
||||||
|
if(leastBitTrue[i] == leastBitFalse[i]) leastBitTrue[i]++;
|
||||||
|
|
||||||
//if same, true has priority
|
//If more 1s than 0s
|
||||||
if (mostBitTrue[i] == mostBitFalse[i]) mostBitTrue[i]++;
|
if (leastBitTrue[i] > leastBitFalse[i])
|
||||||
|
|
||||||
if (mostBitTrue[i] > mostBitFalse[i])
|
|
||||||
{
|
{
|
||||||
mostCommonListIt = remove_if(mostCommonList.begin(), mostCommonList.end(), bitIsFalse);
|
//iterate through string
|
||||||
}
|
for (int j=(leastCommonList.length() / byteSize);j>=0;j--)
|
||||||
if (mostBitFalse[i] > mostBitTrue[i])
|
|
||||||
{
|
{
|
||||||
mostCommonListIt = remove_if(mostCommonList.begin(), mostCommonList.end(), bitIsTrue);
|
//remove byte with indexed bit of 0
|
||||||
|
if (leastCommonList[j*byteSize + i] == '1') leastCommonWorking.erase(j*byteSize, byteSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
leastCommonFinal = *leastCommonList.begin();
|
//If more 0s than 1s
|
||||||
|
if (leastBitFalse[i] > leastBitTrue[i])
|
||||||
for(i=0;i<5;i++)
|
|
||||||
{
|
{
|
||||||
if(mostCommonFinal[i] == 1) oxygenGenRatingBin[i]=1;
|
//iterate through string
|
||||||
else if(mostCommonFinal[i] == 0) oxygenGenRatingBin[i]=0;
|
for (int j=(leastCommonList.length() / byteSize);j>=0;j--)
|
||||||
|
{
|
||||||
|
// remove byte with indexed bit of 1
|
||||||
|
if (leastCommonList[j*byteSize + i] == '0') leastCommonWorking.erase(j*byteSize, byteSize);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
leastCommonList = leastCommonWorking;
|
||||||
|
|
||||||
|
//testing block
|
||||||
|
for (int j=0;j<(leastCommonList.length() / byteSize);j++)
|
||||||
|
{
|
||||||
|
cout << leastCommonList[j*byteSize] << leastCommonList[j*byteSize + 1] << leastCommonList[j*byteSize + 2] << leastCommonList[j*byteSize + 3] << leastCommonList[j*byteSize + 4] << endl;
|
||||||
|
}
|
||||||
|
cout << endl << "******************************" << endl;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Final least Common: ";
|
||||||
|
leastCommonFinal = leastCommonList;
|
||||||
|
cout << leastCommonFinal << endl;
|
||||||
|
|
||||||
|
for(i=0;i<byteSize;i++)
|
||||||
|
{
|
||||||
|
if(mostCommonFinal[i] == '1') oxygenGenRatingBin[i]=1;
|
||||||
|
else if(mostCommonFinal[i] == '0') oxygenGenRatingBin[i]=0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout << "Error!!!" << endl;
|
cout << "Error!!!" << endl;
|
||||||
return 3;
|
return 3;
|
||||||
}
|
}
|
||||||
if(leastCommonFinal[i] == 1) coScrubRatingBin[i]=1;
|
if(leastCommonFinal[i] == '1') coScrubRatingBin[i]=1;
|
||||||
else if(leastCommonFinal[i] == 0) coScrubRatingBin[i]=0;
|
else if(leastCommonFinal[i] == '0') coScrubRatingBin[i]=0;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
cout << "Error!!!!" << endl;
|
cout << "Error!!!!" << endl;
|
||||||
return 4;
|
return 4;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for(int i=0;i<5;i++)
|
for(int i=0;i<byteSize;i++)
|
||||||
{
|
{
|
||||||
oxygenGenRatingDec += oxygenGenRatingBin[i] * pow(2,04-i);
|
oxygenGenRatingDec += oxygenGenRatingBin[i] * pow(2,byteSize-1-i);
|
||||||
coScrubRatingDec += coScrubRatingBin[i] * pow(2,04-i);
|
coScrubRatingDec += coScrubRatingBin[i] * pow(2,byteSize-1-i);
|
||||||
}
|
}
|
||||||
lifeSupport = oxygenGenRatingDec * coScrubRatingDec;
|
lifeSupport = oxygenGenRatingDec * coScrubRatingDec;
|
||||||
cout << "\nLife Support Rating: " << lifeSupport;
|
cout << "\nLife Support Rating: " << lifeSupport;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue