2020-12-06 05:26:20 +00:00
|
|
|
#include <folly/String.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <execution>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <numeric>
|
|
|
|
#include <set>
|
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-12-06 06:26:47 +00:00
|
|
|
std::vector<std::string> readInput(const std::string &filename) {
|
2020-12-06 05:26:20 +00:00
|
|
|
std::vector<std::string> input;
|
|
|
|
std::string line;
|
|
|
|
std::ifstream file(filename);
|
|
|
|
while (std::getline(file, line)) {
|
|
|
|
input.push_back(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return input;
|
|
|
|
}
|
|
|
|
|
2020-12-06 06:26:47 +00:00
|
|
|
/**
|
|
|
|
* Separate the inputs into groups - each inner vector is a list of lines within that group (separated by newlines)
|
|
|
|
* @param input The puzzle input
|
|
|
|
* @return std::vector<std::vector<std::string>> The inputs grouped by newlines
|
|
|
|
*/
|
|
|
|
std::vector<std::vector<std::string>> getGroups(const std::vector<std::string> &input) {
|
|
|
|
std::vector<std::vector<std::string>> groups;
|
|
|
|
std::vector<std::string> currentGroup;
|
|
|
|
for (std::string line : input) {
|
2020-12-06 05:26:20 +00:00
|
|
|
if (line.length() == 0) {
|
2020-12-06 06:26:47 +00:00
|
|
|
groups.push_back(currentGroup);
|
|
|
|
currentGroup.clear();
|
2020-12-06 05:26:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-06 06:26:47 +00:00
|
|
|
currentGroup.push_back(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
groups.push_back(currentGroup);
|
|
|
|
return groups;
|
|
|
|
}
|
|
|
|
|
|
|
|
int part1(const std::vector<std::vector<std::string>> &groups) {
|
|
|
|
int total = 0;
|
|
|
|
for (const std::vector<std::string> &group : groups) {
|
|
|
|
std::set<char> groupAnswers;
|
|
|
|
for (const std::string &personAnswers : group) {
|
|
|
|
std::for_each(personAnswers.cbegin(), personAnswers.cend(), [&groupAnswers](char answer) {
|
|
|
|
groupAnswers.insert(answer);
|
|
|
|
});
|
2020-12-06 05:26:20 +00:00
|
|
|
}
|
2020-12-06 06:26:47 +00:00
|
|
|
total += groupAnswers.size();
|
2020-12-06 05:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2020-12-06 06:26:47 +00:00
|
|
|
int part2(const std::vector<std::vector<std::string>> &groups) {
|
2020-12-06 05:26:20 +00:00
|
|
|
int total = 0;
|
2020-12-06 06:26:47 +00:00
|
|
|
// Make a set of all possible answers, from a to z.
|
|
|
|
std::set<char> allPossibleAnswers;
|
2020-12-06 05:26:20 +00:00
|
|
|
for (char i = 'a'; i <= 'z'; i++) {
|
2020-12-06 06:26:47 +00:00
|
|
|
allPossibleAnswers.insert(i);
|
2020-12-06 05:26:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 06:26:47 +00:00
|
|
|
for (const std::vector<std::string> &group : groups) {
|
|
|
|
// Not technically needed for the problem but it would be incorrect if this happened :) total would add 26.
|
|
|
|
if (group.size() == 0) {
|
2020-12-06 05:26:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-12-06 06:26:47 +00:00
|
|
|
// Copy all possible answers so we can produce a set intersection with them
|
|
|
|
std::set<char> commonAnswers(allPossibleAnswers);
|
|
|
|
for (const std::string &rawPersonAnswers : group) {
|
2020-12-06 06:29:20 +00:00
|
|
|
// Get a set of this person's answers
|
2020-12-06 06:26:47 +00:00
|
|
|
std::set<char> personAnswers;
|
|
|
|
std::for_each(rawPersonAnswers.cbegin(), rawPersonAnswers.cend(), [&personAnswers](char answer) {
|
|
|
|
personAnswers.insert(answer);
|
|
|
|
});
|
|
|
|
|
2020-12-06 06:29:20 +00:00
|
|
|
// Intersect it with all of the common answers
|
2020-12-06 06:26:47 +00:00
|
|
|
std::set<char> intersection;
|
|
|
|
std::set_intersection(
|
|
|
|
personAnswers.begin(),
|
|
|
|
personAnswers.end(),
|
|
|
|
commonAnswers.begin(),
|
|
|
|
commonAnswers.end(),
|
|
|
|
std::inserter(intersection, intersection.begin()));
|
2020-12-06 05:26:20 +00:00
|
|
|
|
2020-12-06 06:26:47 +00:00
|
|
|
commonAnswers = std::move(intersection);
|
2020-12-06 05:26:20 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 06:26:47 +00:00
|
|
|
total += commonAnswers.size();
|
2020-12-06 05:26:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
if (argc != 2) {
|
|
|
|
std::cerr << argv[0] << " <input_file>" << std::endl;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-12-06 06:26:47 +00:00
|
|
|
auto input = readInput(argv[1]);
|
|
|
|
auto groups = getGroups(input);
|
|
|
|
std::cout << part1(groups) << std::endl;
|
|
|
|
std::cout << part2(groups) << std::endl;
|
2020-12-06 05:26:20 +00:00
|
|
|
}
|