Files
advent-of-code/2020/day_2/password_philosophy.cc

55 lines
1.1 KiB
C++

#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <regex>
#include <vector>
#include <string>
struct tokenized {
int first;
int second;
char letter;
std::string password;
};
std::vector<std::string> read_input_file() {
std::ifstream file("input");
std::vector<std::string> data;
std::string line;
while(std::getline(file, line))
data.push_back(line);
return data;
}
tokenized parse_line(std::string &s) {
std::smatch match;
std::regex regex = std::regex("(.+)-(.+) (.): (.+)");
std::regex_search(s, match, regex);
return {stoi(match.str(1)), stoi(match.str(2)), match.str(3)[0], match.str(4)};
}
int main(){
auto data = read_input_file();
int result_a = 0;
int result_b = 0;
for(auto &line: data) {
auto t = parse_line(line);
int count = std::count(t.password.begin(), t.password.end(), t.letter);
if(t.first <= count && count <= t.second)
result_a++;
if((t.password[t.first - 1] == t.letter) ^ (t.password[t.second - 1] == t.letter))
result_b++;
}
std::cout << result_a << ' ' << result_b << std::endl;
return 0;
}