-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.d
92 lines (81 loc) · 2.08 KB
/
solution.d
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
import std.stdio, std.string, std.file, std.process;
void main()
{
auto grid = readText("./input.txt").splitLines();
auto rows = cast(int) grid.length;
auto columns = cast(int) grid[0].length;
if (environment.get("part") == "part2") {
int score = 0;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
int up = 0;
for (int y2 = y - 1; y2 >= 0; y2--) {
up++;
if (grid[y][x] <= grid[y2][x]) {
break;
}
}
int down = 0;
for (int y2 = y + 1; y2 < rows; y2++) {
down++;
if (grid[y][x] <= grid[y2][x]) {
break;
}
}
int left = 0;
for (int x2 = x - 1; x2 >= 0; x2--) {
left++;
if (grid[y][x] <= grid[y][x2]) {
break;
}
}
int right = 0;
for (int x2 = x + 1; x2 < columns; x2++) {
right++;
if (grid[y][x] <= grid[y][x2]) {
break;
}
}
auto current = up * left * right * down;
if (current > score) score = current;
}
}
writeln(score);
} else {
int count = 0;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < columns; x++) {
if (y == 0 || x == 0 || y == columns - 1 || x == rows - 1) {
count++;
} else {
bool findY(int start, int end) {
for (int y2 = start; y2 < end; y2++) {
if (grid[y][x] <= grid[y2][x]) {
return false;
}
}
return true;
}
bool findX(int start, int end) {
for (int x2 = start; x2 < end; x2++) {
if (grid[y][x] <= grid[y][x2]) {
return false;
}
}
return true;
}
if (
findY(0, y) ||
findY(y + 1, rows) ||
findX(0, x) ||
findX(x + 1, columns)
) {
count++;
continue;
}
}
}
}
writeln(count);
}
}