chore: clean code up

This commit is contained in:
Rekai Nyangadzayi Musuka 2020-12-02 03:17:14 -06:00
parent abf4233189
commit b2492ec7a6
1 changed files with 14 additions and 21 deletions

View File

@ -2,49 +2,44 @@
#include <fstream> #include <fstream>
void part1(std::ifstream& input, std::string& line) { void part1(std::ifstream& input, std::string& line) {
int num_valid = 0; int valid_count = 0;
while (std::getline(input, line)) { while (std::getline(input, line)) {
size_t fp_i = line.find(' '); // Index of the first space in line size_t fp_i = line.find(' '); // Index of the first space in line
size_t dash_i = line.find('-'); // Index of the only dash in line size_t dash_i = line.find('-'); // Index of the only dash in line
size_t ss_i = line.substr(fp_i + 1).find(' ') + fp_i + 1; // Index of the second space in line std::string password = line.substr(line.find(": ") + 2);
std::string range = line.substr(0, fp_i); // "9-11": the range that we're looking for letter to be in
std::string password = line.substr(ss_i + 1);
char letter = line[fp_i + 1]; char letter = line[fp_i + 1];
int low = std::stoi(range.substr(0, dash_i)); int low = std::stoi(line.substr(0, dash_i));
int high = std::stoi(range.substr(dash_i + 1)); int high = std::stoi(line.substr(dash_i + 1, fp_i));
int letter_count = std::count(password.cbegin(), password.cend(), letter); int letter_count = std::count(password.cbegin(), password.cend(), letter);
if (letter_count <= high && letter_count >= low) num_valid++; if (letter_count <= high && letter_count >= low) valid_count++;
} }
std::cout << num_valid << std::endl; std::cout << valid_count << std::endl;
} }
void part2(std::ifstream& input, std::string& line) { void part2(std::ifstream& input, std::string& line) {
int num_valid = 0; int valid_count = 0;
while(std::getline(input, line)) { while(std::getline(input, line)) {
size_t fp_i = line.find(' '); // Index of the first space in line size_t fp_i = line.find(' '); // Index of the first space in line
size_t dash_i = line.find('-'); // Index of the only dash in line size_t dash_i = line.find('-'); // Index of the only dash in line
size_t ss_index = line.substr(fp_i + 1).find(' ') + fp_i + 1; // Index of the second space in line std::string password = line.substr(line.find(": ") + 2);
std::string range = line.substr(0, fp_i); // "9-11": We will check indexes 9 and 11
std::string password = line.substr(ss_index + 1);
char letter = line[fp_i + 1]; char letter = line[fp_i + 1];
size_t i1 = std::stoi(range.substr(0, dash_i)) - 1; size_t i1 = std::stoi(line.substr(0, dash_i)) - 1;
size_t i2 = std::stoi(range.substr(dash_i + 1)) - 1; size_t i2 = std::stoi(line.substr(dash_i + 1, fp_i)) - 1;
if (password[i1] == password[i2]) continue; // If they are the same we can skip this line if (password[i1] == password[i2]) continue; // If they are the same we can skip this line
if (password[i1] == letter || password[i2] == letter) num_valid++; if (password[i1] == letter || password[i2] == letter) valid_count++;
} }
std::cout << num_valid << std::endl; std::cout << valid_count << std::endl;
} }
int main() { int main() {
std::ifstream input("../input.txt"); std::ifstream input("../input.txt");
std::string line; std::string line;
@ -58,5 +53,3 @@ int main() {
return 0; return 0;
} }