From 0c56620ba63b4e9f89ba364cc796a8215f13fee6 Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Tue, 7 Dec 2021 11:33:32 +0000 Subject: [PATCH] Add 2016 day 18 puzzles --- 2016/puzzle-18-01.cc | 41 +++++++++++++++++++++++++++++++++++++++++ 2016/puzzle-18-02.cc | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 2016/puzzle-18-01.cc create mode 100644 2016/puzzle-18-02.cc diff --git a/2016/puzzle-18-01.cc b/2016/puzzle-18-01.cc new file mode 100644 index 0000000..318ad88 --- /dev/null +++ b/2016/puzzle-18-01.cc @@ -0,0 +1,41 @@ +// +// Created by Matthew Gretton-Dann on 07/12/2021. +// + +#include +#include +#include + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to get line.\n"; + return 1; + } + + line = '.' + line + '.'; + unsigned safe_count{0}; + constexpr unsigned row_count{40}; + for (unsigned row{0}; row < row_count; ++row) { + safe_count = std::accumulate(line.begin(), line.end(), safe_count, + [](unsigned a, char c) { return a + (c == '.' ? 1 : 0); }) - + 2; + std::string next_line{"."}; + next_line.reserve(line.size()); + for (unsigned i = 1; i < line.size() - 1; ++i) { + auto check{line.substr(i - 1, 3)}; + if (check == "^^." || check == ".^^" || check == "^.." || check == "..^") { + next_line += '^'; + } + else { + next_line += '.'; + } + } + next_line += '.'; + line = next_line; + } + + std::cout << "Number of safe spots: " << safe_count << '\n'; + return 0; +} \ No newline at end of file diff --git a/2016/puzzle-18-02.cc b/2016/puzzle-18-02.cc new file mode 100644 index 0000000..32b2f18 --- /dev/null +++ b/2016/puzzle-18-02.cc @@ -0,0 +1,41 @@ +// +// Created by Matthew Gretton-Dann on 07/12/2021. +// + +#include +#include +#include + +auto main() -> int +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Unable to get line.\n"; + return 1; + } + + line = '.' + line + '.'; + unsigned safe_count{0}; + constexpr unsigned row_count{400000}; + for (unsigned row{0}; row < row_count; ++row) { + safe_count = std::accumulate(line.begin(), line.end(), safe_count, + [](unsigned a, char c) { return a + (c == '.' ? 1 : 0); }) - + 2; + std::string next_line{"."}; + next_line.reserve(line.size()); + for (unsigned i = 1; i < line.size() - 1; ++i) { + auto check{line.substr(i - 1, 3)}; + if (check == "^^." || check == ".^^" || check == "^.." || check == "..^") { + next_line += '^'; + } + else { + next_line += '.'; + } + } + next_line += '.'; + line = next_line; + } + + std::cout << "Number of safe spots: " << safe_count << '\n'; + return 0; +} \ No newline at end of file