Primeros 2 dias de advent of code :3
This commit is contained in:
1000
2020/day_2/input
Normal file
1000
2020/day_2/input
Normal file
File diff suppressed because it is too large
Load Diff
56
2020/day_2/password_philosophy.cpp
Normal file
56
2020/day_2/password_philosophy.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#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;
|
||||
}
|
||||
23
2020/day_2/password_philosophy.py
Normal file
23
2020/day_2/password_philosophy.py
Normal file
@@ -0,0 +1,23 @@
|
||||
with open('input') as f:
|
||||
data = [line.strip().split(' ') for line in f]
|
||||
|
||||
def first_policy(first, second, letter, password):
|
||||
return first <= password.count(letter) <= second
|
||||
|
||||
def second_policy(first, second, letter, password):
|
||||
return (password[first - 1] == letter) ^ (password[second - 1] == letter)
|
||||
|
||||
def analyze(policy):
|
||||
result = 0
|
||||
for line in data:
|
||||
first, second= [int(i) for i in line[0].split('-')]
|
||||
letter = line[1].replace(':', '')
|
||||
password = line[2]
|
||||
|
||||
if policy(first, second, letter, password):
|
||||
result += 1
|
||||
|
||||
return result
|
||||
|
||||
print(analyze(first_policy))
|
||||
print(analyze(second_policy))
|
||||
Reference in New Issue
Block a user