advent-of-code/2024/6/main-2.cpp

71 lines
1.3 KiB
C++
Raw Permalink Normal View History

2024-12-05 21:42:10 -08:00
#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> 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;
for (int x = 0; x < lines.size(); x++) {
for (int y = 0; y < lines[0].size(); y++) {
pair<int, int> dir = {-1, 0};
auto pos = pos_;
if (lines[x][y] != '.')
continue;
lines[x][y] = '#';
map<pair<int, int>, set<int>> track;
while (pos.first >= 0 && pos.first < lines.size() &&
pos.second >= 0 && pos.second < lines[0].size()) {
if (lines[pos.first][pos.second] == '#') {
pos.first -= dir.first;
pos.second -= dir.second;
int d = dir.first * 2 + dir.second;
2024-12-07 21:59:13 -08:00
if (track[pos].contains(d)) {
2024-12-05 21:42:10 -08:00
count++;
goto next;
}
track[pos].insert(d);
switch (d) {
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;
}
next:;
lines[x][y] = '.';
}
}
cout << count << '\n';
}