You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

40 lines
1.2 KiB

#include <sstream>
#include <cstdint>
#include <stdexcept>
#include "solutions.hpp"
using namespace std;
template <>
pair<string, string> solve<2>(istream& input) {
uint64_t part1 = 0, part2 = 0;
for (string line; getline(input, line); ) {
stringstream line_stream(line);
uint64_t min_count, max_count;
char target;
string password;
line_stream >> min_count;
if (line_stream.get() != (int) '-') {
throw domain_error("input incorrect");
}
line_stream >> max_count;
if (line_stream.get() != (int) ' ') {
throw domain_error("input incorrect");
}
line_stream >> target;
if (line_stream.get() != (int) ':') {
throw domain_error("input incorrect");
}
if (line_stream.get() != (int) ' ') {
throw domain_error("input incorrect");
}
line_stream >> password;
size_t count = 0;
for (char c : password) {
count+=(int)(c == target);
}
part1 += (int)(count >= min_count && count <= max_count);
part2 += (int)((password[min_count - 1] == target) != (password[max_count - 1] == target));
}
return pair(to_string(part1), to_string(part2));
}