Advertisement
Guest User

Untitled

a guest
Dec 5th, 2022
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. std::string input(stdfs::path const& path) {
  2.     std::ifstream reader(path);
  3.     std::string text{std::istreambuf_iterator<char>(reader), std::istreambuf_iterator<char>()};
  4.  
  5.     return text.substr(text.find("\n\n") + 2);
  6. }
  7.  
  8. std::string move_crates(std::array<std::string, 9> crates, std::string_view instructions, bool reverse = true) {
  9.     for (auto line : instructions | std::views::split("\n"sv)) {
  10.         int count, from, to;
  11.         std::ignore = scn::scan(line, "move {} from {} to {}"sv, count, from, to);
  12.  
  13.         auto x = crates[from - 1].substr(0, count);
  14.         if (reverse)
  15.             stdr::reverse(x);
  16.  
  17.         crates[to - 1] = x + crates[to - 1];
  18.         crates[from - 1].erase(0, count);
  19.     }
  20.  
  21.     auto it = std::views::transform(crates, [](auto&& s) { return s[0]; });
  22.     return std::string(it.begin(), it.end());
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement