diff --git a/solutions/01A.cpp b/solutions/01A.cpp index 2fa7724..57d29c5 100644 --- a/solutions/01A.cpp +++ b/solutions/01A.cpp @@ -3,28 +3,31 @@ #include #include +const std::vector& readToInt(const std::string& path); int main() { -std::ifstream inputFile("../input/01.txt"); -std::string input; -std::vector values; + std::vector depths = readToInt("../input/01.txt"); + + 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& readToInt(const std::string& path) +{ + std::ifstream inputFile(path); + static std::vector inputs; -while(std::getline(inputFile, input)) -{ - values.push_back(std::stoi(input)); -} - -inputFile.close(); - - -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; + std::string input; + while(std::getline(inputFile, input)) + inputs.push_back(std::stoi(input)); + + inputFile.close(); + + return inputs; } diff --git a/solutions/01B.cpp b/solutions/01B.cpp index e465d4e..328ee8c 100644 --- a/solutions/01B.cpp +++ b/solutions/01B.cpp @@ -2,29 +2,32 @@ #include #include #include -#include + +const std::vector& readToInt(const std::string& path); int main() { - std::ifstream inputFile("../input/01.txt"); - std::string input; - std::vector values; + std::vector depths = readToInt("../input/01.txt"); - while(std::getline(inputFile, input)) - { - values.push_back(std::stoi(input)); - } - - inputFile.close(); - int increases = 0; - for(auto i = 0; i < values.size() - 3; i++) - { - increases += 1 * (values[i]+values[i+1]+values[i+2] < values[i+1]+values[i+2]+values[i+3]); - } + 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]); std::cout << increases << std::endl; return 0; } +const std::vector& readToInt(const std::string& path) +{ + std::ifstream inputFile(path); + static std::vector inputs; + + std::string input; + while(std::getline(inputFile, input)) + inputs.push_back(std::stoi(input)); + + inputFile.close(); + + return inputs; +} diff --git a/solutions/02A.cpp b/solutions/02A.cpp index 80cf3cd..6b459b4 100644 --- a/solutions/02A.cpp +++ b/solutions/02A.cpp @@ -1,19 +1,21 @@ #include #include #include +#include + +const std::vector& readToString(const std::string& path); int main() { - std::ifstream inputFile("../input/02.txt"); - std::string input; + std::vector commands = readToString("../input/02.txt"); long h,d = 0; - while(std::getline(inputFile, input)) + for(auto& command : commands) { - input.pop_back(); - int val = input.back() - 48; + command.pop_back(); + int val = command.back() - 48; - switch(input.front()) + switch(command.front()) { case 'f': h += val; break; case 'u': d -= val; break; @@ -21,9 +23,19 @@ int main() } } - inputFile.close(); - std::cout << d*h << std::endl; return 0; +} + +const std::vector& readToString(const std::string& path) +{ + std::ifstream inputFile(path); + std::string input; + static std::vector inputs; + + while(std::getline(inputFile, input)) + inputs.push_back(input); + + return inputs; } \ No newline at end of file diff --git a/solutions/02B.cpp b/solutions/02B.cpp index 8ab341f..7e1d53f 100644 --- a/solutions/02B.cpp +++ b/solutions/02B.cpp @@ -1,19 +1,21 @@ #include #include #include +#include + +const std::vector& readToString(const std::string& path); int main() { - std::ifstream inputFile("../input/02.txt"); - std::string input; + std::vector commands = readToString("../input/02.txt"); long h,d,a = 0; - while(std::getline(inputFile, input)) + for(auto& command : commands) { - input.pop_back(); - int val = input.back() - 48; + command.pop_back(); + int val = command.back() - 48; - switch(input.front()) + switch(command.front()) { case 'f': d += a * val; h += val; break; @@ -22,9 +24,19 @@ int main() } } - inputFile.close(); - std::cout << d*h << std::endl; return 0; +} + +const std::vector& readToString(const std::string& path) +{ + std::ifstream inputFile(path); + std::string input; + static std::vector inputs; + + while(std::getline(inputFile, input)) + inputs.push_back(input); + + return inputs; } \ No newline at end of file diff --git a/solutions/03A.cpp b/solutions/03A.cpp index 4ca5e29..7484b2c 100644 --- a/solutions/03A.cpp +++ b/solutions/03A.cpp @@ -13,18 +13,16 @@ const void getGammaEpsilon(std::bitset& g, int main() { - - std::vector inputs = readToString("../input/03.txt"); + std::vector rates = readToString("../input/03.txt"); std::bitset g, e; - getGammaEpsilon(g, e, inputs); + getGammaEpsilon(g, e, rates); std::cout << (g.to_ulong() * e.to_ulong()) << std::endl; return 0; } - const std::vector& readToString(const std::string& path) { std::ifstream inputFile(path); @@ -46,10 +44,10 @@ const void getGammaEpsilon(std::bitset& g, int bitOccurence [bSize]{}; for(const auto& el : v) - for(int b = 0; b < bSize; b++) + for(auto b = 0; b < bSize; b++) 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); e = ~g; diff --git a/solutions/03B.cpp b/solutions/03B.cpp index 8eae4d7..f68dce9 100644 --- a/solutions/03B.cpp +++ b/solutions/03B.cpp @@ -16,10 +16,10 @@ std::string filterCommonBinary(bool common, std::vector v); int main() { - std::vector inputs = readToString("../input/03.txt"); + std::vector rates = readToString("../input/03.txt"); - std::bitset o{filterCommonBinary(true, inputs)}; - std::bitset c{filterCommonBinary(false, inputs)}; + std::bitset o{filterCommonBinary(true, rates)}; + std::bitset c{filterCommonBinary(false, rates)}; std::cout << o.to_ulong() * c.to_ulong() << std::endl; @@ -47,13 +47,12 @@ const void getGammaEpsilon(std::bitset& g, int bitOccurence [bSize]{}; for(const auto& el : v) - for(int b = 0; b < bSize; b++) + for(auto b = 0; b < bSize; b++) 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)); - e = ~g; } @@ -61,12 +60,9 @@ const void filterCommonBit(int bit, const std::bitset& f, std::vector nv; - - for(int i = 0; i < v.size(); i++) { + for(auto 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) @@ -77,7 +73,7 @@ std::string filterCommonBinary(bool common, std::vector v) { std::bitset g, e; - for(int b = 0; b < bSize; b++) + for(auto b = 0; b < bSize; b++) { getGammaEpsilon(g, e, v); filterCommonBit(b, common ? g : e, v);