advent-of-code/2024/16/main-1.cpp
2024-12-16 01:28:41 -08:00

72 lines
1.1 KiB
C++

#include <bits/stdc++.h>
#include "../../include/aoc.hpp"
using namespace std;
unordered_map<icoord, unordered_map<int, int>> m;
inline icoord posdir(int p) {
switch (p) {
case 0:
return {0, 1};
case 1:
return {1, 0};
case 2:
return {0, -1};
case 3:
return {-1, 0};
default:
return {};
}
}
void rec(icoord pos, int dir, int score) {
if (!m.contains(pos)) {
return;
}
auto &mp = m[pos];
if (mp.contains(dir) && mp[dir] <= score) {
return;
}
mp[dir] = score;
rec(pos + posdir(dir), dir, score + 1);
rec(pos, (dir + 1) & 3, score + 1000);
rec(pos, (dir - 1) & 3, score + 1000);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
icoord pos, s, e;
string line;
while (getline(cin, line) && !line.empty()) {
for (pos.y = 0; pos.y < line.size(); pos.y++) {
switch (line[pos.y]) {
case '#':
continue;
case 'S':
s = pos;
break;
case 'E':
e = pos;
break;
}
m[pos];
}
pos.x++;
}
rec(s, 0, 0);
int minscore = -1;
for (auto [dir, score] : m[e]) {
if (minscore == -1 || score < minscore) {
minscore = score;
}
}
cout << minscore << '\n';
}