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
|
#include "common.h"
namespace aoc2022 {
struct trees {
static const int grid = 99;
struct pos {
int x;
int y;
};
char ts[grid * grid] = {0};
void load(int r, line_view lv) {
for(int i = 0; i < grid; i++) {
ts[r * grid + i] = *(lv.line + i) - '0';
// printf("%d %d -> %d\n", i, r, ts[r * grid + i]);
}
}
void print() {
for(int y = 0; y < grid; y++) {
for (int x = 0; x < grid; x++) {
printf("%d", height({x, y}));
}
printf("\n");
}
}
enum direction {
top,
left,
bottom,
right,
};
int height(pos p) {
return ts[p.y* grid + p.x];
}
bool valid(pos p) {
return p.x < grid && p.x >= 0 && p.y < grid && p.y >= 0;
}
pos next(direction d, pos p) {
switch (d) {
case top : return {p.x, p.y - 1};
case left : return {p.x - 1, p.y};
case bottom : return {p.x, p.y + 1};
case right : return {p.x + 1, p.y};
}
return {-1, -1};
}
int score(pos p) {
direction ds[4] = {top, left, bottom, right};
int s[4] = {0, 0, 0, 0};
for (int i = 0; i < 4; i++) {
auto x = next(ds[i], p);
while(valid(x)) {
s[i] += 1;
if (height(p) <= height(x)) {
break;
}
x = next(ds[i], x);
}
}
// printf("[%d, %d](%d) has score [%d, %d, %d, %d]\n",
// p.x, p.y, height(p), s[0], s[1], s[2], s[3]);
return s[0] * s[1] * s[2] * s[3];
}
bool visiable(pos p) {
direction ds[4] = {top, left, bottom, right};
bool visiable[4] = {true, true, true, true};
// const char* literal[4] = {"top", "left", "bottom", "right"};
for (int i = 0; i < 4; i++) {
auto x = next(ds[i], p);
while(valid(x)) {
if (height(x) >= height(p)) {
// printf("(%d,%d) %d is not visiable from [%s]\n", p.x, p.y, height(p), literal[i]);
visiable[i] = false;
break;
}
x = next(ds[i], x);
}
}
for (bool b: visiable) {
if (b) return true;
}
return false;
}
};
std::pair<int,int> day8(line_view file);
}
|