From 628fec2f5e5c3e9b91fcd41633f7f0c2e2537a44 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Wed, 15 Dec 2021 13:57:20 +0000 Subject: [PATCH] Add 2017 day 11 puzzles --- 2017/puzzle-11-01.cc | 60 ++++++++++++++++++++++++++++++++++++++ 2017/puzzle-11-02.cc | 69 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 2017/puzzle-11-01.cc create mode 100644 2017/puzzle-11-02.cc diff --git a/2017/puzzle-11-01.cc b/2017/puzzle-11-01.cc new file mode 100644 index 0000000..5a5c039 --- /dev/null +++ b/2017/puzzle-11-01.cc @@ -0,0 +1,60 @@ +#include +#include + +using Coord = std::pair; + +auto move(Coord const& c, std::string const& dir) -> Coord +{ + if (dir == "nw") { + return {c.first - 1, c.second + 1}; + } + if (dir == "n") { + return {c.first, c.second + 2}; + } + if (dir == "ne") { + return {c.first + 1, c.second + 1}; + } + if (dir == "se") { + return {c.first + 1, c.second - 1}; + } + if (dir == "s") { + return {c.first, c.second - 2}; + } + if (dir == "sw") { + return {c.first - 1, c.second - 1}; + } + abort(); +} + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line\n"; + return 1; + } + + std::size_t idx{0}; + Coord pos{0, 0}; + while (idx < line.size()) { + auto next{line.find(',', idx)}; + if (next == std::string::npos) { + next = line.size(); + } + pos = move(pos, line.substr(idx, next - idx)); + + idx = next + 1; + } + + std::cout << "Ended up at (" << pos.first << ", " << pos.second << ") moves away.\n"; + /* We move horizontally pos.first steps - which gets us up to pos.first steps vertically as well. + * Then we move the remaining vertical distances. */ + auto x_dist{std::abs(pos.first)}; + auto y_dist{std::abs(pos.second)}; + auto moves{x_dist}; + if (y_dist > x_dist) { + moves += (y_dist - x_dist) / 2; + } + std::cout << "Ended up " << moves << " moves away.\n"; + return 0; +} diff --git a/2017/puzzle-11-02.cc b/2017/puzzle-11-02.cc new file mode 100644 index 0000000..83b20d6 --- /dev/null +++ b/2017/puzzle-11-02.cc @@ -0,0 +1,69 @@ +#include +#include + +using Coord = std::pair; + +auto move(Coord const& c, std::string const& dir) -> Coord +{ + if (dir == "nw") { + return {c.first - 1, c.second + 1}; + } + if (dir == "n") { + return {c.first, c.second + 2}; + } + if (dir == "ne") { + return {c.first + 1, c.second + 1}; + } + if (dir == "se") { + return {c.first + 1, c.second - 1}; + } + if (dir == "s") { + return {c.first, c.second - 2}; + } + if (dir == "sw") { + return {c.first - 1, c.second - 1}; + } + abort(); +} + +auto dist(Coord const& pos) -> int +{ + /* We move horizontally pos.first steps - which gets us up to pos.first steps vertically as well. + * Then we move the remaining vertical distances. */ + auto x_dist{std::abs(pos.first)}; + auto y_dist{std::abs(pos.second)}; + auto moves{x_dist}; + if (y_dist > x_dist) { + moves += (y_dist - x_dist) / 2; + } + return moves; +} + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to read line\n"; + return 1; + } + + std::size_t idx{0}; + Coord pos{0, 0}; + int max_dist{0}; + while (idx < line.size()) { + auto next{line.find(',', idx)}; + if (next == std::string::npos) { + next = line.size(); + } + pos = move(pos, line.substr(idx, next - idx)); + if (max_dist < dist(pos)) { + max_dist = dist(pos); + } + idx = next + 1; + } + + std::cout << "Ended up at (" << pos.first << ", " << pos.second << ") moves away.\n"; + std::cout << "Ended up " << dist(pos) << " moves away.\n"; + std::cout << "Max distance away " << max_dist << '\n'; + return 0; +}