Skip to content

Instantly share code, notes, and snippets.

@HappyCerberus
Created December 22, 2021 12:30
std::istream &operator>>(std::istream &s, CuboidInstruction &cube) {
std::string line;
if (!getline(s, line))
return s;
CuboidInstruction result;
if (line.starts_with("on")) {
result.on = true;
} else if (line.starts_with("off")) {
result.on = false;
} else {
throw std::runtime_error("Failed to parse input. No leading \"on\"/\"off\" found.");
}
auto xpos = line.find("x=");
if (xpos == std::string::npos)
throw std::runtime_error("Failed to parsed input, did not find x coordinate.");
result.min.x = std::stoll(line.substr(xpos + 2));
auto xpos_max = line.find("..", xpos + 2);
if (xpos_max == std::string::npos)
throw std::runtime_error("Failed to parsed input, did not find end of x coordinate.");
result.max.x = std::stoll(line.substr(xpos_max + 2));
auto ypos = line.find("y=");
if (ypos == std::string::npos)
throw std::runtime_error("Failed to parsed input, did not find y coordinate.");
result.min.y = std::stoll(line.substr(ypos + 2));
auto ypos_max = line.find("..", ypos + 2);
if (ypos_max == std::string::npos)
throw std::runtime_error("Failed to parsed input, did not find end of x coordinate.");
result.max.y = std::stoll(line.substr(ypos_max + 2));
auto zpos = line.find("z=");
if (zpos == std::string::npos)
throw std::runtime_error("Failed to parsed input, did not find z coordinate.");
result.min.z = std::stoll(line.substr(zpos + 2));
auto zpos_max = line.find("..", zpos + 2);
if (zpos_max == std::string::npos)
throw std::runtime_error("Failed to parsed input, did not find end of x coordinate.");
result.max.z = std::stoll(line.substr(zpos_max + 2));
cube = result;
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment