advent-of-code/2024/20/main-1.cpp
2024-12-19 22:13:32 -08:00

71 lines
1 KiB
C++

#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
void rec(icoord c, unordered_map<icoord, int> &m, int score) {
if (!m.contains(c)) {
return;
}
auto &s = m[c];
if (s != -1 && s <= score) {
return;
}
s = score;
for (auto dir : icoord::units()) {
rec(c + dir, m, score + 1);
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
string line;
icoord p, s, e;
unordered_map<icoord, int> ms, me;
while (getline(cin, line)) {
for (p.y = 0; p.y < line.size(); p.y++) {
switch (line[p.y]) {
case '#':
continue;
case 'S':
s = p;
break;
case 'E':
e = p;
break;
}
ms[p] = -1;
me[p] = -1;
}
p.x++;
}
rec(s, ms, 0);
rec(e, me, 0);
int t = ms[e];
int count = 0;
for (auto [sc, ss] : ms) {
if (ss == -1) {
continue;
}
for (auto [ec, es] : me) {
if (es == -1) {
continue;
}
auto dc = sc - ec;
if (abs(dc.x) + abs(dc.y) != 2) {
continue;
}
if (ss + es + 2 <= t - 100) {
count++;
}
}
}
cout << count << '\n';
}