Compare commits

...

2 commits

Author SHA1 Message Date
eriedaberrie 4f20bfde3b 2024 day 20 2024-12-19 22:02:11 -08:00
eriedaberrie 7f1556b52d Remove nearby and add units to util file 2024-12-19 22:01:51 -08:00
3 changed files with 163 additions and 22 deletions

77
2024/20/main-1.cpp Normal file
View file

@ -0,0 +1,77 @@
#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
const array<icoord, 4> directions = {
icoord(-1, 0),
icoord(1, 0),
icoord(0, -1),
icoord(0, 1),
};
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 : 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<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';
}

78
2024/20/main-2.cpp Normal file
View file

@ -0,0 +1,78 @@
#include "../../include/aoc.hpp"
#include <bits/stdc++.h>
using namespace std;
const array<icoord, 4> directions = {
icoord(-1, 0),
icoord(1, 0),
icoord(0, -1),
icoord(0, 1),
};
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 : 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<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;
int l = abs(dc.x) + abs(dc.y);
if (l > 20) {
continue;
}
if (ss + es + l <= t - 100) {
count++;
}
}
}
cout << count << '\n';
}

View file

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <vector> #include <array>
typedef long long ll; typedef long long ll;
typedef unsigned long long ull; typedef unsigned long long ull;
@ -15,6 +15,13 @@ template <typename T> struct Coord {
this->y = y; this->y = y;
} }
static inline const std::array<Coord<T>, 4> units = {
Coord<T>(0, 1),
Coord<T>(1, 0),
Coord<T>(0, -1),
Coord<T>(-1, 0),
};
Coord<T> operator-() { return {-this->x, -this->y}; } Coord<T> operator-() { return {-this->x, -this->y}; }
Coord<T> &operator+=(const Coord<T> &other) { Coord<T> &operator+=(const Coord<T> &other) {
@ -40,27 +47,6 @@ template <typename T> struct Coord {
y /= other; y /= other;
return *this; return *this;
} }
std::vector<Coord<T>> nearby(const Coord<T> &cMin, const Coord<T> &cMax) {
std::vector<Coord<T>> ret;
const T xMin = std::max(this->x - 1, cMin.x);
const T xMax = std::min(this->x + 1, cMax.x);
const T yMin = std::max(this->y - 1, cMin.y);
const T yMax = std::min(this->y + 1, cMax.y);
for (T x = xMin; x <= xMax; x++) {
for (T y = yMin; y <= yMax; y++) {
Coord<T> xy(x, y);
if (*this != xy) {
ret.push_back(xy);
}
}
}
return ret;
}
std::vector<Coord<T>> nearby(const Coord<T> &cMax) {
return this->nearby(Coord<T>(), cMax);
}
}; };
template <typename T> template <typename T>