diff --git a/day3/day3Part2 b/day3/day3Part2 index b67eeac..2475692 100755 Binary files a/day3/day3Part2 and b/day3/day3Part2 differ diff --git a/day3/day3Part2.cpp b/day3/day3Part2.cpp index 5cde171..67a692e 100644 --- a/day3/day3Part2.cpp +++ b/day3/day3Part2.cpp @@ -7,6 +7,7 @@ #include using namespace std; +int byteSize = 12; int i = 0; char zeroChar = '0'; @@ -22,129 +23,170 @@ bool bitIsTrue(string binString) int main (void) { - vector inputValues; - vector mostCommonList; - vector::iterator mostCommonListIt; - vector leastCommonList; - vector::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 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 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