#include #include #include #include #include #include #include "util.h" using Cmd = std::tuple; using Pos = std::pair; using Polygon = std::vector; std::vector parse(const char* file) { std::vector cmds; if (std::ifstream input{ file }; input.is_open()) { char dir; int steps; std::string line; while (not std::getline(input,line).eof()) { std::stringstream in{ line }; in >> dir >> steps; cmds.push_back({ dir, steps }); } } return cmds; } std::pair compute_border(Pos pos, const std::vector& cmds) { Polygon poly; int border{}; auto [x, y] = pos; for(const auto& cmd : cmds) { auto [dir, steps] = cmd; switch (dir) { case 'R': y += steps; break; case 'D': x += steps; break; case 'L': y -= steps; break; case 'U': x -= steps; } poly.push_back({ x, y }); border += steps; } return { border, std::move(poly) }; } int main(int argc, char* argv[]) { auto cmds = parse(argv[1]); auto [border, polygon] = compute_border({ 0, 0 }, cmds); int area = util::shoelace(polygon.cbegin(), polygon.cend()); std::cout << border + util::pick(area, border) << std::endl; return 0; }