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>
|
||||
using namespace std;
|
||||
|
||||
int byteSize = 12;
|
||||
int i = 0;
|
||||
char zeroChar = '0';
|
||||
|
||||
|
@ -22,129 +23,170 @@ bool bitIsTrue(string binString)
|
|||
|
||||
int main (void)
|
||||
{
|
||||
vector<string> inputValues;
|
||||
vector<string> mostCommonList;
|
||||
vector<string>::iterator mostCommonListIt;
|
||||
vector<string> leastCommonList;
|
||||
vector<string>::iterator leastCommonListIt;
|
||||
string inputValues;
|
||||
string mostCommonList;
|
||||
string mostCommonWorking;
|
||||
string leastCommonList;
|
||||
string leastCommonWorking;
|
||||
string currentLine;
|
||||
string mostCommonFinal;
|
||||
string leastCommonFinal;
|
||||
int mostBitTrue[5] = {};
|
||||
int mostBitFalse[5] = {};
|
||||
int leastBitTrue[5] = {};
|
||||
int leastBitFalse[5] = {};
|
||||
int oxygenGenRatingBin[5] = {};
|
||||
int coScrubRatingBin[5] = {};
|
||||
int oxygenGenRatingDec = 0;
|
||||
int coScrubRatingDec = 0;
|
||||
int lifeSupport = 0;
|
||||
ifstream readFile ("data/testInput.txt", ios::in);
|
||||
ifstream readFile ("data/input.txt", ios::in);
|
||||
|
||||
//file Read
|
||||
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;
|
||||
mostCommonWorking = mostCommonList;
|
||||
leastCommonList = inputValues;
|
||||
|
||||
leastCommonWorking = leastCommonList;
|
||||
|
||||
//most Common filter
|
||||
//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
|
||||
if (mostCommonList.size() == 1) break;
|
||||
//exit loop if list only has one byte
|
||||
if (mostCommonList.size() == byteSize) break;
|
||||
|
||||
//iterate through list
|
||||
for (int j=0;j<mostCommonList.size();j++)
|
||||
//iterate through string
|
||||
for (int j=0;j<(mostCommonList.length() / byteSize);j++)
|
||||
{
|
||||
//increment counter
|
||||
cout << mostCommonList[j][i];
|
||||
if (mostCommonList[j][i] == '0') mostBitFalse[i]++;
|
||||
else if (mostCommonList[j][i] == '1') mostBitTrue[i]++;
|
||||
if (mostCommonList[j*byteSize + i] == '0') mostBitFalse[i]++;
|
||||
else if (mostCommonList[j*byteSize + i] == '1') mostBitTrue[i]++;
|
||||
else
|
||||
{
|
||||
cout << "Error!" << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
//Favor 1s over 0s
|
||||
if(mostBitTrue[i] == mostBitFalse[i]) mostBitTrue[i]++;
|
||||
|
||||
//if same, true has priority
|
||||
if (mostBitTrue[i] == mostBitFalse[i]) mostBitTrue[i]++;
|
||||
//If more 1s than 0s
|
||||
if (mostBitTrue[i] > mostBitFalse[i])
|
||||
{
|
||||
//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])
|
||||
{
|
||||
//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;
|
||||
|
||||
if (mostBitTrue[i] > mostBitFalse[i])
|
||||
//testing block
|
||||
for (int j=0;j<(mostCommonList.length() / byteSize);j++)
|
||||
{
|
||||
mostCommonListIt = remove_if(mostCommonList.begin(), mostCommonList.end(), bitIsFalse);
|
||||
}
|
||||
if (mostBitFalse[i] > mostBitTrue[i])
|
||||
{
|
||||
mostCommonListIt = remove_if(mostCommonList.begin(), mostCommonList.end(), bitIsTrue);
|
||||
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
|
||||
//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
|
||||
if (leastCommonList.size() == 1) break;
|
||||
//exit loop if list only has one byte
|
||||
if (leastCommonList.size() == byteSize) break;
|
||||
|
||||
//iterate through list
|
||||
for (auto it = leastCommonList.begin(); it != leastCommonList.end(); ++it)
|
||||
//iterate through string
|
||||
for (int j=0;j<(leastCommonList.length() / byteSize);j++)
|
||||
{
|
||||
//make index of list usable
|
||||
currentLine = *it;
|
||||
|
||||
//increment counter
|
||||
if (currentLine[i] == 0) leastBitFalse[i]++;
|
||||
else if (currentLine[i] == 1) leastBitTrue[i]++;
|
||||
if (leastCommonList[j*byteSize + i] == '0') leastBitFalse[i]++;
|
||||
else if (leastCommonList[j*byteSize + i] == '1') leastBitTrue[i]++;
|
||||
else
|
||||
{
|
||||
cout << "Error!" << endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
//Favor 1s over 0s
|
||||
if(leastBitTrue[i] == leastBitFalse[i]) leastBitTrue[i]++;
|
||||
|
||||
//if same, true has priority
|
||||
if (mostBitTrue[i] == mostBitFalse[i]) mostBitTrue[i]++;
|
||||
//If more 1s than 0s
|
||||
if (leastBitTrue[i] > leastBitFalse[i])
|
||||
{
|
||||
//iterate through string
|
||||
for (int j=(leastCommonList.length() / byteSize);j>=0;j--)
|
||||
{
|
||||
//remove byte with indexed bit of 0
|
||||
if (leastCommonList[j*byteSize + i] == '1') leastCommonWorking.erase(j*byteSize, byteSize);
|
||||
}
|
||||
}
|
||||
//If more 0s than 1s
|
||||
if (leastBitFalse[i] > leastBitTrue[i])
|
||||
{
|
||||
//iterate through string
|
||||
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;
|
||||
|
||||
if (mostBitTrue[i] > mostBitFalse[i])
|
||||
//testing block
|
||||
for (int j=0;j<(leastCommonList.length() / byteSize);j++)
|
||||
{
|
||||
mostCommonListIt = remove_if(mostCommonList.begin(), mostCommonList.end(), bitIsFalse);
|
||||
}
|
||||
if (mostBitFalse[i] > mostBitTrue[i])
|
||||
{
|
||||
mostCommonListIt = remove_if(mostCommonList.begin(), mostCommonList.end(), bitIsTrue);
|
||||
cout << leastCommonList[j*byteSize] << leastCommonList[j*byteSize + 1] << leastCommonList[j*byteSize + 2] << leastCommonList[j*byteSize + 3] << leastCommonList[j*byteSize + 4] << endl;
|
||||
}
|
||||
cout << endl << "******************************" << endl;
|
||||
|
||||
}
|
||||
leastCommonFinal = *leastCommonList.begin();
|
||||
|
||||
for(i=0;i<5;i++)
|
||||
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;
|
||||
if(mostCommonFinal[i] == '1') oxygenGenRatingBin[i]=1;
|
||||
else if(mostCommonFinal[i] == '0') oxygenGenRatingBin[i]=0;
|
||||
else
|
||||
{
|
||||
cout << "Error!!!" << endl;
|
||||
return 3;
|
||||
}
|
||||
if(leastCommonFinal[i] == 1) coScrubRatingBin[i]=1;
|
||||
else if(leastCommonFinal[i] == 0) coScrubRatingBin[i]=0;
|
||||
if(leastCommonFinal[i] == '1') coScrubRatingBin[i]=1;
|
||||
else if(leastCommonFinal[i] == '0') coScrubRatingBin[i]=0;
|
||||
else
|
||||
{
|
||||
cout << "Error!!!!" << endl;
|
||||
return 4;
|
||||
}
|
||||
}
|
||||
for(int i=0;i<5;i++)
|
||||
for(int i=0;i<byteSize;i++)
|
||||
{
|
||||
oxygenGenRatingDec += oxygenGenRatingBin[i] * pow(2,04-i);
|
||||
coScrubRatingDec += coScrubRatingBin[i] * pow(2,04-i);
|
||||
oxygenGenRatingDec += oxygenGenRatingBin[i] * pow(2,byteSize-1-i);
|
||||
coScrubRatingDec += coScrubRatingBin[i] * pow(2,byteSize-1-i);
|
||||
}
|
||||
lifeSupport = oxygenGenRatingDec * coScrubRatingDec;
|
||||
cout << "\nLife Support Rating: " << lifeSupport;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue