Limpie un poco el codigo
This commit is contained in:
134
2020/day_4/passport_processing.cc
Normal file
134
2020/day_4/passport_processing.cc
Normal file
@@ -0,0 +1,134 @@
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <vector>
|
||||
#include <unordered_map>
|
||||
|
||||
#include <regex>
|
||||
|
||||
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) {
|
||||
std::vector<std::string> tokens;
|
||||
std::string token;
|
||||
std::istringstream token_stream(s);
|
||||
|
||||
while(std::getline(token_stream, token, ' ')) tokens.push_back(token);
|
||||
|
||||
return tokens;
|
||||
}
|
||||
|
||||
typedef std::unordered_map<std::string, std::string> passport;
|
||||
|
||||
std::vector<passport> parse_passports(const std::vector<std::string> &data) {
|
||||
std::vector<passport> passports;
|
||||
passport current;
|
||||
|
||||
for(auto &line : data) {
|
||||
if(line != ""){
|
||||
for(auto &field : split(line)) {
|
||||
current[field.substr(0,3)] = field.substr(4, field.size());
|
||||
}
|
||||
} else {
|
||||
passports.push_back(current);
|
||||
current.clear();
|
||||
}
|
||||
}
|
||||
|
||||
passports.push_back(current);
|
||||
|
||||
return passports;
|
||||
}
|
||||
|
||||
|
||||
bool has_fields(const passport &passport) {
|
||||
return passport.find("byr") != passport.end() && passport.find("iyr") != passport.end() &&
|
||||
passport.find("eyr") != passport.end() && passport.find("hgt") != passport.end() &&
|
||||
passport.find("hcl") != passport.end() && passport.find("ecl") != passport.end() &&
|
||||
passport.find("pid") != passport.end();
|
||||
|
||||
}
|
||||
|
||||
bool validate_birth(const passport &passport) {
|
||||
return stoi(passport.at("byr")) >= 1920 && stoi(passport.at("byr")) <= 2002;
|
||||
}
|
||||
|
||||
bool validate_issue(const passport &passport) {
|
||||
return stoi(passport.at("iyr")) >= 2010 && stoi(passport.at("iyr")) <= 2020;
|
||||
}
|
||||
|
||||
bool validate_expire(const passport &passport) {
|
||||
return stoi(passport.at("eyr")) >= 2020 && stoi(passport.at("eyr")) <= 2030;
|
||||
}
|
||||
|
||||
bool validate_eye(const passport &passport) {
|
||||
std::string color = passport.at("ecl");
|
||||
return color == "amb" || color == "blu" || color == "brn" || color == "gry" ||
|
||||
color == "grn" || color == "hzl" || color == "oth";
|
||||
}
|
||||
|
||||
bool validate_hair(const passport &passport) {
|
||||
return std::regex_match(passport.at("hcl"), std::regex("^#[0-9a-f]{6}$"));
|
||||
}
|
||||
|
||||
bool validate_pid(const passport &passport) {
|
||||
return std::regex_match(passport.at("pid"), std::regex("^[0-9]{9}$"));
|
||||
}
|
||||
|
||||
bool validate_height(const passport &passport) {
|
||||
std::string hgt = passport.at("hgt");
|
||||
std::string unit = hgt.substr(hgt.size() - 2, 2);
|
||||
int height = stoi(hgt);
|
||||
|
||||
if (unit == "cm" && height >= 150 && height <= 193) return true;
|
||||
else if(unit == "in" && height >= 59 && height <= 76) return true;
|
||||
else return false;
|
||||
}
|
||||
|
||||
bool full_validation(const passport &passport) {
|
||||
return has_fields(passport) && validate_birth(passport) && validate_issue(passport) &&
|
||||
validate_expire(passport) && validate_hair(passport) && validate_pid(passport) &&
|
||||
validate_eye(passport) && validate_height(passport);
|
||||
}
|
||||
|
||||
void print_passport(const passport &passport) {
|
||||
std::cout << "---------------------------" << std::endl;
|
||||
for(auto &pair: passport) {
|
||||
std::cout << "[" << pair.first << "] = " << pair.second << std::endl;
|
||||
}
|
||||
std::cout << "---------------------------" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int solve_a(const std::vector<passport> &passports) {
|
||||
int count = 0;
|
||||
for(auto &passport: passports) if(has_fields(passport)) count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
int solve_b(const std::vector<passport> &passports) {
|
||||
int count = 0;
|
||||
for(auto &passport: passports) {
|
||||
if(full_validation(passport)) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
int main() {
|
||||
auto data = read_input_file();
|
||||
auto passports = parse_passports(data);
|
||||
|
||||
std::cout << solve_a(passports) << ' ' << solve_b(passports) << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -58,6 +58,7 @@ def solve_b(passports):
|
||||
return len([passport for passport in passports if full_validation(passport)])
|
||||
|
||||
passports = parse_passports(data)
|
||||
print(len(passports))
|
||||
|
||||
print(solve_a(passports))
|
||||
print(solve_b(passports))
|
||||
|
||||
@@ -8,23 +8,15 @@ def binary_partition(bsp, r):
|
||||
|
||||
half = (r[1] + r[0]) // 2
|
||||
|
||||
if bsp[0] in ['F', 'L']: r = (r[0], half)
|
||||
elif bsp[0] in ['B', 'R']: r = (half + 1, r[1])
|
||||
if bsp[0] in ['F', 'L']: return binary_partition(bsp[1:], (r[0], half))
|
||||
elif bsp[0] in ['B', 'R']: return binary_partition(bsp[1:], (half + 1, r[1]))
|
||||
|
||||
return binary_partition(bsp[1:], r)
|
||||
|
||||
def from_binary(bsp):
|
||||
"""
|
||||
Apparently doing a binary partition as the
|
||||
problem describes is the same as reading
|
||||
the instructions in binary
|
||||
"""
|
||||
#Apparently doing a binary partition as the problem describes is the same as reading
|
||||
#the instructions in binary, i think is in someway equivalent to the traversal of a binary tree
|
||||
|
||||
number = []
|
||||
for l in bsp:
|
||||
if l in ['F', 'L']: number.append('0')
|
||||
elif l in ['B', 'R']: number.append('1')
|
||||
return int("".join(number), 2)
|
||||
return int(bsp.replace('F', '0').replace('L', '0').replace('B', '1').replace('R', '1'), 2)
|
||||
|
||||
def get_id(row, col): return row * 8 + col
|
||||
|
||||
|
||||
Reference in New Issue
Block a user