2018-15 checkpoint
This commit is contained in:
79
2017/19.cpp
Normal file
79
2017/19.cpp
Normal file
@@ -0,0 +1,79 @@
|
||||
#include <cstdint>
|
||||
#include <cctype>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
#include <vector>
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <aocpp/Startup.hpp>
|
||||
#include <aocpp/Grid.hpp>
|
||||
#include <aocpp/Coord.hpp>
|
||||
|
||||
using namespace aocpp;
|
||||
|
||||
namespace {
|
||||
|
||||
auto Drive(Grid const& grid) -> std::pair<std::string, int>
|
||||
{
|
||||
std::string path;
|
||||
int steps = 0;
|
||||
|
||||
Coord here {};
|
||||
Coord dir {0,1};
|
||||
|
||||
// Find starting column
|
||||
for (std::size_t i = 0; i < grid.rows[0].size(); i++) {
|
||||
if (grid.rows[0][i] != ' ') {
|
||||
here.x = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
for(;;) {
|
||||
char c = grid[here];
|
||||
if (c == '+') {
|
||||
if (grid[here + CW(dir)] != ' ') {
|
||||
dir = CW(dir);
|
||||
} else if (grid[here + CCW(dir)] != ' ') {
|
||||
dir = CCW(dir);
|
||||
}
|
||||
} else if (c == ' ') {
|
||||
break;
|
||||
} else if (std::isalpha(c)) {
|
||||
path += c;
|
||||
}
|
||||
steps++;
|
||||
here += dir;
|
||||
}
|
||||
|
||||
return {path, steps};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_SUITE("2017-19 examples") {
|
||||
TEST_CASE("example") {
|
||||
std::istringstream in {
|
||||
" | \n"
|
||||
" | +--+ \n"
|
||||
" A | C \n"
|
||||
" F---|--|-E---+ \n"
|
||||
" | | | D \n"
|
||||
" +B-+ +--+ \n"
|
||||
" \n"
|
||||
};
|
||||
auto [p1,p2] = Drive(Grid::Parse(in));
|
||||
CHECK(p1 == "ABCDEF");
|
||||
CHECK(p2 == 38);
|
||||
}
|
||||
}
|
||||
|
||||
auto main(int argc, char** argv) -> int {
|
||||
auto input = Grid::Parse(*aocpp::Startup(argc, argv));
|
||||
auto [part1, part2] = Drive(input);
|
||||
std::cout << "Part 1: " << part1 << std::endl;
|
||||
std::cout << "Part 2: " << part2 << std::endl;
|
||||
}
|
@@ -24,3 +24,6 @@ target_link_libraries(2017_17 aocpp)
|
||||
|
||||
add_executable(2017_18 18.cpp)
|
||||
target_link_libraries(2017_18 aocpp)
|
||||
|
||||
add_executable(2017_19 19.cpp)
|
||||
target_link_libraries(2017_19 aocpp)
|
||||
|
Reference in New Issue
Block a user