Day 3
This commit is contained in:
parent
f9c3c61ef7
commit
26112b1d32
4 changed files with 1149 additions and 1 deletions
56
solutions/03A.cpp
Normal file
56
solutions/03A.cpp
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <bitset>
|
||||
|
||||
constexpr std::size_t bSize = 12;
|
||||
|
||||
const std::vector<std::string>& readToString(const std::string& path);
|
||||
const void getGammaEpsilon(std::bitset<bSize>& g,
|
||||
std::bitset<bSize>& e,
|
||||
const std::vector<std::string>& v);
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
std::vector<std::string> inputs = readToString("../input/03.txt");
|
||||
std::bitset<bSize> g, e;
|
||||
|
||||
getGammaEpsilon(g, e, inputs);
|
||||
|
||||
std::cout << (g.to_ulong() * e.to_ulong()) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
const std::vector<std::string>& readToString(const std::string& path)
|
||||
{
|
||||
std::ifstream inputFile(path);
|
||||
std::string input;
|
||||
static std::vector<std::string> inputs;
|
||||
|
||||
while(std::getline(inputFile, input))
|
||||
inputs.push_back(input);
|
||||
|
||||
inputFile.close();
|
||||
|
||||
return inputs;
|
||||
}
|
||||
|
||||
const void getGammaEpsilon(std::bitset<bSize>& g,
|
||||
std::bitset<bSize>& e,
|
||||
const std::vector<std::string>& v)
|
||||
{
|
||||
int bitOccurence [bSize]{};
|
||||
|
||||
for(const auto& el : v)
|
||||
for(int b = 0; b < bSize; b++)
|
||||
bitOccurence[b] += el[b] - 48;
|
||||
|
||||
for(int b = 0; b < bSize; b++)
|
||||
g.set(bSize-b-1, bitOccurence[b] > v.size()/2);
|
||||
|
||||
e = ~g;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue