diff --git a/2022/puzzle-08-01.cc b/2022/puzzle-08-01.cc new file mode 100644 index 0000000..f1761b6 --- /dev/null +++ b/2022/puzzle-08-01.cc @@ -0,0 +1,107 @@ +// +// Created by Matthew Gretton-Dann on 08/12/2022. +// + +#include +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; +using Pos = std::complex; +using Data = std::pair; + +struct compare_pos +{ + constexpr auto operator()(Pos const& l, Pos const& r) const noexcept + { + if (l.imag() < r.imag()) { + return true; + } + if (l.imag() == r.imag() && l.real() < r.real()) { + return true; + } + return false; + } +}; + +auto main() -> int +{ + std::string line; + Pos grid_size{0, 0}; + std::map trees; + + while (std::getline(std::cin, line) && !line.empty()) { + Pos pos{0, grid_size.imag()}; + assert(grid_size.real() == 0 || grid_size.real() == line.size()); + for (auto c : line) { + trees.insert({pos, Data{static_cast(c - '0'), false}}); + pos += 1; + } + grid_size = pos + Pos{0, 1}; + } + + for (UInt j = 0; j < grid_size.imag(); ++j) { + UInt min{0}; + for (UInt i{0}; i < grid_size.real(); ++i) { + auto it{trees.find(Pos{i, j})}; + assert(it != trees.end()); + if (it->second.first >= min) { + min = it->second.first + 1; + it->second.second = true; + } + } + min = 0; + for (UInt i{grid_size.real()}; i > 0; --i) { + auto it{trees.find(Pos{i - 1, j})}; + assert(it != trees.end()); + if (it->second.first >= min) { + min = it->second.first + 1; + it->second.second = true; + } + } + } + + for (UInt i = 0; i < grid_size.real(); ++i) { + UInt min{0}; + for (UInt j{0}; j < grid_size.imag(); ++j) { + auto it{trees.find(Pos{i, j})}; + assert(it != trees.end()); + if (it->second.first >= min) { + min = it->second.first + 1; + it->second.second = true; + } + } + min = 0; + for (UInt j{grid_size.imag()}; j > 0; --j) { + auto it{trees.find(Pos{i, j - 1})}; + assert(it != trees.end()); + if (it->second.first >= min) { + min = it->second.first + 1; + it->second.second = true; + } + } + } + + auto visible_count{ + std::accumulate(trees.begin(), trees.end(), UInt{0}, + [](UInt a, auto const& node) { return node.second.second ? (a + 1) : a; })}; + std::cout << "Number visible " << visible_count << '\n'; +#if 0 + for (UInt j{0}; j != grid_size.imag(); ++j) { + for (UInt i{0}; i != grid_size.imag(); ++i) { + std::cout << trees.find({i, j})->second.first; + } + std::cout << '\n'; + } + for (UInt j{0}; j != grid_size.imag(); ++j) { + for (UInt i{0}; i != grid_size.imag(); ++i) { + std::cout << (trees.find({i, j})->second.second ? '*' : '.'); + } + std::cout << '\n'; + } +#endif + return 0; +} diff --git a/2022/puzzle-08-02.cc b/2022/puzzle-08-02.cc new file mode 100644 index 0000000..c848412 --- /dev/null +++ b/2022/puzzle-08-02.cc @@ -0,0 +1,92 @@ +// +// Created by Matthew Gretton-Dann on 08/12/2022. +// + +#include +#include +#include +#include +#include +#include + +using UInt = std::uint64_t; +using Pos = std::complex; + +struct compare_pos +{ + constexpr auto operator()(Pos const& l, Pos const& r) const noexcept + { + if (l.imag() < r.imag()) { + return true; + } + if (l.imag() == r.imag() && l.real() < r.real()) { + return true; + } + return false; + } +}; + +auto main() -> int +{ + std::string line; + Pos grid_size{0, 0}; + std::map trees; + + while (std::getline(std::cin, line) && !line.empty()) { + Pos pos{0, grid_size.imag()}; + assert(grid_size.real() == 0 || grid_size.real() == line.size()); + for (auto c : line) { + trees.insert({pos, static_cast(c - '0')}); + pos += 1; + } + grid_size = pos + Pos{0, 1}; + } + + UInt max_scenic_score{0}; + // All edges score 0. + for (UInt x{1}; x < grid_size.real() - 1; ++x) { + for (UInt y{1}; y < grid_size.imag() - 1; ++y) { + std::cout << "(" << x << ", " << y << "):"; + UInt const tree_height{trees.find({x, y})->second}; + UInt scenic_score{1}; + UInt i{x + 1}; + for (; i < grid_size.real(); ++i) { + if (trees.find({i, y})->second >= tree_height) { + break; + } + } + scenic_score *= i - x - static_cast(i == grid_size.real()); + std::cout << i - x - static_cast(i == grid_size.real()) << " * "; + + for (i = x; i > 0; --i) { + if (trees.find({i - 1, y})->second >= tree_height) { + break; + } + } + scenic_score *= x - i + static_cast(i != 0); + std::cout << x - i + static_cast(i != 0) << " * "; + + UInt j{y + 1}; + for (; j < grid_size.imag(); ++j) { + if (trees.find({x, j})->second >= tree_height) { + break; + } + } + scenic_score *= j - y - static_cast(j == grid_size.imag()); + std::cout << j - y - static_cast(j == grid_size.imag()) << " * "; + + for (j = y; j > 0; --j) { + if (trees.find({x, j - 1})->second >= tree_height) { + break; + } + } + scenic_score *= y - j + static_cast(j != 0); + std::cout << y - j + static_cast(j != 0); + + std::cout << " = " << scenic_score << '\n'; + max_scenic_score = std::max(max_scenic_score, scenic_score); + } + } + std::cout << "Mac scenic score " << max_scenic_score << '\n'; + return 0; +}