Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bitset>
- #include <vector>
- static void Day3A_()
- {
- std::ifstream infile("3.txt");
- std::bitset<12> currBitset;
- std::vector<int> binaryCount(12);
- while (infile >> currBitset)
- {
- for (unsigned i = 0; i < currBitset.size(); ++i)
- {
- currBitset.test(i) ? binaryCount[i]++ : binaryCount[i]--;
- }
- }
- std::bitset<12> gamma;
- for (unsigned i = 0; i < binaryCount.size(); ++i)
- {
- gamma.set(i, binaryCount[i] GT 0);
- }
- auto epsilon = ~gamma;
- std::cout << "3A:" << gamma.to_ulong() * epsilon.to_ulong() << "\n";
- }
- using Bitset = std::bitset<12>;
- bool MajoritySet_(int index, const std::vector<Bitset>& list)
- {
- int count = 0;
- for (const auto& currBitset : list)
- {
- currBitset.test(index) ? count++ : count--;
- }
- return count GE 0;
- }
- #include <functional>
- using ComparisonFunc = std::function<bool(bool, bool)>;
- int FilteredValue_(std::vector<Bitset> list, //copied list since it is modified. HOWEVER, could use std::partition instead of erase_if
- const ComparisonFunc& func)
- {
- for (unsigned index = 11; list.size() GT 1 AND index GE 0; --index)
- {
- bool isSet = MajoritySet_(index, list);
- std::erase_if(list, [&](const Bitset& bit) {return func(bit.test(index), isSet); });
- }
- _ASSERT(list.size() EQ 1);
- return list.front().to_ulong();
- }
- static void Day3B_()
- {
- /*
- In string form:
- 100000101101
- ^ ^
- 11th 0th exponent
- In bitset,
- 101101000001
- ^ ^
- 0th 11th exponent
- */
- std::vector<Bitset> list;
- std::ifstream infile("3.txt");
- Bitset currBitset;
- while (infile >> currBitset)
- {
- list.push_back(currBitset);
- }
- auto first = FilteredValue_(list, [](const bool b1, const bool b2) {return b1 NE b2; }); //Erase things NOT matching majority
- auto second = FilteredValue_(list, [](const bool b1, const bool b2) {return b1 EQ b2; }); //Erase things matching majority.
- std::cout << "3B:" << first * second << "\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement