2024 day 6

This commit is contained in:
eriedaberrie 2024-12-05 21:42:10 -08:00
parent 0fabff92b8
commit 06b7ac26cf
2 changed files with 134 additions and 0 deletions

60
2024/6/main-1.cpp Normal file
View file

@ -0,0 +1,60 @@
#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()) {
istringstream l(line);
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';
}

74
2024/6/main-2.cpp Normal file
View file

@ -0,0 +1,74 @@
#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()) {
istringstream l(line);
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;
if (!track.contains(pos)) {
set<int> s;
track[pos] = s;
} else if (track[pos].contains(d)) {
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';
}