From 020f3b7c69fd06ac242b9bd32741aed3026b90e4 Mon Sep 17 00:00:00 2001 From: Shanti Chellaram Date: Sat, 23 Oct 2021 15:34:37 -0400 Subject: [PATCH] Solve problem 6 --- c++/problem6.cpp | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 c++/problem6.cpp diff --git a/c++/problem6.cpp b/c++/problem6.cpp new file mode 100644 index 0000000..265ce18 --- /dev/null +++ b/c++/problem6.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include +#include "solutions.hpp" + +using namespace std; + +template <> +pair solve<6>(istream& input) { + uint64_t part1 = 0, part2 = 0; + unordered_set any_answered; + unordered_set all_answered; + bool new_group = true; + for (string line; getline(input, line); ) { + if (!line.empty()) { + unordered_set present; + for (char c : line) { + any_answered.insert(c); + if (new_group || all_answered.contains(c)) { + present.insert(c); + } + } + all_answered = present; + new_group = false; + } + if (line.empty() || input.peek() == EOF) { + part1 += any_answered.size(); + part2 += all_answered.size(); + any_answered.clear(); + all_answered.clear(); + new_group = true; + } + } + return pair(to_string(part1), to_string(part2)); +} +