From ef3676f9903c1de677b9f0069b3e10793314b513 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Sat, 4 Dec 2021 11:33:25 +0000 Subject: [PATCH] Add 2016 day 6 puzzles --- 2016/puzzle-06-01.cc | 39 +++++++++++++++++++++++++++++++++++++++ 2016/puzzle-06-02.cc | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 2016/puzzle-06-01.cc create mode 100644 2016/puzzle-06-02.cc diff --git a/2016/puzzle-06-01.cc b/2016/puzzle-06-01.cc new file mode 100644 index 0000000..e4e76f3 --- /dev/null +++ b/2016/puzzle-06-01.cc @@ -0,0 +1,39 @@ +// +// Created by Matthew Gretton-Dann on 04/12/2021. +// + +#include +#include +#include +#include +#include + +auto main() -> int +{ + std::vector> counts; + std::string line; + + while (std::getline(std::cin, line)) { + if (counts.empty()) { + counts.resize(line.size()); + for (unsigned i = 0; i < line.size(); ++i) { + for (unsigned j = 0; j < 26; ++j) { + counts[i][j] = 0; + } + } + } + + assert(counts.size() == line.size()); + for (std::size_t i = 0; i < line.size(); ++i) { + ++counts[i][line.at(i) - 'a']; + } + } + + for (auto const& idx : counts) { + auto letter{std::max_element(idx.begin(), idx.end())}; // NOLINT(llvm-qualified-auto) + std::cout << static_cast('a' + (letter - idx.begin())); + } + std::cout << '\n'; + + return 0; +} \ No newline at end of file diff --git a/2016/puzzle-06-02.cc b/2016/puzzle-06-02.cc new file mode 100644 index 0000000..30293d0 --- /dev/null +++ b/2016/puzzle-06-02.cc @@ -0,0 +1,37 @@ +// +// Created by Matthew Gretton-Dann on 04/12/2021. +// + +#include +#include +#include +#include +#include + +auto main() -> int +{ + std::vector> counts; + std::string line; + + while (std::getline(std::cin, line)) { + if (counts.empty()) { + counts.resize(line.size()); + } + + assert(counts.size() == line.size()); + for (std::size_t i = 0; i < line.size(); ++i) { + auto [it, success] = counts[i].insert({line[i], 0}); + ++(it->second); + } + } + + for (auto const& idx : counts) { + auto letter{std::min_element(idx.begin(), idx.end(), [](auto const& lhs, auto const& rhs) { + return lhs.second < rhs.second; + })}; + std::cout << static_cast(letter->first); + } + std::cout << '\n'; + + return 0; +} \ No newline at end of file