Added part one of day four, plus a few 2020's from my laptop.
This commit is contained in:
2
2020/2/Makefile
Normal file
2
2020/2/Makefile
Normal file
@@ -0,0 +1,2 @@
|
||||
a.out: main.cpp
|
||||
clang++ -std=c++14 main.cpp -g -O0
|
BIN
2020/2/a.out
Executable file
BIN
2020/2/a.out
Executable file
Binary file not shown.
1000
2020/2/data
Normal file
1000
2020/2/data
Normal file
File diff suppressed because it is too large
Load Diff
72
2020/2/main.cpp
Normal file
72
2020/2/main.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
// 6-7 w: wwhmzwtwwk
|
||||
bool Valid1(const std::string &str)
|
||||
{
|
||||
int min, max;
|
||||
char letter;
|
||||
std::string password;
|
||||
|
||||
auto dash = str.find('-');
|
||||
auto space = str.find(' ');
|
||||
auto space_two = str.find(' ', space + 1);
|
||||
|
||||
min = std::atoi(str.substr(0, dash).c_str());
|
||||
max = std::atoi(str.substr(dash + 1, space).c_str());
|
||||
letter = str.at(space + 1);
|
||||
password = str.substr(space_two + 1);
|
||||
|
||||
// std::cout << "min: " << min << std::endl
|
||||
// << "max: " << max << std::endl
|
||||
// << "letter: " << letter << std::endl
|
||||
// << "pass: " << password << std::endl;
|
||||
|
||||
int total = std::count(password.begin(), password.end(), letter);
|
||||
|
||||
return (total >= min && total <= max);
|
||||
}
|
||||
|
||||
bool Valid2(const std::string &str)
|
||||
{
|
||||
int min, max;
|
||||
char letter;
|
||||
std::string password;
|
||||
|
||||
auto dash = str.find('-');
|
||||
auto space = str.find(' ');
|
||||
auto space_two = str.find(' ', space + 1);
|
||||
|
||||
min = std::atoi(str.substr(0, dash).c_str());
|
||||
max = std::atoi(str.substr(dash + 1, space).c_str());
|
||||
letter = str.at(space + 1);
|
||||
password = str.substr(space_two + 1);
|
||||
|
||||
bool a = password[min - 1] == letter;
|
||||
bool b = password[max - 1] == letter;
|
||||
|
||||
return a != b;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
std::ifstream ifs("data");
|
||||
if(!ifs.is_open())
|
||||
return -1;
|
||||
|
||||
int valid1 = 0;
|
||||
int valid2 = 0;
|
||||
for(std::string line; std::getline(ifs, line); )
|
||||
{
|
||||
if(Valid1(line))
|
||||
++valid1;
|
||||
if(Valid2(line))
|
||||
++valid2;
|
||||
}
|
||||
|
||||
std::cout << valid1 << " valid1 passwords." << std::endl;
|
||||
std::cout << valid2 << " valid2 passwords." << std::endl;
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user