aoc

My solutions for the Advent of Code
git clone https://git.tronto.net/aoc
Download | Log | Files | Refs | README

commit a299e1723f0a700a892720badab2e06a202d1eda
parent b3231ba7503f551f09b588355dd9f8145282709c
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Sun,  8 Dec 2024 07:49:46 +0100

Day 8 2024

Diffstat:
A2024/08/Makefile | 24++++++++++++++++++++++++
A2024/08/day08a.cpp | 49+++++++++++++++++++++++++++++++++++++++++++++++++
A2024/08/day08b.cpp | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
M2024/learned.txt | 1+
M2024/template.cpp | 2++
5 files changed, 144 insertions(+), 0 deletions(-)

diff --git a/2024/08/Makefile 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 @@ -0,0 +1,49 @@ +#include <algorithm> +#include <iostream> +#include <map> +#include <set> +#include <sstream> +#include <string> +#include <string_view> +#include <vector> +using namespace std; + +pair<int, int> antinode(pair<int, int> p, pair<int, int> q) { + auto [xp, yp] = p; + auto [xq, yq] = q; + return make_pair(2*xp-xq, 2*yp-yq); +} + +void add_antinodes(set<pair<int, int>>& points, + pair<int, int> p, set<pair<int, int>>& antinodes) { + for (auto q : points) { + antinodes.insert(antinode(p, q)); + antinodes.insert(antinode(q, p)); + } +} + +int main() { + string line; + map<char, set<pair<int, int>>> d; + set<pair<int, int>> 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<int, int> 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 @@ -0,0 +1,68 @@ +#include <algorithm> +#include <iostream> +#include <map> +#include <set> +#include <sstream> +#include <string> +#include <string_view> +#include <vector> +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<pair<int, int>> antinode_all(pair<int, int> p, pair<int, int> 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<pair<int, int>> 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<pair<int, int>>& points, + pair<int, int> p, set<pair<int, int>>& antinodes) { + for (auto q : points) { + auto a = antinode_all(p, q); + for (auto r : a) + antinodes.insert(r); + } +} + +int main() { + string line; + map<char, set<pair<int, int>>> d; + set<pair<int, int>> 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<int, int> 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 @@ -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<int&, int&> * 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 @@ -1,5 +1,7 @@ #include <algorithm> #include <iostream> +#include <map> +#include <set> #include <sstream> #include <string> #include <string_view>