From 1f992486fd6ab3a9ef1f36deaff5a3f8d9c7fcf7 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Sat, 14 Dec 2024 23:09:24 -0800 Subject: [PATCH] 2024 day 15 --- 2024/15/main-1.cpp | 82 +++++++++++++++++++++++++++++++ 2024/15/main-2.cpp | 117 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 199 insertions(+) create mode 100644 2024/15/main-1.cpp create mode 100644 2024/15/main-2.cpp diff --git a/2024/15/main-1.cpp b/2024/15/main-1.cpp new file mode 100644 index 0000000..21c453c --- /dev/null +++ b/2024/15/main-1.cpp @@ -0,0 +1,82 @@ +#include +using namespace std; + +inline void incpair(pair &a, pair b) { + a.first += b.first; + a.second += b.second; +} + +inline pair addpair(pair a, pair b) { + incpair(a, b); + return a; +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + vector grid; + string line; + set> boxes; + set> walls; + pair robot; + double x = 0; + while (getline(cin, line) && !line.empty()) { + double y = 0; + for (char c : line) { + switch (c) { + case '#': + walls.insert({x, y}); + break; + case 'O': + boxes.insert({x, y}); + break; + case '@': + robot = {x, y}; + break; + } + y++; + } + x++; + } + + while (getline(cin, line) && !line.empty()) { + for (char c : line) { + pair dir; + switch (c) { + case '<': + dir = {0, -1}; + break; + case '>': + dir = {0, 1}; + break; + case '^': + dir = {-1, 0}; + break; + case 'v': + dir = {1, 0}; + break; + } + + auto first = addpair(robot, dir); + auto next = first; + while (boxes.contains(next)) { + incpair(next, dir); + } + if (walls.contains(next)) { + continue; + } + robot = first; + if (boxes.erase(first)) { + boxes.insert(next); + } + } + } + + int sum = 0; + for (auto box : boxes) { + sum += 100 * box.first + box.second; + } + + cout << sum << '\n'; +} diff --git a/2024/15/main-2.cpp b/2024/15/main-2.cpp new file mode 100644 index 0000000..7a0fee5 --- /dev/null +++ b/2024/15/main-2.cpp @@ -0,0 +1,117 @@ +#include +using namespace std; + +set> boxes; +set> walls; + +inline void incpair(pair &a, pair b) { + a.first += b.first; + a.second += b.second; +} + +inline pair addpair(pair a, pair b) { + incpair(a, b); + return a; +} + +bool rec(pair pos, pair dir, set> &acc) { + if (acc.contains(pos)) { + return true; + } + + if (walls.contains(pos)) { + return false; + } + + if (!boxes.contains(pos)) { + incpair(pos, {0, -1}); + if (!boxes.contains(pos)) { + return true; + } + } + acc.insert(pos); + incpair(pos, dir); + + bool other; + switch (dir.second) { + case 0: + other = rec(addpair(pos, {0, 1}), dir, acc); + break; + case 1: + pos.second += 1; + default: + other = true; + } + + return other && rec(pos, dir, acc); +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + vector grid; + string line; + pair robot; + double x = 0; + while (getline(cin, line) && !line.empty()) { + double y = 0; + for (char c : line) { + switch (c) { + case '#': + walls.insert({x, y}); + walls.insert({x, y + 1}); + break; + case 'O': + boxes.insert({x, y}); + break; + case '@': + robot = {x, y}; + break; + } + y += 2; + } + x++; + } + + while (getline(cin, line) && !line.empty()) { + for (char c : line) { + pair dir; + switch (c) { + case '<': + dir = {0, -1}; + break; + case '>': + dir = {0, 1}; + break; + case '^': + dir = {-1, 0}; + break; + case 'v': + dir = {1, 0}; + break; + } + + set> r; + auto first = addpair(robot, dir); + if (!rec(first, dir, r)) { + continue; + } + + robot = first; + for (auto box : r) { + boxes.erase(box); + } + for (auto box : r) { + boxes.insert(addpair(box, dir)); + } + } + } + + int sum = 0; + for (auto box : boxes) { + sum += 100 * box.first + box.second; + } + + cout << sum << '\n'; +}