aocpp/2017/19.cpp

80 lines
1.5 KiB
C++
Raw Permalink Normal View History

2022-11-26 10:01:10 -08:00
#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"
};
2023-01-31 08:58:42 -08:00
auto const [p1, p2] = Drive(Grid::Parse(in));
2022-11-26 10:01:10 -08:00
CHECK(p1 == "ABCDEF");
CHECK(p2 == 38);
}
}
2023-01-31 09:15:15 -08:00
auto Main(std::istream & in, std::ostream & out) -> void
2023-01-31 08:58:42 -08:00
{
auto const [part1, part2] = Drive(Grid::Parse(in));
2023-01-31 09:15:15 -08:00
out << "Part 1: " << part1 << std::endl;
out << "Part 2: " << part2 << std::endl;
2022-11-26 10:01:10 -08:00
}