Skip to content

Instantly share code, notes, and snippets.

@HappyCerberus
Created December 5, 2022 09:55
Show Gist options
  • Save HappyCerberus/d3eb7b713193e6e12c7f918f620e2afc to your computer and use it in GitHub Desktop.
Save HappyCerberus/d3eb7b713193e6e12c7f918f620e2afc to your computer and use it in GitHub Desktop.
struct Order {
uint32_t count;
size_t from;
size_t to;
};
void crateMover9001(std::vector<std::vector<char>>& state,
const std::vector<Order>& orders) {
for (auto &order : orders) {
// view of the last order.count elements in order
auto src = state[order.from] |
std::views::reverse | std::views::take(order.count) |
std::views::reverse;
std::ranges::copy(src, std::back_inserter(state[order.to]));
// remove the last order.count elements
state[order.from].resize(state[order.from].size()-order.count);
}
}
std::string top_crates(const std::vector<std::vector<char>>& state) {
std::string result;
for (auto &stack : state) {
if (stack.size() == 0) continue;
result += stack.back();
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment