diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-08 07:49:46 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-08 07:49:46 +0100 |
| commit | a299e1723f0a700a892720badab2e06a202d1eda (patch) | |
| tree | 474675a9361b5c7ee805d60972f140fb81380366 /2024 | |
| parent | b3231ba7503f551f09b588355dd9f8145282709c (diff) | |
| download | aoc-a299e1723f0a700a892720badab2e06a202d1eda.tar.gz aoc-a299e1723f0a700a892720badab2e06a202d1eda.zip | |
Day 8 2024
Diffstat (limited to '2024')
| -rw-r--r-- | 2024/08/Makefile | 24 | ||||
| -rw-r--r-- | 2024/08/day08a.cpp | 49 | ||||
| -rw-r--r-- | 2024/08/day08b.cpp | 68 | ||||
| -rw-r--r-- | 2024/learned.txt | 1 | ||||
| -rw-r--r-- | 2024/template.cpp | 2 |
5 files changed, 144 insertions, 0 deletions
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 @@ | |||
| 1 | CC=g++ -std=c++20 -g -Wall | ||
| 2 | |||
| 3 | a: | ||
| 4 | ${CC} -o a.out day08a.cpp | ||
| 5 | |||
| 6 | b: | ||
| 7 | ${CC} -o b.out day08b.cpp | ||
| 8 | |||
| 9 | clean: | ||
| 10 | rm -f a b | ||
| 11 | |||
| 12 | atest: a | ||
| 13 | ./a.out | ||
| 14 | |||
| 15 | btest: b | ||
| 16 | ./b.out | ||
| 17 | |||
| 18 | arun: a | ||
| 19 | ./a.out < input | ||
| 20 | |||
| 21 | brun: b | ||
| 22 | ./b.out < input | ||
| 23 | |||
| 24 | .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 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <set> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <string_view> | ||
| 8 | #include <vector> | ||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | pair<int, int> antinode(pair<int, int> p, pair<int, int> q) { | ||
| 12 | auto [xp, yp] = p; | ||
| 13 | auto [xq, yq] = q; | ||
| 14 | return make_pair(2*xp-xq, 2*yp-yq); | ||
| 15 | } | ||
| 16 | |||
| 17 | void add_antinodes(set<pair<int, int>>& points, | ||
| 18 | pair<int, int> p, set<pair<int, int>>& antinodes) { | ||
| 19 | for (auto q : points) { | ||
| 20 | antinodes.insert(antinode(p, q)); | ||
| 21 | antinodes.insert(antinode(q, p)); | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | int main() { | ||
| 26 | string line; | ||
| 27 | map<char, set<pair<int, int>>> d; | ||
| 28 | set<pair<int, int>> antinodes; | ||
| 29 | int i, j; | ||
| 30 | for (i = 0; getline(cin, line); i++) { | ||
| 31 | stringstream s(line); | ||
| 32 | char c; | ||
| 33 | for (j = 0; s >> c; j++) { | ||
| 34 | if (c != '.') { | ||
| 35 | pair<int, int> p(i, j); | ||
| 36 | add_antinodes(d[c], p, antinodes); | ||
| 37 | d[c].insert(p); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | int tot = 0; | ||
| 43 | for (auto [x, y] : antinodes) | ||
| 44 | if (x >= 0 && x < i && y >= 0 && y < j) | ||
| 45 | tot++; | ||
| 46 | |||
| 47 | cout << tot << endl; | ||
| 48 | return 0; | ||
| 49 | } | ||
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 @@ | |||
| 1 | #include <algorithm> | ||
| 2 | #include <iostream> | ||
| 3 | #include <map> | ||
| 4 | #include <set> | ||
| 5 | #include <sstream> | ||
| 6 | #include <string> | ||
| 7 | #include <string_view> | ||
| 8 | #include <vector> | ||
| 9 | using namespace std; | ||
| 10 | |||
| 11 | #define N 50 | ||
| 12 | #define M 50 | ||
| 13 | |||
| 14 | bool in_range(int x, int y) { | ||
| 15 | return x >= 0 && x < N && y >= 0 && y < M; | ||
| 16 | } | ||
| 17 | |||
| 18 | vector<pair<int, int>> antinode_all(pair<int, int> p, pair<int, int> q) { | ||
| 19 | auto [xp, yp] = p; | ||
| 20 | auto [xq, yq] = q; | ||
| 21 | /* In theory these two coefficients must be divided by the gcd of | ||
| 22 | * of the coordinates, which leads to possibly more points, | ||
| 23 | * as far as I understood the problem statement. Apparently, this | ||
| 24 | * is not the case. */ | ||
| 25 | int gx = (xp - xq); | ||
| 26 | int gy = (yp - yq); | ||
| 27 | vector<pair<int, int>> v; | ||
| 28 | for (int i = 0; in_range(xp+i*gx, yp+i*gy); i++) | ||
| 29 | v.push_back(make_pair(xp+i*gx, yp+i*gy)); | ||
| 30 | for (int i = 0; in_range(xq-i*gx, yq-i*gy); i++) | ||
| 31 | v.push_back(make_pair(xq-i*gx, yq-i*gy)); | ||
| 32 | return v; | ||
| 33 | } | ||
| 34 | |||
| 35 | void add_antinodes(set<pair<int, int>>& points, | ||
| 36 | pair<int, int> p, set<pair<int, int>>& antinodes) { | ||
| 37 | for (auto q : points) { | ||
| 38 | auto a = antinode_all(p, q); | ||
| 39 | for (auto r : a) | ||
| 40 | antinodes.insert(r); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | int main() { | ||
| 45 | string line; | ||
| 46 | map<char, set<pair<int, int>>> d; | ||
| 47 | set<pair<int, int>> antinodes; | ||
| 48 | int i, j; | ||
| 49 | for (i = 0; getline(cin, line); i++) { | ||
| 50 | stringstream s(line); | ||
| 51 | char c; | ||
| 52 | for (j = 0; s >> c; j++) { | ||
| 53 | if (c != '.') { | ||
| 54 | pair<int, int> p(i, j); | ||
| 55 | add_antinodes(d[c], p, antinodes); | ||
| 56 | d[c].insert(p); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | int tot = 0; | ||
| 62 | for (auto [x, y] : antinodes) | ||
| 63 | if (in_range(x, y)) | ||
| 64 | tot++; | ||
| 65 | |||
| 66 | cout << tot << endl; | ||
| 67 | return 0; | ||
| 68 | } | ||
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. | |||
| 6 | * Day 4: nothing special, some practice with classic and a weird trick | 6 | * Day 4: nothing special, some practice with classic and a weird trick |
| 7 | to assign two variables at once with a pair of references pair<int&, int&> | 7 | to assign two variables at once with a pair of references pair<int&, int&> |
| 8 | * Day 5: std::find and std::replace | 8 | * Day 5: std::find and std::replace |
| 9 | * 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 @@ | |||
| 1 | #include <algorithm> | 1 | #include <algorithm> |
| 2 | #include <iostream> | 2 | #include <iostream> |
| 3 | #include <map> | ||
| 4 | #include <set> | ||
| 3 | #include <sstream> | 5 | #include <sstream> |
| 4 | #include <string> | 6 | #include <string> |
| 5 | #include <string_view> | 7 | #include <string_view> |
