Cleaned up day 1-3

This commit is contained in:
Hugo Mårdbrink 2021-12-09 00:44:10 +01:00
parent c295529953
commit ab21e8ad7d
6 changed files with 92 additions and 68 deletions

View file

@ -3,28 +3,31 @@
#include <string> #include <string>
#include <vector> #include <vector>
const std::vector<int>& readToInt(const std::string& path);
int main() int main()
{ {
std::ifstream inputFile("../input/01.txt"); std::vector<int> depths = readToInt("../input/01.txt");
std::string input;
std::vector<int> values; int increases = 0;
for(auto i = 0; i < depths.size() - 1; i++)
increases += 1 * (depths[i] < depths[i+1]);
std::cout << increases << std::endl;
return 0;
}
const std::vector<int>& readToInt(const std::string& path)
{
std::ifstream inputFile(path);
static std::vector<int> inputs;
while(std::getline(inputFile, input)) std::string input;
{ while(std::getline(inputFile, input))
values.push_back(std::stoi(input)); inputs.push_back(std::stoi(input));
}
inputFile.close();
inputFile.close();
return inputs;
int increases = 0;
for(auto i = 0; i < values.size() - 1; i++)
{
increases += 1 * (values[i] < values[i+1]);
}
std::cout << increases << std::endl;
return 0;
} }

View file

@ -2,29 +2,32 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <numeric>
const std::vector<int>& readToInt(const std::string& path);
int main() int main()
{ {
std::ifstream inputFile("../input/01.txt"); std::vector<int> depths = readToInt("../input/01.txt");
std::string input;
std::vector<int> values;
while(std::getline(inputFile, input))
{
values.push_back(std::stoi(input));
}
inputFile.close();
int increases = 0; int increases = 0;
for(auto i = 0; i < values.size() - 3; i++) for(auto i = 0; i < depths.size() - 3; i++)
{ increases += 1 * (depths[i]+depths[i+1]+depths[i+2] < depths[i+1]+depths[i+2]+depths[i+3]);
increases += 1 * (values[i]+values[i+1]+values[i+2] < values[i+1]+values[i+2]+values[i+3]);
}
std::cout << increases << std::endl; std::cout << increases << std::endl;
return 0; return 0;
} }
const std::vector<int>& readToInt(const std::string& path)
{
std::ifstream inputFile(path);
static std::vector<int> inputs;
std::string input;
while(std::getline(inputFile, input))
inputs.push_back(std::stoi(input));
inputFile.close();
return inputs;
}

View file

@ -1,19 +1,21 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector>
const std::vector<std::string>& readToString(const std::string& path);
int main() int main()
{ {
std::ifstream inputFile("../input/02.txt"); std::vector<std::string> commands = readToString("../input/02.txt");
std::string input;
long h,d = 0; long h,d = 0;
while(std::getline(inputFile, input)) for(auto& command : commands)
{ {
input.pop_back(); command.pop_back();
int val = input.back() - 48; int val = command.back() - 48;
switch(input.front()) switch(command.front())
{ {
case 'f': h += val; break; case 'f': h += val; break;
case 'u': d -= val; break; case 'u': d -= val; break;
@ -21,9 +23,19 @@ int main()
} }
} }
inputFile.close();
std::cout << d*h << std::endl; std::cout << d*h << std::endl;
return 0; 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);
return inputs;
} }

View file

@ -1,19 +1,21 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector>
const std::vector<std::string>& readToString(const std::string& path);
int main() int main()
{ {
std::ifstream inputFile("../input/02.txt"); std::vector<std::string> commands = readToString("../input/02.txt");
std::string input;
long h,d,a = 0; long h,d,a = 0;
while(std::getline(inputFile, input)) for(auto& command : commands)
{ {
input.pop_back(); command.pop_back();
int val = input.back() - 48; int val = command.back() - 48;
switch(input.front()) switch(command.front())
{ {
case 'f': d += a * val; case 'f': d += a * val;
h += val; break; h += val; break;
@ -22,9 +24,19 @@ int main()
} }
} }
inputFile.close();
std::cout << d*h << std::endl; std::cout << d*h << std::endl;
return 0; 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);
return inputs;
} }

