Add 2016 day 6 puzzles
This commit is contained in:
39
2016/puzzle-06-01.cc
Normal file
39
2016/puzzle-06-01.cc
Normal file
@@ -0,0 +1,39 @@
|
||||
//
|
||||
// Created by Matthew Gretton-Dann on 04/12/2021.
|
||||
//
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
std::vector<std::array<unsigned, 26>> 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<char>('a' + (letter - idx.begin()));
|
||||
}
|
||||
std::cout << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
37
2016/puzzle-06-02.cc
Normal file
37
2016/puzzle-06-02.cc
Normal file
@@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by Matthew Gretton-Dann on 04/12/2021.
|
||||
//
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
auto main() -> int
|
||||
{
|
||||
std::vector<std::map<char, unsigned>> 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<char>(letter->first);
|
||||
}
|
||||
std::cout << '\n';
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user