From 24753f81d55a4f057c18948e4a2d254fecb93919 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 1 Dec 2022 17:36:53 +0000 Subject: [PATCH] Add 2018 Day 2 Part 1 --- 2018/puzzle-02-01.cc | 45 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 2018/puzzle-02-01.cc diff --git a/2018/puzzle-02-01.cc b/2018/puzzle-02-01.cc new file mode 100644 index 0000000..fe0df88 --- /dev/null +++ b/2018/puzzle-02-01.cc @@ -0,0 +1,45 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2022. +// + +#include +#include +#include +#include +#include + +using Int = std::int64_t; + +auto has_pairs_triplets(std::string const& s) -> std::pair +{ + std::map counts; + for (auto c : s) { + auto it = counts.find(c); + if (it == counts.end()) { + counts.insert({c, 1}); + } + else { + ++(it->second); + } + } + + bool has_pairs{std::any_of(counts.begin(), counts.end(), [](auto x) { return x.second == 2; })}; + bool has_triplets{std::any_of(counts.begin(), counts.end(), [](auto x) { return x.second == 3; })}; + return std::make_pair(has_pairs, has_triplets); +} + +auto main() -> int +{ + Int num_pairs{0}; + Int num_triplets{0}; + std::string line; + while (std::getline(std::cin, line)) { + auto [has_pair, has_triplet] = has_pairs_triplets(line); + if (has_pair) { ++ num_pairs; } + if (has_triplet) { ++ num_triplets;} + } + std::cout << "Num pairs: " << num_pairs << '\n'; + std::cout << "Num triplets: " << num_triplets << '\n'; + std::cout << "Result: " << num_pairs * num_triplets << '\n'; + return EXIT_SUCCESS; +}