View file

@ -13,18 +13,16 @@ const void getGammaEpsilon(std::bitset<bSize>& g,
int main() int main()
{ {
std::vector<std::string> rates = readToString("../input/03.txt");
std::vector<std::string> inputs = readToString("../input/03.txt");
std::bitset<bSize> g, e; std::bitset<bSize> g, e;
getGammaEpsilon(g, e, inputs); getGammaEpsilon(g, e, rates);
std::cout << (g.to_ulong() * e.to_ulong()) << std::endl; std::cout << (g.to_ulong() * e.to_ulong()) << std::endl;
return 0; return 0;
} }
const std::vector<std::string>& readToString(const std::string& path) const std::vector<std::string>& readToString(const std::string& path)
{ {
std::ifstream inputFile(path); std::ifstream inputFile(path);
@ -46,10 +44,10 @@ const void getGammaEpsilon(std::bitset<bSize>& g,
int bitOccurence [bSize]{}; int bitOccurence [bSize]{};
for(const auto& el : v) for(const auto& el : v)
for(int b = 0; b < bSize; b++) for(auto b = 0; b < bSize; b++)
bitOccurence[b] += el[b] - 48; bitOccurence[b] += el[b] - 48;
for(int b = 0; b < bSize; b++) for(auto b = 0; b < bSize; b++)
g.set(bSize-b-1, bitOccurence[b] > v.size()/2); g.set(bSize-b-1, bitOccurence[b] > v.size()/2);
e = ~g; e = ~g;

View file

@ -16,10 +16,10 @@ std::string filterCommonBinary(bool common, std::vector<std::string> v);
int main() int main()
{ {
std::vector<std::string> inputs = readToString("../input/03.txt"); std::vector<std::string> rates = readToString("../input/03.txt");
std::bitset<bSize> o{filterCommonBinary(true, inputs)}; std::bitset<bSize> o{filterCommonBinary(true, rates)};
std::bitset<bSize> c{filterCommonBinary(false, inputs)}; std::bitset<bSize> c{filterCommonBinary(false, rates)};
std::cout << o.to_ulong() * c.to_ulong() << std::endl; std::cout << o.to_ulong() * c.to_ulong() << std::endl;
@ -47,13 +47,12 @@ const void getGammaEpsilon(std::bitset<bSize>& g,
int bitOccurence [bSize]{}; int bitOccurence [bSize]{};
for(const auto& el : v) for(const auto& el : v)
for(int b = 0; b < bSize; b++) for(auto b = 0; b < bSize; b++)
bitOccurence[b] += el[b] - 48; bitOccurence[b] += el[b] - 48;
for(int b = 0; b < bSize; b++) for(auto b = 0; b < bSize; b++)
g.set(bSize-b-1, bitOccurence[b] >= round((float) v.size()/2)); g.set(bSize-b-1, bitOccurence[b] >= round((float) v.size()/2));
e = ~g; e = ~g;
} }
@ -61,12 +60,9 @@ const void filterCommonBit(int bit, const std::bitset<bSize>& f, std::vector<std
{ {
std::vector<std::string> nv; std::vector<std::string> nv;
for(auto i = 0; i < v.size(); i++)
for(int i = 0; i < v.size(); i++) {
if(f[bSize-bit-1] == v[i][bit] - 48) if(f[bSize-bit-1] == v[i][bit] - 48)
nv.push_back(v[i]); nv.push_back(v[i]);
}
v.clear(); v.clear();
for(const auto& e : nv) for(const auto& e : nv)
@ -77,7 +73,7 @@ std::string filterCommonBinary(bool common, std::vector<std::string> v)
{ {
std::bitset<bSize> g, e; std::bitset<bSize> g, e;
for(int b = 0; b < bSize; b++) for(auto b = 0; b < bSize; b++)
{ {
getGammaEpsilon(g, e, v); getGammaEpsilon(g, e, v);
filterCommonBit(b, common ? g : e, v); filterCommonBit(b, common ? g : e, v);