diff --git a/2024/20/main-1.cpp b/2024/20/main-1.cpp new file mode 100644 index 0000000..09db839 --- /dev/null +++ b/2024/20/main-1.cpp @@ -0,0 +1,77 @@ +#include "../../include/aoc.hpp" +#include +using namespace std; + +const array directions = { + icoord(-1, 0), + icoord(1, 0), + icoord(0, -1), + icoord(0, 1), +}; + +void rec(icoord c, unordered_map &m, int score) { + if (!m.contains(c)) { + return; + } + + auto &s = m[c]; + if (s != -1 && s <= score) { + return; + } + + s = score; + for (auto dir : directions) { + rec(c + dir, m, score + 1); + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + + icoord p, s, e; + unordered_map 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'; +} diff --git a/2024/20/main-2.cpp b/2024/20/main-2.cpp new file mode 100644 index 0000000..6d06d3b --- /dev/null +++ b/2024/20/main-2.cpp @@ -0,0 +1,78 @@ +#include "../../include/aoc.hpp" +#include +using namespace std; + +const array directions = { + icoord(-1, 0), + icoord(1, 0), + icoord(0, -1), + icoord(0, 1), +}; + +void rec(icoord c, unordered_map &m, int score) { + if (!m.contains(c)) { + return; + } + + auto &s = m[c]; + if (s != -1 && s <= score) { + return; + } + + s = score; + for (auto dir : directions) { + rec(c + dir, m, score + 1); + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0); + + string line; + + icoord p, s, e; + unordered_map 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; + int l = abs(dc.x) + abs(dc.y); + if (l > 20) { + continue; + } + if (ss + es + l <= t - 100) { + count++; + } + } + } + cout << count << '\n'; +}