aboutsummaryrefslogtreecommitdiff
path: root/2024/20/day20a.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2024-12-20 07:06:29 +0100
committerSebastiano Tronto <sebastiano@tronto.net>2024-12-20 07:06:29 +0100
commitecb8bdb0b63536916a3a3d55fd4ebbf7aeb21997 (patch)
treeb2a2c1e3b8eaa69b1ad7e7b07be826671ce428e7 /2024/20/day20a.cpp
parent3c8331701f9f77c28847d678b935d5efb9a5d575 (diff)
downloadaoc-ecb8bdb0b63536916a3a3d55fd4ebbf7aeb21997.tar.gz
aoc-ecb8bdb0b63536916a3a3d55fd4ebbf7aeb21997.zip
Day 20 2024
Diffstat (limited to '2024/20/day20a.cpp')
-rw-r--r--2024/20/day20a.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/2024/20/day20a.cpp b/2024/20/day20a.cpp
new file mode 100644
index 0000000..8837de7
--- /dev/null
+++ b/2024/20/day20a.cpp
@@ -0,0 +1,108 @@
1#include <algorithm>
2#include <cstdint>
3#include <iostream>
4#include <map>
5#include <queue>
6#include <ranges>
7#include <set>
8#include <sstream>
9#include <string>
10#include <string_view>
11#include <vector>
12using namespace std;
13
14class Map {
15public:
16 int N, M, L;
17 int *cell;
18
19 Map(const vector<string>& lines)
20 : N{(int)lines.size()}, M{(int)lines[0].size()}, cell{new int[M*N]}
21 {
22 for (int i = 0; i < N; i++) {
23 for (int j = 0; j < N; j++) {
24 cell[M*i+j] = lines[i][j] == '#' ? -1 : 0;
25 if (lines[i][j] == 'S') {
26 is = i;
27 js = j;
28 }
29 if (lines[i][j] == 'E') {
30 ie = i;
31 je = j;
32 }
33 }
34 }
35 findpath();
36 }
37
38 ~Map() {
39 delete[] cell;
40 }
41
42 int& operator()(int i, int j) {
43 if (i < 0 || i >= N || j < 0 || j >= M)
44 return out_of_bound;
45 return cell[M*i+j];
46 }
47
48 const int& operator()(int i, int j) const {
49 if (i < 0 || i >= N || j < 0 || j >= M)
50 return out_of_bound;
51 return cell[M*i+j];
52 }
53
54 int cheat(int i, int j) {
55 if ((*this)(i, j) != -1)
56 return -1;
57 if ((*this)(i+1, j) >= 0 && (*this)(i-1, j) >= 0)
58 return abs((*this)(i+1, j) - (*this)(i-1, j)) - 2;
59 if ((*this)(i, j+1) >= 0 && (*this)(i, j-1) >= 0)
60 return abs((*this)(i, j+1) - (*this)(i, j-1)) - 2;
61 return -1;
62 }
63private:
64 int is, js, ie, je;
65 int out_of_bound = -1;
66 vector<pair<int, int>> directions {{0,1}, {0,-1}, {1,0}, {-1,0}};
67
68 void findpath() {
69 int i, j, k;
70 for (i = is, j = js, k = 1; i != ie || j != je; step(i, j, k))
71 (*this)(i, j) = k;
72 (*this)(ie, je) = k;
73 L = k-1;
74 }
75
76 void step(int& i, int& j, int& k) {
77 k++;
78 for (auto p : directions) {
79 if ((*this)(i+p.first, j+p.second) == 0) {
80 i = i+p.first;
81 j = j+p.second;
82 return;
83 }
84 }
85 cout << "Error! at " << i << ", " << j << endl;
86 }
87};
88
89int main() {
90 string line;
91 vector<string> lines;
92 while (getline(cin, line))
93 lines.push_back(line);
94 Map m(lines);
95
96 int count = 0;
97 for (int i = 0; i < m.N; i++) {
98 for (int j = 0; j < m.M; j++) {
99 int c = m.cheat(i, j);
100 if (c >= 100)
101 count++;
102 }
103 }
104
105 cout << count << endl;
106
107 return 0;
108}

Generated with cgit - Back to sebastiano.tronto.net