summaryrefslogtreecommitdiff
path: root/2022/Cpp/Day8/main.cpp
blob: 1fd440adb03efe4eea5331e1bbfcd26e079d4c34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <bits/stdc++.h>
#include <sys/types.h>
using namespace std;

using ll = long long;
using pi = pair<int, int>;
using vpi = vector<pi>;
using vi = vector<int>;
using vll = vector<long long>;
using mpii = map<int, int>;
using mpll = map<ll, ll>;
using db = long double;

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define lb lower_bound
#define ub upper_bound

const int MOD = (int)1e9 + 7;
const db PI = acos((db)-1);
const int dx[4]{1, 0, -1, 0};
const int dy[4]{0, 1, 0, -1};

// pretty printing
template <typename K, typename V> void printm(const map<K, V> &mp) {
  cerr << "{" << endl;
  for (auto p : mp) {
    cerr << "  { " << p.first << " : " << p.second << " }\n";
  }
  cerr << "}" << endl;
}
template <typename T> void printv(const vector<T> &v) {
  cerr << "[";
  for (int i = 0; i < v.size(); i++) {
    if (i == v.size() - 1) {
      cerr << v[i];
    } else {
      cerr << v[i] << ", ";
    }
  }
  cerr << "]\n";
}

template <typename T> void printvv(const vector<vector<T>> &v) {
  cerr << "[\n";
  for (auto &vec : v) {
    cout << "  ";
    printv(vec);
  }
  cerr << "]\n";
}
void print() { cerr << "\n"; }

template <typename T, typename... TS> void print(T val, TS... vals) {
  cerr << val << " ";
  print(vals...);
}

/* stuff you should look for:
  ---------------------------
   * special cases (n=1?)
   * int overflow, array bounds
   * do smth instead of nothing and stay organized
   * WRITE STUFF DOWN
   * DON'T GET STUCK ON ONE APPROACH
 */

#define u8 uint8_t

struct P2 {
  int x;
  int y;
};

bool isValidIdx(P2 p, int r, int c) {
  return p.x >= 0 && p.y >= 0 && p.x < r && p.y < c;
}

array<int ,4> calcScenicScore(P2 p, vector<vector<int>> &trees) {
  std::array<int, 4> res{1, 1, 1, 1};
  int r = trees.size();
  int c = trees[0].size();

  // down
  for (int i = p.x + 1; i < r; i++) {
    if (trees[i][p.y] >= trees[p.x][p.y] || i == r - 1) {
      res[0] = abs(p.x - i);
      break;
    }
  }
  // up
  for (int i = p.x - 1; i >= 0; i--) {
    if (trees[i][p.y] >= trees[p.x][p.y] || i == 0) {
      res[1] = abs(p.x - i);
      break;
    }
  }

  // right
  for (int i = p.y + 1; i < c; i++) {
    if (trees[p.x][i] >= trees[p.x][p.y] || i == r - 1) {
      res[2] = abs(p.y - i);
      break;
    }
  }

  // left
  for (int i = p.y - 1; i >= 0; i--) {
    if (trees[p.x][i] >= trees[p.x][p.y] || i == 0) {
      res[3] = abs(p.y - i);
      break;
    }
  }

  return res;
}

bool isVisibile(P2 p, vector<vector<int>> &trees) {
  std::array<bool, 4> ans{true, true, true, true};
  int r = trees.size();
  int c = trees[0].size();
  // down
  for (int i = p.x + 1; i < r; i++) {
    if (trees[i][p.y] >= trees[p.x][p.y]) {
      ans[0] = false;
      break;
    }
  }
  // up
  for (int i = p.x - 1; i >= 0; i--) {
    if (trees[i][p.y] >= trees[p.x][p.y]) {
      ans[1] = false;
      break;
    }
  }

  // right
  for (int i = p.y + 1; i < c; i++) {
    if (trees[p.x][i] >= trees[p.x][p.y]) {
      ans[2] = false;
      break;
    }
  }

  // left
  for (int i = p.y - 1; i >= 0; i--) {
    if (trees[p.x][i] >= trees[p.x][p.y]) {
      ans[3] = false;
      break;
    }
  }

  for (int i = 0; i < 4; i++) {
    cout << boolalpha << ans[i] << " ";
  }
  cout << endl;
  bool res = 0;
  for (int i = 0; i < 4; i++) {
    res |= ans[i];
  }
  return res;
}

int mulArray(array<int, 4> arr) {
  int ans = 1;
  for (auto x: arr) {
    ans *= x;
  }
  return ans;
}

void solve_part_one() {
  string s;
  vector<vector<int>> trees;
  vector<int> row;
  while (cin >> s) {
    for (auto ch : s) {
      row.pb(ch - '0');
    }
    trees.pb(row);
    row.clear();
  }

  int r = trees.size();
  int c = trees[0].size();
  int ans = 2 * r + 2 * c - 4;
  cout << ans << endl;

  for (int x = 1; x < r - 1; x++) {
    for (int y = 1; y < c - 1; y++) {
      if (isVisibile(P2 { x, y }, trees)) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}

void solve_part_two() {
  string s;
  vector<vector<int>> trees;
  vector<int> row;
  while (cin >> s) {
    for (auto ch : s) {
      row.pb(ch - '0');
    }
    trees.pb(row);
    row.clear();
  }

  int r = trees.size();
  int c