advent-of-code/2024/6/main-1.cpp
2024-12-07 21:59:13 -08:00

60 lines
1 KiB
C++

#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string line;
vector<string> lines;
while (getline(cin, line) && !line.empty()) {
lines.push_back(line);
}
pair<int, int> dir = {-1, 0};
pair<int, int> pos;
for (int x = 0; x < lines.size(); x++) {
for (int y = 0; y < lines[x].size(); y++) {
if (lines[x][y] == '^') {
pos = {x, y};
goto posend;
}
}
}
posend:;
int count = 0;
while (pos.first >= 0 && pos.first < lines.size() && pos.second >= 0 &&
pos.second < lines[0].size()) {
auto x = &lines[pos.first][pos.second];
switch (*x) {
case '^':
case '.':
*x = 'X';
count++;
break;
case '#':
pos.first -= dir.first;
pos.second -= dir.second;
switch (dir.first * 2 + dir.second) {
case -2:
dir = {0, 1};
break;
case -1:
dir = {-1, 0};
break;
case 1:
dir = {1, 0};
break;
case 2:
dir = {0, -1};
break;
}
}
pos.first += dir.first;
pos.second += dir.second;
}
cout << count << '\n';
}