diff --git a/2022/puzzle-09-01.cc b/2022/puzzle-09-01.cc new file mode 100644 index 0000000..c2dd2ca --- /dev/null +++ b/2022/puzzle-09-01.cc @@ -0,0 +1,96 @@ +// +// Created by Matthew Gretton-Dann on 09/12/2022. +// + +#include +#include +#include +#include +#include + +using Int = std::int64_t; +using Pos = std::pair; + +struct compare_pos +{ + constexpr auto operator()(Pos const& l, Pos const& r) const noexcept + { + if (l.second < r.second) { + return true; + } + if (l.second == r.second && l.first < r.first) { + return true; + } + return false; + } +}; + +struct State +{ + State() { visited_.insert(tail_pos_); + } + + auto move(Pos dir, Int steps) + { + for (Int i{0}; i != steps; ++i) { + head_pos_.first += dir.first; + head_pos_.second += dir.second; + + if (std::abs(head_pos_.first - tail_pos_.first) > 1 || + std::abs(head_pos_.second - tail_pos_.second) > 1) { + if (tail_pos_.first < head_pos_.first) { + ++tail_pos_.first; + } + if (tail_pos_.first > head_pos_.first) { + --tail_pos_.first; + } + if (tail_pos_.second < head_pos_.second) { + ++tail_pos_.second; + } + if (tail_pos_.second > head_pos_.second) { + --tail_pos_.second; + } + visited_.insert(tail_pos_); + } + } + } + + [[nodiscard]] auto visited_count() const noexcept { return visited_.size(); } + + Pos head_pos_{0, 0}; + Pos tail_pos_{0, 0}; + std::set visited_; +}; + +auto main() -> int +{ + std::string line; + State state; + + while (std::getline(std::cin, line) && !line.empty()) { + auto item{line[0]}; + Pos dir{0, 0}; + Int const amount{std::stoll(line.substr(2))}; + switch (item) { + case 'L': + dir.first = -1; + break; + case 'R': + dir.first = 1; + break; + case 'U': + dir.second = 1; + break; + case 'D': + dir.second = -1; + break; + default: + std::cerr << "Unable to interpret: " << line << '\n'; + return EXIT_FAILURE; + } + state.move(dir, amount); + } + + std::cout << "Number of locations visited: " << state.visited_count() << '\n'; + return 0; +} diff --git a/2022/puzzle-09-02.cc b/2022/puzzle-09-02.cc new file mode 100644 index 0000000..ae90b84 --- /dev/null +++ b/2022/puzzle-09-02.cc @@ -0,0 +1,107 @@ +// +// Created by Matthew Gretton-Dann on 09/12/2022. +// + +#include +#include +#include +#include +#include +#include + +using Int = std::int64_t; +using Pos = std::pair; + +struct compare_pos +{ + constexpr auto operator()(Pos const& l, Pos const& r) const noexcept + { + if (l.second < r.second) { + return true; + } + if (l.second == r.second && l.first < r.first) { + return true; + } + return false; + } +}; + +struct State +{ + State() + { + for (auto it{rope_.begin()}; it != rope_.end(); ++it) { + *it = {0, 0}; + } + visited_.insert({0, 0}); + } + + auto move(Pos dir, Int steps) + { + for (Int i{0}; i != steps; ++i) { + auto it = rope_.begin(); + it->first += dir.first; + it->second += dir.second; + + Pos pos{*it}; + while (it != rope_.end()) { + if (std::abs(it->first - pos.first) > 1 || std::abs(it->second - pos.second) > 1) { + if (it->first < pos.first) { + ++it->first; + } + if (it->first > pos.first) { + --it->first; + } + if (it->second < pos.second) { + ++it->second; + } + if (it->second > pos.second) { + --it->second; + } + } + pos = *it; + ++it; + } + + visited_.insert(pos); + } + } + + [[nodiscard]] auto visited_count() const noexcept { return visited_.size(); } + + std::array rope_; + std::set visited_; +}; + +auto main() -> int +{ + std::string line; + State state; + + while (std::getline(std::cin, line) && !line.empty()) { + auto item{line[0]}; + Pos dir{0, 0}; + Int const amount{std::stoll(line.substr(2))}; + switch (item) { + case 'L': + dir.first = -1; + break; + case 'R': + dir.first = 1; + break; + case 'U': + dir.second = 1; + break; + case 'D': + dir.second = -1; + break; + default: + std::cerr << "Unable to interpret: " << line << '\n'; + return EXIT_FAILURE; + } + state.move(dir, amount); + } + + std::cout << "Number of locations visited: " << state.visited_count() << '\n'; + return 0; +}