This commit is contained in:
Hugo Mårdbrink 2021-12-09 00:27:26 +01:00
parent f9c3c61ef7
commit 26112b1d32
4 changed files with 1149 additions and 1 deletions

View file

@ -12,3 +12,5 @@ add_executable(01A "solutions/01A.cpp")
add_executable(01B "solutions/01B.cpp")
add_executable(02A "solutions/02A.cpp")
add_executable(02B "solutions/02B.cpp")
add_executable(03A "solutions/03A.cpp")
add_executable(03B "solutions/03B.cpp")

1000
input/03.txt Normal file

File diff suppressed because it is too large Load diff

56
solutions/03A.cpp Normal file
View 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;
}

90
solutions/03B.cpp Normal file
View file

@ -0,0 +1,90 @@
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <bitset>
#include <cmath>
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);
const void filterCommonBit(int bit, const std::bitset<bSize>& f, std::vector<std::string>& v);
std::string filterCommonBinary(bool common, std::vector<std::string> v);
int main()
{
std::vector<std::string> inputs = readToString("../input/03.txt");
std::bitset<bSize> o{filterCommonBinary(true, inputs)};
std::bitset<bSize> c{filterCommonBinary(false, inputs)};
std::cout << o.to_ulong() * c.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] >= round((float) v.size()/2));
e = ~g;
}
const void filterCommonBit(int bit, const std::bitset<bSize>& f, std::vector<std::string>& v)
{
std::vector<std::string> nv;
for(int i = 0; i < v.size(); i++) {
if(f[bSize-bit-1] == v[i][bit] - 48)
nv.push_back(v[i]);
}
v.clear();
for(const auto& e : nv)
v.push_back(e);
}
std::string filterCommonBinary(bool common, std::vector<std::string> v)
{
std::bitset<bSize> g, e;
for(int b = 0; b < bSize; b++)
{
getGammaEpsilon(g, e, v);
filterCommonBit(b, common ? g : e, v);
if(v.size() == 1)
return v[0];
}
return "";
}