From 5b64f9670f85fd8d2ba4257a7092c87738abcac1 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Mon, 13 Dec 2021 07:27:34 +0000 Subject: [PATCH] Add 2021 day 13 puzzles --- 2021/puzzle-13-01.cc | 70 ++++++++++++++++++++++++++++++++++ 2021/puzzle-13-02.cc | 90 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 160 insertions(+) create mode 100644 2021/puzzle-13-01.cc create mode 100644 2021/puzzle-13-02.cc diff --git a/2021/puzzle-13-01.cc b/2021/puzzle-13-01.cc new file mode 100644 index 0000000..4460cde --- /dev/null +++ b/2021/puzzle-13-01.cc @@ -0,0 +1,70 @@ +// +// Created by Matthew Gretton-Dann on 13/12/2021. +// + +#include +#include +#include +#include + +using Coord = std::pair; +using LightSet = std::set; + +auto fold(LightSet const& lights, Coord const& fold_coord) -> LightSet +{ + LightSet result; + Coord shift{0, 0}; + for (auto const& c : lights) { + Coord folded{c.first > fold_coord.first ? (2 * fold_coord.first - c.first) : c.first, + c.second > fold_coord.second ? (2 * fold_coord.second - c.second) : c.second}; + shift = {std::min(shift.first, folded.first), std::min(shift.second, folded.second)}; + result.insert(folded); + } + + if (shift == Coord{0, 0}) { + return result; + } + + LightSet result2; + for (auto const& c : result) { + result2.emplace(c.first - shift.first, c.second - shift.second); + } + return result2; +} + +auto main() -> int +{ + static std::regex fold_x{"fold along x=(\\d+)"}; + static std::regex fold_y{"fold along y=(\\d+)"}; + + std::string line; + LightSet lights; + /* Read coords. */ + while (std::getline(std::cin, line) && !line.empty()) { + std::size_t end{0}; + auto x{std::stol(line, &end)}; + auto y{std::stol(line.substr(end + 1))}; + lights.emplace(x, y); + } + + /* Now do folds. */ + int count{0}; + std::cout << "Initial lights: " << lights.size() << '\n'; + while (std::getline(std::cin, line)) { + std::smatch m; + Coord fold_coord{std::numeric_limits::max(), std::numeric_limits::max()}; + if (std::regex_search(line, m, fold_x)) { + fold_coord.first = std::stol(m.str(1)); + } + else if (std::regex_search(line, m, fold_y)) { + fold_coord.second = std::stol(m.str(1)); + } + else { + std::cerr << "Cannot interpret: " << line << "\n"; + return 1; + } + ++count; + lights = fold(lights, fold_coord); + std::cout << "After " << count << " folds: " << lights.size() << '\n'; + } +} diff --git a/2021/puzzle-13-02.cc b/2021/puzzle-13-02.cc new file mode 100644 index 0000000..9259e4a --- /dev/null +++ b/2021/puzzle-13-02.cc @@ -0,0 +1,90 @@ +// +// Created by Matthew Gretton-Dann on 13/12/2021. +// + +#include +#include +#include +#include + +using Coord = std::pair; +using LightSet = std::set; + +auto fold(LightSet const& lights, Coord const& fold_coord) -> LightSet +{ + LightSet result; + Coord shift{0, 0}; + for (auto const& c : lights) { + Coord folded{c.first > fold_coord.first ? (2 * fold_coord.first - c.first) : c.first, + c.second > fold_coord.second ? (2 * fold_coord.second - c.second) : c.second}; + shift = {std::min(shift.first, folded.first), std::min(shift.second, folded.second)}; + result.insert(folded); + } + + if (shift == Coord{0, 0}) { + return result; + } + + LightSet result2; + for (auto const& c : result) { + result2.emplace(c.first - shift.first, c.second - shift.second); + } + return result2; +} + +auto print_lights(LightSet lights) +{ + Coord size{0, 0}; + for (auto const& c : lights) { + size = {std::max(c.first + 1, size.first), std::max(c.second + 1, size.second)}; + } + + std::vector display; + for (long i{0}; i < size.second; ++i) { + display.push_back(std::string(size.first, ' ')); + } + + for (auto const& c : lights) { + display[c.second][c.first] = '*'; + } + + for (auto const& l : display) { + std::cout << l << '\n'; + } +} + +auto main() -> int +{ + static std::regex fold_x{"fold along x=(\\d+)"}; + static std::regex fold_y{"fold along y=(\\d+)"}; + + std::string line; + LightSet lights; + /* Read coords. */ + while (std::getline(std::cin, line) && !line.empty()) { + std::size_t end{0}; + auto x{std::stol(line, &end)}; + auto y{std::stol(line.substr(end + 1))}; + lights.emplace(x, y); + } + + /* Now do folds. */ + while (std::getline(std::cin, line)) { + std::smatch m; + Coord fold_coord{std::numeric_limits::max(), std::numeric_limits::max()}; + if (std::regex_search(line, m, fold_x)) { + fold_coord.first = std::stol(m.str(1)); + } + else if (std::regex_search(line, m, fold_y)) { + fold_coord.second = std::stol(m.str(1)); + } + else { + std::cerr << "Cannot interpret: " << line << "\n"; + return 1; + } + lights = fold(lights, fold_coord); + } + + print_lights(lights); + return 0; +}