aocpp/2017/19.cpp
2023-01-31 09:15:25 -08:00

80 lines
1.5 KiB
C++

#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 const [p1, p2] = Drive(Grid::Parse(in));
CHECK(p1 == "ABCDEF");
CHECK(p2 == 38);
}
}
auto Main(std::istream & in, std::ostream & out) -> void
{
auto const [part1, part2] = Drive(Grid::Parse(in));
out << "Part 1: " << part1 << std::endl;
out << "Part 2: " << part2 << std::endl;
}