2022-11-24 23:49:08 -08:00
|
|
|
#include <array>
|
|
|
|
#include <bit>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <iostream>
|
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <doctest.h>
|
|
|
|
|
|
|
|
#include <aocpp/Startup.hpp>
|
|
|
|
#include <aocpp/Parsing.hpp>
|
2022-11-25 10:55:28 -08:00
|
|
|
#include <aocpp/Hex.hpp>
|
2022-11-24 23:49:08 -08:00
|
|
|
|
2022-11-25 10:55:28 -08:00
|
|
|
using namespace aocpp;
|
2022-11-24 23:49:08 -08:00
|
|
|
|
2022-11-25 10:55:28 -08:00
|
|
|
namespace {
|
2022-11-24 23:49:08 -08:00
|
|
|
|
|
|
|
auto Walk(std::string const& commands)
|
|
|
|
-> std::pair<std::int64_t, std::int64_t>
|
|
|
|
{
|
|
|
|
Hex here {};
|
|
|
|
std::int64_t part2 = 0;
|
|
|
|
for (auto const& command : aocpp::SplitOn(commands, ",")) {
|
|
|
|
here +=
|
|
|
|
"n" == command ? HEX_N :
|
|
|
|
"ne" == command ? HEX_NE :
|
|
|
|
"nw" == command ? HEX_NW :
|
|
|
|
"s" == command ? HEX_S :
|
|
|
|
"se" == command ? HEX_SE :
|
|
|
|
"sw" == command ? HEX_SW :
|
|
|
|
throw std::runtime_error{"bad input"};
|
|
|
|
part2 = std::max(part2, hex_distance(here));
|
|
|
|
}
|
|
|
|
return {hex_distance(here), part2};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
TEST_SUITE("2017-11 examples") {
|
|
|
|
TEST_CASE("example 1") {
|
|
|
|
auto [p1,p2] = Walk("ne,ne,ne");
|
|
|
|
CHECK(p1 == 3);
|
|
|
|
CHECK(p2 == 3);
|
|
|
|
}
|
|
|
|
TEST_CASE("example 2") {
|
|
|
|
auto [p1,p2] = Walk("ne,ne,sw,sw");
|
|
|
|
CHECK(p1 == 0);
|
|
|
|
CHECK(p2 == 2);
|
|
|
|
}
|
|
|
|
TEST_CASE("example 3") {
|
|
|
|
auto [p1,p2] = Walk("ne,ne,s,s");
|
|
|
|
CHECK(p1 == 2);
|
|
|
|
CHECK(p2 == 2);
|
|
|
|
}
|
|
|
|
TEST_CASE("example 4") {
|
|
|
|
auto [p1,p2] = Walk("se,sw,se,sw,sw");
|
|
|
|
CHECK(p1 == 3);
|
|
|
|
CHECK(p2 == 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto main(int argc, char** argv) -> int {
|
|
|
|
std::string line;
|
2022-11-25 10:55:28 -08:00
|
|
|
std::getline(*Startup(argc, argv), line);
|
2022-11-24 23:49:08 -08:00
|
|
|
auto [part1, part2] = Walk(line);
|
|
|
|
std::cout << "Part 1: " << part1 << std::endl;
|
|
|
|
std::cout << "Part 2: " << part2 << std::endl;
|
|
|
|
}
|