Skip to content

Instantly share code, notes, and snippets.

@HappyCerberus
Created December 9, 2022 11:39
Show Gist options
  • Save HappyCerberus/96a02ea8863a3651b38c5bcf433edc1f to your computer and use it in GitHub Desktop.
Save HappyCerberus/96a02ea8863a3651b38c5bcf433edc1f to your computer and use it in GitHub Desktop.
enum class Direction {
Right,
Left,
Up,
Down
};
struct Order {
Direction direction;
uint32_t count;
friend std::istream& operator>>(std::istream& s, Order& order) {
char dir;
if (s >> dir >> order.count) switch (dir) {
case 'D': order.direction = Direction::Down; break;
case 'U': order.direction = Direction::Up; break;
case 'L': order.direction = Direction::Left; break;
case 'R': order.direction = Direction::Right; break;
default: throw std::runtime_error("Can't parse Order, wrong direction.");
}
return s;
}
};
// Overloading the stream extraction operator enables the use
// std::views::istream and a simple copy.
std::vector<Order> data;
std::ranges::copy(std::views::istream<Order>(file), std::back_inserter(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment