From a299e1723f0a700a892720badab2e06a202d1eda Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sun, 8 Dec 2024 07:49:46 +0100 Subject: Day 8 2024 --- 2024/08/Makefile | 24 +++++++++++++++++++ 2024/08/day08a.cpp | 49 +++++++++++++++++++++++++++++++++++++++ 2024/08/day08b.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2024/learned.txt | 1 + 2024/template.cpp | 2 ++ 5 files changed, 144 insertions(+) create mode 100644 2024/08/Makefile create mode 100644 2024/08/day08a.cpp create mode 100644 2024/08/day08b.cpp (limited to '2024') diff --git a/2024/08/Makefile b/2024/08/Makefile new file mode 100644 index 0000000..d335110 --- /dev/null +++ b/2024/08/Makefile @@ -0,0 +1,24 @@ +CC=g++ -std=c++20 -g -Wall + +a: + ${CC} -o a.out day08a.cpp + +b: + ${CC} -o b.out day08b.cpp + +clean: + rm -f a b + +atest: a + ./a.out + +btest: b + ./b.out + +arun: a + ./a.out < input + +brun: b + ./b.out < input + +.PHONY: a b clean atest btest arun brun diff --git a/2024/08/day08a.cpp b/2024/08/day08a.cpp new file mode 100644 index 0000000..7a18ded --- /dev/null +++ b/2024/08/day08a.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +pair antinode(pair p, pair q) { + auto [xp, yp] = p; + auto [xq, yq] = q; + return make_pair(2*xp-xq, 2*yp-yq); +} + +void add_antinodes(set>& points, + pair p, set>& antinodes) { + for (auto q : points) { + antinodes.insert(antinode(p, q)); + antinodes.insert(antinode(q, p)); + } +} + +int main() { + string line; + map>> d; + set> antinodes; + int i, j; + for (i = 0; getline(cin, line); i++) { + stringstream s(line); + char c; + for (j = 0; s >> c; j++) { + if (c != '.') { + pair p(i, j); + add_antinodes(d[c], p, antinodes); + d[c].insert(p); + } + } + } + + int tot = 0; + for (auto [x, y] : antinodes) + if (x >= 0 && x < i && y >= 0 && y < j) + tot++; + + cout << tot << endl; + return 0; +} diff --git a/2024/08/day08b.cpp b/2024/08/day08b.cpp new file mode 100644 index 0000000..1d1381f --- /dev/null +++ b/2024/08/day08b.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#define N 50 +#define M 50 + +bool in_range(int x, int y) { + return x >= 0 && x < N && y >= 0 && y < M; +} + +vector> antinode_all(pair p, pair q) { + auto [xp, yp] = p; + auto [xq, yq] = q; + /* In theory these two coefficients must be divided by the gcd of + * of the coordinates, which leads to possibly more points, + * as far as I understood the problem statement. Apparently, this + * is not the case. */ + int gx = (xp - xq); + int gy = (yp - yq); + vector> v; + for (int i = 0; in_range(xp+i*gx, yp+i*gy); i++) + v.push_back(make_pair(xp+i*gx, yp+i*gy)); + for (int i = 0; in_range(xq-i*gx, yq-i*gy); i++) + v.push_back(make_pair(xq-i*gx, yq-i*gy)); + return v; +} + +void add_antinodes(set>& points, + pair p, set>& antinodes) { + for (auto q : points) { + auto a = antinode_all(p, q); + for (auto r : a) + antinodes.insert(r); + } +} + +int main() { + string line; + map>> d; + set> antinodes; + int i, j; + for (i = 0; getline(cin, line); i++) { + stringstream s(line); + char c; + for (j = 0; s >> c; j++) { + if (c != '.') { + pair p(i, j); + add_antinodes(d[c], p, antinodes); + d[c].insert(p); + } + } + } + + int tot = 0; + for (auto [x, y] : antinodes) + if (in_range(x, y)) + tot++; + + cout << tot << endl; + return 0; +} diff --git a/2024/learned.txt b/2024/learned.txt index 3a01fc0..86d32ff 100644 --- a/2024/learned.txt +++ b/2024/learned.txt @@ -6,3 +6,4 @@ List of things I learned (or refreshed) with this year's AoC. * Day 4: nothing special, some practice with classic and a weird trick to assign two variables at once with a pair of references pair * Day 5: std::find and std::replace +* Day 8: set::insert_range(), but it is from C++23 only diff --git a/2024/template.cpp b/2024/template.cpp index 7508a23..b2fd4ec 100644 --- a/2024/template.cpp +++ b/2024/template.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include #include #include -- cgit v1.3