diff --git a/2018/puzzle-02-02.cc b/2018/puzzle-02-02.cc new file mode 100644 index 0000000..b8496f8 --- /dev/null +++ b/2018/puzzle-02-02.cc @@ -0,0 +1,55 @@ +// +// Created by Matthew Gretton-Dann on 01/12/2022. +// + +#include +#include +#include +#include +#include + +using Int = std::int64_t; + +auto str_dist(std::string const& s1, std::string const& s2) noexcept -> Int +{ + Int result{0}; + for (std::size_t i{0}; i != s1.size(); ++i) { + if (s1[i] != s2[i]) { + ++result; + } + } + return result; +} + +auto main() -> int +{ + std::string line; + std::vector lines; + while (std::getline(std::cin, line)) { + lines.push_back(line); + } + + Int closest{std::numeric_limits::max()}; + std::string s1; + std::string s2; + for (auto it{lines.begin()}; it != lines.end(); ++it) { + for (auto it2{it + 1}; it2 != lines.end(); ++it2) { + auto distance{str_dist(*it, *it2)}; + if (distance < closest) { + closest = distance; + s1 = *it; + s2 = *it2; + } + } + } + std::string result; + result.reserve(s1.size()); + for (std::size_t i{0}; i != s1.size(); ++i) { + if (s1[i] == s2[i]) { + result += s1[i]; + } + } + + std::cout << "Result: " << result << '\n'; + return EXIT_SUCCESS; +}