2022-08
This commit is contained in:
parent
353400fbfd
commit
b41a9d546d
80
2022/08.cpp
Normal file
80
2022/08.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <numeric>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Grid.hpp>
|
||||
#include <aocpp/Coord.hpp>
|
||||
#include <aocpp/Startup.hpp>
|
||||
|
||||
using namespace aocpp;
|
||||
|
||||
namespace {
|
||||
|
||||
using Dir = Coord(*)(Coord);
|
||||
Dir directions[4] {Up, Down, Left, Right};
|
||||
|
||||
auto Part1(Grid const& grid) -> std::int64_t
|
||||
{
|
||||
std::int64_t result {0};
|
||||
grid.each([&](Coord const c, char const v) {
|
||||
result += std::any_of(
|
||||
std::begin(directions), std::end(directions),
|
||||
[&](Dir const dir) {
|
||||
for (auto x = dir(c); grid.contains(x); x = dir(x)) {
|
||||
if (grid[x] >= v) { return false; }
|
||||
}
|
||||
return true;
|
||||
});
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
auto Part2(Grid const& grid) -> std::int64_t
|
||||
{
|
||||
std::int64_t result {0};
|
||||
grid.each([&](Coord const c, char const v) {
|
||||
auto score = std::transform_reduce(
|
||||
std::begin(directions), std::end(directions),
|
||||
std::int64_t{1},
|
||||
std::multiplies(),
|
||||
[&](Dir const dir) {
|
||||
std::int64_t count {0};
|
||||
for (auto x = dir(c); grid.contains(x); x = dir(x)) {
|
||||
count++;
|
||||
if (grid[x] >= v) break;
|
||||
}
|
||||
return count;
|
||||
});
|
||||
if (result < score) result = score;
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_SUITE("2022-08 examples") {
|
||||
TEST_CASE("example") {
|
||||
std::istringstream in {
|
||||
"30373\n"
|
||||
"25512\n"
|
||||
"65332\n"
|
||||
"33549\n"
|
||||
"35390\n"};
|
||||
auto grid = aocpp::Grid::Parse(in);
|
||||
CHECK(Part1(grid) == 21);
|
||||
CHECK(Part2(grid) == 8);
|
||||
}
|
||||
}
|
||||
|
||||
auto main(int argc, char** argv) -> int
|
||||
{
|
||||
auto grid = Grid::Parse(*aocpp::Startup(argc, argv));
|
||||
std::cout << "Part 1: " << Part1(grid) << std::endl;
|
||||
std::cout << "Part 2: " << Part2(grid) << std::endl;
|
||||
}
|
@ -7,6 +7,9 @@ target_link_libraries(2022_03 aocpp)
|
||||
add_executable(2022_04 04.cpp)
|
||||
target_link_libraries(2022_04 aocpp)
|
||||
|
||||
add_executable(2022_08 08.cpp)
|
||||
target_link_libraries(2022_08 aocpp)
|
||||
|
||||
add_executable(2022_12 12.cpp)
|
||||
target_link_libraries(2022_12 aocpp)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user