aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-04 08:07:57 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-04 08:07:57 +0100
commit59892b65a9810044ca7f135e39c8d44094224c85 (patch)
treeeaaeefa9253f2828bf17fe79ac2f690fe9bd708c
parent4aebede600c180b51d0b6ded0e42ec8a4223283e (diff)
downloadaoc-59892b65a9810044ca7f135e39c8d44094224c85.tar.gz
aoc-59892b65a9810044ca7f135e39c8d44094224c85.zip
Day 4 2024
-rw-r--r--2024/04/Makefile24
-rwxr-xr-x2024/04/b.outbin0 -> 176168 bytes
-rw-r--r--2024/04/day04a.cpp96
-rw-r--r--2024/04/day04b.cpp114
-rw-r--r--2024/learned.txt2
5 files changed, 236 insertions, 0 deletions
diff --git a/2024/04/Makefile b/2024/04/Makefile
new file mode 100644
index 0000000..855c831
--- /dev/null
+++ b/2024/04/Makefile
@@ -0,0 +1,24 @@
1CC=g++ -std=c++20 -g -Wall
2
3a:
4 ${CC} -o a.out day04a.cpp
5
6b:
7 ${CC} -o b.out day04b.cpp
8
9clean:
10 rm -f a b
11
12atest: a
13 ./a.out
14
15btest: b
16 ./b.out
17
18arun: a
19 ./a.out < input
20
21brun: b
22 ./b.out < input
23
24.PHONY: a b clean atest btest arun brun
diff --git a/2024/04/b.out b/2024/04/b.out
new file mode 100755
index 0000000..d0afbc2
--- /dev/null
+++ b/2024/04/b.out
Binary files differ
diff --git a/2024/04/day04a.cpp b/2024/04/day04a.cpp
new file mode 100644
index 0000000..887ebef
--- /dev/null
+++ b/2024/04/day04a.cpp
@@ -0,0 +1,96 @@
1#include <algorithm>
2#include <iostream>
3#include <sstream>
4#include <string>
5#include <string_view>
6#include <vector>
7using namespace std;
8
9enum class Direction { U, D, R, L, UR, UL, DR, DL };
10Direction all_directions[] = {
11 Direction::U, Direction::D, Direction::R, Direction::L,
12 Direction::UR, Direction::UL, Direction::DR, Direction::DL
13};
14
15pair<int, int> step(pair<int, int> p, Direction d) {
16 auto [i, j] = p;
17
18 switch (d) {
19 case Direction::U:
20 return make_pair(i-1, j);
21 case Direction::D:
22 return make_pair(i+1, j);
23 case Direction::R:
24 return make_pair(i, j+1);
25 case Direction::L:
26 return make_pair(i, j-1);
27 case Direction::UR:
28 return make_pair(i-1, j+1);
29 case Direction::UL:
30 return make_pair(i-1, j-1);
31 case Direction::DR:
32 return make_pair(i+1, j+1);
33 case Direction::DL:
34 return make_pair(i+1, j-1);
35 }
36
37 return make_pair(-999,-999);
38}
39
40class Board {
41public:
42 int M, N;
43
44 Board(vector<string> &lines) {
45 N = lines.size();
46 M = 0;
47 for (string l : lines)
48 M = max(M, (int)l.size());
49 cells = new char[M * N];
50 for (int i = 0; i < N; i++)
51 for (int j = 0; j < M; j++)
52 cells[N*i + j] = j < (int)lines[i].size() ?
53 lines[i][j] : out_of_bound;
54 }
55
56 string word_at(int i, int j, Direction d, int l) {
57 string ret = "";
58 auto p = make_pair(i, j);
59 for (int k = 0; k < l; k++, p = step(p, d))
60 ret += (*this)[p];
61 return ret;
62 }
63
64 ~Board() {
65 delete []cells;
66 }
67
68private:
69 const char out_of_bound = '.';
70 char *cells;
71
72 char operator[](pair<int, int> p) {
73 auto [i, j] = p;
74 if (i >= N || i < 0 || j >= M || j < 0)
75 return out_of_bound;
76 return cells[N*i + j];
77 }
78};
79
80int main() {
81 string line;
82 vector<string> lines;
83 while (getline(cin, line))
84 lines.push_back(line);
85
86 Board board(lines);
87 int tot = 0;
88 for (int i = 0; i < board.N; i++)
89 for (int j = 0; j < board.M; j++)
90 for (Direction d : all_directions)
91 if (board.word_at(i, j, d, 4) == "XMAS")
92 tot++;
93 cout << tot << endl;
94
95 return 0;
96}
diff --git a/2024/04/day04b.cpp b/2024/04/day04b.cpp
new file mode 100644
index 0000000..3d338d8
--- /dev/null
+++ b/2024/04/day04b.cpp
@@ -0,0 +1,114 @@
1#include <algorithm>
2#include <iostream>
3#include <sstream>
4#include <string>
5#include <string_view>
6#include <vector>
7using namespace std;
8
9enum class Direction { U, D, R, L, UR, UL, DR, DL };
10Direction all_directions[] = {
11 Direction::U, Direction::D, Direction::R, Direction::L,
12 Direction::UR, Direction::UL, Direction::DR, Direction::DL
13};
14
15pair<int, int> step(pair<int, int> p, Direction d) {
16 auto [i, j] = p;
17
18 switch (d) {
19 case Direction::U:
20 return make_pair(i-1, j);
21 case Direction::D:
22 return make_pair(i+1, j);
23 case Direction::R:
24 return make_pair(i, j+1);
25 case Direction::L:
26 return make_pair(i, j-1);
27 case Direction::UR:
28 return make_pair(i-1, j+1);
29 case Direction::UL:
30 return make_pair(i-1, j-1);
31 case Direction::DR:
32 return make_pair(i+1, j+1);
33 case Direction::DL:
34 return make_pair(i+1, j-1);
35 }
36
37 return make_pair(-999,-999);
38}
39
40class Board {
41public:
42 int M, N;
43
44 Board(vector<string> &lines) {
45 N = lines.size();
46 M = 0;
47 for (string l : lines)
48 M = max(M, (int)l.size());
49 cells = new char[M * N];
50 for (int i = 0; i < N; i++)
51 for (int j = 0; j < M; j++)
52 cells[N*i + j] = j < (int)lines[i].size() ?
53 lines[i][j] : out_of_bound;
54 }
55
56 string word_at(int i, int j, Direction d, int l) {
57 string ret = "";
58 auto p = make_pair(i, j);
59 for (int k = 0; k < l; k++, p = step(p, d))
60 ret += (*this)[p];
61 return ret;
62 }
63
64 ~Board() {
65 delete []cells;
66 }
67
68private:
69 const char out_of_bound = '.';
70 char *cells;
71
72 char operator[](pair<int, int> p) {
73 auto [i, j] = p;
74 if (i >= N || i < 0 || j >= M || j < 0)
75 return out_of_bound;
76 return cells[N*i + j];
77 }
78};
79
80int main() {
81 string line;
82 vector<string> lines;
83 while (getline(cin, line))
84 lines.push_back(line);
85
86 Board board(lines);
87 int tot = 0;
88 for (int i = 0; i < board.N; i++) {
89 for (int j = 0; j < board.M; j++) {
90 int tmp = 0;
91 auto p = make_pair(i, j);
92 auto [x, y] = step(p, Direction::UL);
93 string wUL = board.word_at(x, y, Direction::DR, 3);
94 tmp += wUL == "MAS";
95
96 pair<int&, int&>(x, y) = step(p, Direction::UR);
97 string wUR = board.word_at(x, y, Direction::DL, 3);
98 tmp += wUR == "MAS";
99
100 pair<int&, int&>(x, y) = step(p, Direction::DL);
101 string wDL = board.word_at(x, y, Direction::UR, 3);
102 tmp += wDL == "MAS";
103
104 pair<int&, int&>(x, y) = step(p, Direction::DR);
105 string wDR = board.word_at(x, y, Direction::UL, 3);
106 tmp += wDR == "MAS";
107
108 if (tmp == 2) tot++;
109 }
110 }
111 cout << tot << endl;
112
113 return 0;
114}
diff --git a/2024/learned.txt b/2024/learned.txt
index 63a0a71..4ab29b1 100644
--- a/2024/learned.txt
+++ b/2024/learned.txt
@@ -3,3 +3,5 @@ List of things I learned (or refreshed) with this year's AoC.
3* Day 1: std::count() 3* Day 1: std::count()
4* Day 2: std::sstream 4* Day 2: std::sstream
5* Day 3: std::string_view 5* Day 3: std::string_view
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&>

Generated with cgit - Back to sebastiano.tronto.net