From e00d9e4a0018cae7b27be0b340bd76672a07c447 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Fri, 3 Dec 2021 14:33:30 +0000 Subject: [PATCH] Add 2016 Day 3 puzzles --- 2016/puzzle-03-01.cc | 48 ++++++++++++++++++++++++++++++++ 2016/puzzle-03-02.cc | 66 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+) create mode 100644 2016/puzzle-03-01.cc create mode 100644 2016/puzzle-03-02.cc diff --git a/2016/puzzle-03-01.cc b/2016/puzzle-03-01.cc new file mode 100644 index 0000000..a69921e --- /dev/null +++ b/2016/puzzle-03-01.cc @@ -0,0 +1,48 @@ +// +// Created by Matthew Gretton-Dann on 03/12/2021. +// + +#include +#include +#include +#include + +struct Triangle +{ + explicit Triangle(std::string const& s) + { + std::size_t idx{0}; + for (unsigned i = 0; i < 3; ++i) { + while (idx < s.size() && std::isspace(s[idx]) != 0) { + ++idx; + } + assert(idx < s.size()); + std::size_t end{0}; + lengths_[i] = std::stoul(s.substr(idx), &end); + idx += end; + } + } + + [[nodiscard]] auto is_valid() const -> bool + { + return (lengths_[0] + lengths_[1]) > lengths_[2] && (lengths_[0] + lengths_[2]) > lengths_[1] && + (lengths_[1] + lengths_[2]) > lengths_[0]; + } + +private: + std::array lengths_; +}; + +auto main() -> int +{ + unsigned valid{0}; + std::string line; + while (std::getline(std::cin, line)) { + Triangle triangle{line}; + if (triangle.is_valid()) { + ++valid; + } + } + std::cout << "Number of valid triangles: " << valid << '\n'; + return 0; +} \ No newline at end of file diff --git a/2016/puzzle-03-02.cc b/2016/puzzle-03-02.cc new file mode 100644 index 0000000..ee66b07 --- /dev/null +++ b/2016/puzzle-03-02.cc @@ -0,0 +1,66 @@ +// +// Created by Matthew Gretton-Dann on 03/12/2021. +// + +#include +#include +#include +#include + +struct Triangle +{ + Triangle() = default; + Triangle(std::uint64_t l0, std::uint64_t l1, std::uint64_t l2) : lengths_({l0, l1, l2}) {} + + explicit Triangle(std::string const& s) + { + std::size_t idx{0}; + for (unsigned i = 0; i < 3; ++i) { + while (idx < s.size() && std::isspace(s[idx]) != 0) { + ++idx; + } + assert(idx < s.size()); + std::size_t end{0}; + lengths_.at(i) = std::stoul(s.substr(idx), &end); + idx += end; + } + } + + [[nodiscard]] auto is_valid() const -> bool + { + return (lengths_[0] + lengths_[1]) > lengths_[2] && (lengths_[0] + lengths_[2]) > lengths_[1] && + (lengths_[1] + lengths_[2]) > lengths_[0]; + } + + [[nodiscard]] auto length(unsigned side) const -> std::uint64_t { return lengths_.at(side); } + void length(unsigned side, std::uint64_t length) { lengths_.at(side) = length; } + +private: + std::array lengths_{0, 0, 0}; +}; + +auto main() -> int +{ + unsigned valid{0}; + std::string line; + while (true) { + std::array triangles; + for (unsigned i = 0; i < 3; ++i) { + if (!std::getline(std::cin, line)) { + std::cout << "Number of valid triangles: " << valid << '\n'; + return 0; + } + triangles.at(i) = Triangle{line}; + } + std::array transposed{ + Triangle{triangles[0].length(0), triangles[1].length(0), triangles[2].length(0)}, + Triangle{triangles[0].length(1), triangles[1].length(1), triangles[2].length(1)}, + Triangle{triangles[0].length(2), triangles[1].length(2), triangles[2].length(2)}, + }; + for (unsigned i = 0; i < 3; ++i) { + if (transposed.at(i).is_valid()) { + ++valid; + } + } + } +} \ No newline at end of file