diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-10 06:13:42 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-10 06:13:42 +0100 |
| commit | 6cee2c54045d253dc14c808f1e4f25765a675a56 (patch) | |
| tree | e5a570a0edb5fc774eebbb4cad962c1a83c2b4cd /2024/10 | |
| parent | 9a1f073f8e33e8b0d6c97332f51161683cfc6e58 (diff) | |
| download | aoc-6cee2c54045d253dc14c808f1e4f25765a675a56.tar.gz aoc-6cee2c54045d253dc14c808f1e4f25765a675a56.zip | |
Day 10 2024
Diffstat (limited to '2024/10')
| -rw-r--r-- | 2024/10/Makefile | 24 | ||||
| -rw-r--r-- | 2024/10/day10a.cpp | 113 | ||||
| -rw-r--r-- | 2024/10/day10b.cpp | 112 |
3 files changed, 249 insertions, 0 deletions
diff --git a/2024/10/Makefile b/2024/10/Makefile new file mode 100644 index 0000000..0211441 --- /dev/null +++ b/2024/10/Makefile | |||
| @@ -0,0 +1,24 @@ | |||
| 1 | CC=g++ -std=c++20 -g -Wall | ||
| 2 | |||
| 3 | a: | ||
| 4 | ${CC} -o a.out day10a.cpp | ||
| 5 | |||
| 6 | b: | ||
| 7 | ${CC} -o b.out day10b.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/10/day10a.cpp b/2024/10/day10a.cpp new file mode 100644 index 0000000..fda4ab8 --- /dev/null +++ b/2024/10/day10a.cpp | |||
| @@ -0,0 +1,113 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <algorithm> | ||
| 3 | #include <set> | ||
| 4 | #include <string> | ||
| 5 | #include <string_view> | ||
| 6 | #include <vector> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | enum Direction { U = 0, D, R, L }; | ||
| 10 | Direction all_directions[] = { | ||
| 11 | Direction::U, Direction::D, Direction::R, Direction::L, | ||
| 12 | }; | ||
| 13 | |||
| 14 | pair<int, int> step(pair<int, int> p, Direction d) { | ||
| 15 | auto [i, j] = p; | ||
| 16 | |||
| 17 | switch (d) { | ||
| 18 | case Direction::U: | ||
| 19 | return make_pair(i-1, j); | ||
| 20 | case Direction::D: | ||
| 21 | return make_pair(i+1, j); | ||
| 22 | case Direction::R: | ||
| 23 | return make_pair(i, j+1); | ||
| 24 | case Direction::L: | ||
| 25 | return make_pair(i, j-1); | ||
| 26 | } | ||
| 27 | |||
| 28 | return make_pair(-999,-999); | ||
| 29 | } | ||
| 30 | |||
| 31 | class Board { | ||
| 32 | public: | ||
| 33 | const int out_of_bound = -1; | ||
| 34 | int M, N; | ||
| 35 | |||
| 36 | Board(vector<string> &lines) { | ||
| 37 | N = lines.size(); | ||
| 38 | M = 0; | ||
| 39 | for (string l : lines) | ||
| 40 | M = max(M, (int)l.size()); | ||
| 41 | |||
| 42 | cells = new int[M * N]; | ||
| 43 | for (int i = 0; i < N; i++) | ||
| 44 | for (int j = 0; j < M; j++) | ||
| 45 | cells[N*i + j] = lines[i][j] - '0'; | ||
| 46 | } | ||
| 47 | |||
| 48 | ~Board() { | ||
| 49 | delete []cells; | ||
| 50 | } | ||
| 51 | |||
| 52 | int operator[](pair<int, int> p) { | ||
| 53 | int c = coord(p); | ||
| 54 | return c == -1 ? out_of_bound : cells[c]; | ||
| 55 | } | ||
| 56 | |||
| 57 | private: | ||
| 58 | int *cells; | ||
| 59 | |||
| 60 | int coord(pair<int, int> p) { | ||
| 61 | auto [i, j] = p; | ||
| 62 | return i >= N || i < 0 || j >= M || j < 0 ? -1 : N*i + j; | ||
| 63 | } | ||
| 64 | }; | ||
| 65 | |||
| 66 | void reachable9(pair<int, int> p, Board& board, set<pair<int, int>>& s) { | ||
| 67 | if (board[p] == 9) { | ||
| 68 | s.insert(p); | ||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | auto q = step(p, Direction::U); | ||
| 73 | if (board[q] == board[p]+1) | ||
| 74 | reachable9(q, board, s); | ||
| 75 | |||
| 76 | q = step(p, Direction::D); | ||
| 77 | if (board[q] == board[p]+1) | ||
| 78 | reachable9(q, board, s); | ||
| 79 | |||
| 80 | q = step(p, Direction::R); | ||
| 81 | if (board[q] == board[p]+1) | ||
| 82 | reachable9(q, board, s); | ||
| 83 | |||
| 84 | q = step(p, Direction::L); | ||
| 85 | if (board[q] == board[p]+1) | ||
| 86 | reachable9(q, board, s); | ||
| 87 | } | ||
| 88 | |||
| 89 | int main() { | ||
| 90 | string line; | ||
| 91 | vector<string> lines; | ||
| 92 | while (getline(cin, line)) | ||
| 93 | lines.push_back(line); | ||
| 94 | |||
| 95 | Board board(lines); | ||
| 96 | |||
| 97 | int tot = 0; | ||
| 98 | |||
| 99 | pair i(0, 0); | ||
| 100 | for (i.first = 0; i.first < board.N; i.first++) { | ||
| 101 | for (i.second = 0; i.second < board.M; i.second++) { | ||
| 102 | if (board[i] == 0) { | ||
| 103 | set<pair<int, int>> s; | ||
| 104 | reachable9(i, board, s); | ||
| 105 | tot += s.size(); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | cout << tot << endl; | ||
| 111 | |||
| 112 | return 0; | ||
| 113 | } | ||
diff --git a/2024/10/day10b.cpp b/2024/10/day10b.cpp new file mode 100644 index 0000000..de972c0 --- /dev/null +++ b/2024/10/day10b.cpp | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | #include <iostream> | ||
| 2 | #include <algorithm> | ||
| 3 | #include <set> | ||
| 4 | #include <string> | ||
| 5 | #include <string_view> | ||
| 6 | #include <vector> | ||
| 7 | using namespace std; | ||
| 8 | |||
| 9 | enum Direction { U = 0, D, R, L }; | ||
| 10 | Direction all_directions[] = { | ||
| 11 | Direction::U, Direction::D, Direction::R, Direction::L, | ||
| 12 | }; | ||
| 13 | |||
| 14 | pair<int, int> step(pair<int, int> p, Direction d) { | ||
| 15 | auto [i, j] = p; | ||
| 16 | |||
| 17 | switch (d) { | ||
| 18 | case Direction::U: | ||
| 19 | return make_pair(i-1, j); | ||
| 20 | case Direction::D: | ||
| 21 | return make_pair(i+1, j); | ||
| 22 | case Direction::R: | ||
| 23 | return make_pair(i, j+1); | ||
| 24 | case Direction::L: | ||
| 25 | return make_pair(i, j-1); | ||
| 26 | } | ||
| 27 | |||
| 28 | return make_pair(-999,-999); | ||
| 29 | } | ||
| 30 | |||
| 31 | class Board { | ||
| 32 | public: | ||
| 33 | const int out_of_bound = -1; | ||
| 34 | int M, N; | ||
| 35 | |||
| 36 | Board(vector<string> &lines) { | ||
| 37 | N = lines.size(); | ||
| 38 | M = 0; | ||
| 39 | for (string l : lines) | ||
| 40 | M = max(M, (int)l.size()); | ||
| 41 | |||
| 42 | cells = new int[M * N]; | ||
| 43 | for (int i = 0; i < N; i++) | ||
| 44 | for (int j = 0; j < M; j++) | ||
| 45 | cells[N*i + j] = lines[i][j] - '0'; | ||
| 46 | } | ||
| 47 | |||
| 48 | ~Board() { | ||
| 49 | delete []cells; | ||
| 50 | } | ||
| 51 | |||
| 52 | int operator[](pair<int, int> p) { | ||
| 53 | int c = coord(p); | ||
| 54 | return c == -1 ? out_of_bound : cells[c]; | ||
| 55 | } | ||
| 56 | |||
| 57 | private: | ||
| 58 | int *cells; | ||
| 59 | |||
| 60 | int coord(pair<int, int> p) { | ||
| 61 | auto [i, j] = p; | ||
| 62 | return i >= N || i < 0 || j >= M || j < 0 ? -1 : N*i + j; | ||
| 63 | } | ||
| 64 | }; | ||
| 65 | |||
| 66 | int reachable9(pair<int, int> p, Board& board) { | ||
| 67 | if (board[p] == 9) | ||
| 68 | return 1; | ||
| 69 | |||
| 70 | int ret = 0; | ||
| 71 | auto q = step(p, Direction::U); | ||
| 72 | if (board[q] == board[p]+1) | ||
| 73 | ret += reachable9(q, board); | ||
| 74 | |||
| 75 | q = step(p, Direction::D); | ||
| 76 | if (board[q] == board[p]+1) | ||
| 77 | ret += reachable9(q, board); | ||
| 78 | |||
| 79 | q = step(p, Direction::R); | ||
| 80 | if (board[q] == board[p]+1) | ||
| 81 | ret += reachable9(q, board); | ||
| 82 | |||
| 83 | q = step(p, Direction::L); | ||
| 84 | if (board[q] == board[p]+1) | ||
| 85 | ret += reachable9(q, board); | ||
| 86 | |||
| 87 | return ret; | ||
| 88 | } | ||
| 89 | |||
| 90 | int main() { | ||
| 91 | string line; | ||
| 92 | vector<string> lines; | ||
| 93 | while (getline(cin, line)) | ||
| 94 | lines.push_back(line); | ||
| 95 | |||
| 96 | Board board(lines); | ||
| 97 | |||
| 98 | int tot = 0; | ||
| 99 | |||
| 100 | pair i(0, 0); | ||
| 101 | for (i.first = 0; i.first < board.N; i.first++) { | ||
| 102 | for (i.second = 0; i.second < board.M; i.second++) { | ||
| 103 | if (board[i] == 0) { | ||
| 104 | tot += reachable9(i, board); | ||
| 105 | } | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | cout << tot << endl; | ||
| 110 | |||
| 111 | return 0; | ||
| 112 | } | ||
