From eb419a7fbd8ca006e28db80e53e01747c22e4eb9 Mon Sep 17 00:00:00 2001 From: Simponic Date: Mon, 5 Dec 2022 01:23:53 -0700 Subject: [PATCH] Day 5 - c++ --- README.md | 3 +- day-04/sol.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 day-04/sol.cpp diff --git a/README.md b/README.md index a7ecc4a..3f77a72 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ - [Day 2](./day-2) - [x] Common LISP - [Day 3](./day-3) -- [] C++ +- [x] C++ + - [Day 4](./day-4) - [] Dart - [] Elixir - [] Emacs Lisp diff --git a/day-04/sol.cpp b/day-04/sol.cpp new file mode 100644 index 0000000..8c9569c --- /dev/null +++ b/day-04/sol.cpp @@ -0,0 +1,113 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using stack_type = std::stack; + +std::tuple, int> build_stacks(std::vector &lines) +{ + int num_stacks = std::ceil(lines[0].size() / 4.0); + + std::vector stacks(num_stacks); + + std::vector stack_positions(num_stacks, 0); + int lines_of_stacks = 0; + for (std::string line : lines) + { + char l = '1'; + for (int i = 0; i < line.size(); ++i) + if (line[i] == l) + { + stack_positions[l - '1'] = i; + l++; + } + if (l == num_stacks + '1') + break; + lines_of_stacks++; + } + + for (int i = lines_of_stacks - 1; i >= 0; i--) + for (int j = 0; j < num_stacks; ++j) + if (lines[i][stack_positions[j]] != ' ') + stacks[j].push(lines[i][stack_positions[j]]); + + return std::make_tuple(stacks, lines_of_stacks); +} + +std::string solve1(std::vector &stacks, std::vector &lines, int start_from) +{ + for (auto line = lines.begin() + start_from; line != lines.end(); ++line) + { + std::istringstream iss(*line); + std::string word; + int from = 0, to = 0, amount = 0; + + while (iss >> word >> amount >> word >> from >> word >> to) + ; + + for (int i = 0; i < amount; i++) + { + stacks[to - 1].push(stacks[from - 1].top()); + stacks[from - 1].pop(); + } + } + + std::string result; + for (auto &stack : stacks) + result += stack.top(); + return result; +} + +std::string solve2(std::vector &stacks, std::vector &lines, int start_from) +{ + stack_type curr; + for (auto line = lines.begin() + start_from; line != lines.end(); ++line) + { + std::istringstream iss(*line); + std::string word; + int from = 0, to = 0, amount = 0; + + while (iss >> word >> amount >> word >> from >> word >> to) + ; + + for (int i = 0; i < amount; i++) + { + curr.push(stacks[from - 1].top()); + stacks[from - 1].pop(); + } + + for (int i = 0; i < amount; i++) + { + stacks[to - 1].push(curr.top()); + curr.pop(); + } + } + + std::string result; + for (auto &stack : stacks) + result += stack.top(); + return result; +} + +int main() +{ + std::ifstream input("input"); + std::vector lines; + for (std::string line; std::getline(input, line);) + lines.push_back(line); + + auto [stacks, start_from] = build_stacks(lines); + std::cout << "Solve 1: " << solve1(stacks, lines, start_from + 1) << std::endl; + auto [stacks2, start_from2] = build_stacks(lines); + std::cout << "Solve 2: " << solve2(stacks2, lines, start_from2 + 1) << std::endl; + + return 0; +}