diff --git a/2017/puzzle-13-01.cc b/2017/puzzle-13-01.cc new file mode 100644 index 0000000..2295c11 --- /dev/null +++ b/2017/puzzle-13-01.cc @@ -0,0 +1,59 @@ +#include +#include +#include +#include + +using UInt = unsigned long; + +struct Layer +{ + explicit Layer(UInt depth) : depth_(depth) {} + + void advance() + { + pos_ += dir_; + if (pos_ == 0 || pos_ == depth_ - 1) { + dir_ = -dir_; + } + } + + [[nodiscard]] auto pos() const noexcept -> UInt { return pos_; } + [[nodiscard]] auto depth() const noexcept -> UInt { return depth_; } + +private: + UInt pos_{0}; + UInt depth_; + UInt dir_{1}; +}; + +using LayerMap = std::map; + +auto main() -> int +{ + static std::regex line_re{"(\\d+): (\\d+)"}; + std::string line; + LayerMap layers; + + while (std::getline(std::cin, line) && !line.empty()) { + std::smatch m; + if (!std::regex_search(line, m, line_re)) { + std::cerr << "Unable to match: " << line << '\n'; + return 1; + } + layers.insert({std::stoul(m.str(1)), Layer{std::stoul(m.str(2))}}); + } + + auto severity{0}; + for (auto& layer : layers) { + for (UInt time{0}; time != layer.first; ++time) { + layer.second.advance(); + } + if (layer.second.pos() == 0) { + severity += layer.first * layer.second.depth(); + } + } + + std::cout << "Severity: " << severity << '\n'; + + return 0; +} diff --git a/2017/puzzle-13-02.cc b/2017/puzzle-13-02.cc new file mode 100644 index 0000000..2bf7ce9 --- /dev/null +++ b/2017/puzzle-13-02.cc @@ -0,0 +1,63 @@ +#include +#include +#include +#include +#include + +using UInt = unsigned long; + +struct Layer +{ + explicit Layer(UInt depth) : depth_(depth) {} + + [[nodiscard]] auto pos_at(UInt time) const noexcept -> UInt + { + time %= 2 * (depth_ - 1); + if (time < depth_ - 1) { + return time; + } + else { + return 2 * (depth_ - 1) - time; + } + } + + [[nodiscard]] auto depth() const noexcept -> UInt { return depth_; } + +private: + UInt depth_; +}; + +using LayerMap = std::map; + +auto main() -> int +{ + static std::regex line_re{"(\\d+): (\\d+)"}; + std::string line; + LayerMap layers; + + while (std::getline(std::cin, line) && !line.empty()) { + std::smatch m; + if (!std::regex_search(line, m, line_re)) { + std::cerr << "Unable to match: " << line << '\n'; + return 1; + } + layers.insert({std::stoul(m.str(1)), Layer{std::stoul(m.str(2))}}); + } + + UInt offset{0}; + UInt cont{true}; + while (cont) { + cont = false; + for (auto& layer : layers) { + if (layer.second.pos_at(layer.first + offset) == 0) { + ++offset; + cont = true; + break; + } + } + } + + std::cout << "Offset: " << offset << '\n'; + + return 0; +}