Actualize un poco las soluciones en c++ por aprender cositas
This commit is contained in:
@@ -12,7 +12,6 @@ std::set<int> read_input_file() {
|
|||||||
while(std::getline(file, line))
|
while(std::getline(file, line))
|
||||||
data.insert(std::stoi(line));
|
data.insert(std::stoi(line));
|
||||||
|
|
||||||
file.close();
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,7 +21,6 @@ int main(){
|
|||||||
|
|
||||||
for(auto &i: data) {
|
for(auto &i: data) {
|
||||||
int rest = 2020 - i;
|
int rest = 2020 - i;
|
||||||
|
|
||||||
for(auto &j: data) {
|
for(auto &j: data) {
|
||||||
int second_rest = rest - j;
|
int second_rest = rest - j;
|
||||||
if (data.count(second_rest)) {
|
if (data.count(second_rest)) {
|
||||||
54
2020/day_2/password_philosophy.cc
Normal file
54
2020/day_2/password_philosophy.cc
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
#include <algorithm>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <fstream>
|
|
||||||
#include <vector>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::string> split(const std::string &s, char delimiter) {
|
|
||||||
std::vector<std::string> tokens;
|
|
||||||
std::string token;
|
|
||||||
std::istringstream token_stream(s);
|
|
||||||
|
|
||||||
while(std::getline(token_stream, token, delimiter))
|
|
||||||
tokens.push_back(token);
|
|
||||||
|
|
||||||
return tokens;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(){
|
|
||||||
auto data = read_input_file();
|
|
||||||
|
|
||||||
int result_a = 0;
|
|
||||||
int result_b = 0;
|
|
||||||
|
|
||||||
for(auto &line: data) {
|
|
||||||
auto main_split = split(line, ' ');
|
|
||||||
auto number_split = split(main_split[0], '-');
|
|
||||||
|
|
||||||
int first = stoi(number_split[0]);
|
|
||||||
int second = stoi(number_split[1]);
|
|
||||||
char letter = main_split[1][0];
|
|
||||||
std::string password = main_split[2];
|
|
||||||
|
|
||||||
int count = std::count(password.begin(), password.end(), letter);
|
|
||||||
|
|
||||||
if(first <= count && count <= second)
|
|
||||||
result_a++;
|
|
||||||
if((password[first - 1] == letter) ^ (password[second - 1] == letter))
|
|
||||||
result_b++;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << result_a << ' ' << result_b << std::endl;
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user