advent-of-code/2024/15/main-1.cpp

83 lines
1.3 KiB
C++
Raw Normal View History

2024-12-14 23:09:24 -08:00
#include <bits/stdc++.h>
using namespace std;
inline void incpair(pair<int, int> &a, pair<int, int> b) {
a.first += b.first;
a.second += b.second;
}
inline pair<int, int> addpair(pair<int, int> a, pair<int, int> b) {
incpair(a, b);
return a;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vector<string> grid;
string line;
set<pair<int, int>> boxes;
set<pair<int, int>> walls;
pair<int, int> 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<int, int> 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';
}