#include #include #include #include #include #include "util.h" enum Dir { R = 0, D, L, U }; 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()) { Dir dir; int steps; std::string color; std::string line; while (not std::getline(input,line).eof()) { std::stringstream in{ line }; in >> util::skip >> util::skip >> color; steps = std::stoi(color.substr(2, 5), nullptr, 16); dir = static_cast(std::stoi(color.substr(7, 1))); cmds.push_back({ dir, steps }); } } return cmds; } std::pair compute_border(Pos pos, const std::vector& cmds) { Polygon poly; long 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); long area = util::shoelace(polygon.cbegin(), polygon.cend()); std::cout << border + util::pick(area, border) << std::endl; return 0; }