commit bd997bf176c831f57ad6580e31d736b879f9d670
parent 3d6cfaf209bb9e2606df478dbf812c311634859c
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date: Sun, 8 Dec 2024 07:49:46 +0100
Day 8 2024
Diffstat:
6 files changed, 194 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/08/input b/2024/08/input
@@ -0,0 +1,50 @@
+.C...............w.......................M.E......
+...............G........V.............Q....M......
+u........k...........V.y..3........Q..........4.a.
+..........c.9........k..................i..7..a...
+..............y.......................o....a......
+.......C...........6.......y.............E........
+.............................5....x............i..
+...............c.....wy..V.......5..............E.
+........k.......c....G..I............o.........m..
+............C....s......G......o..........5.......
+......................Q...............5....e...4i.
+.....I.....................................m.....j
+....9K.T.....I...c......w...................X.....
+................I.........w....f............3..e.N
+C............9..........6..............7...3......
+...Z........K.......T.................6...........
+......Z..................6...............HN.E.m...
+...K...........................1....N...e.o..X....
+............hz......................7........j....
+.........9......U.R......n.....4.Q..L...X.........
+..................A...........S.......0...........
+...............l.........p...........2.3M.......x.
+.h........................U.................g.....
+...Hld...........A..W.......................1x....
+.....Z.....n.......lp...e............Xj...L.......
+........hU................7...j...S...............
+......n............U..........D....S..q...........
+....H.....d.r..T..............0..........L.S......
+......H......A..T...lp.........LK....1.....2.f.x..
+....Z............................g....4...........
+..d..r............V...............f..g....2.......
+.rn.........D............Pp........q....g.........
+..................................................
+...................D...0.........Y..t...P.q.......
+.......R.s.......................q.P..1...........
+...........h..........................2.........f.
+........................W.........................
+...8...........O................k.................
+....rY...........D................P...............
+....................O...u.........................
+..s..................F............................
+...................R......F.......................
+......8...........z0....F................J.W......
+...................F..z................u..........
+..............R.........O.............v.Jt........
+s.............8.........m........J.t............v.
+......Y.....M........................u..tv........
+.................................................v
+..................................................
+.................z.W..................J...........
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>