-
Notifications
You must be signed in to change notification settings - Fork 0
/
day8.c3
149 lines (140 loc) · 3.01 KB
/
day8.c3
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
module day8;
import std::collections::list;
import std::io;
distinct Forest = inline List(<String>);
fn Forest* parse_forest()
{
File f = file::open("forest.txt", "rb")!!;
defer (void)f.close();
Forest* forest = mem::alloc(Forest);
forest.new_init(512);
while (!f.eof())
{
String line = io::treadline(&f)!!;
assert(line.len);
forest.push(line);
}
return forest;
}
fn int Forest.is_visible(Forest* this, int x, int y)
{
if (x == 0 || y == 0) return 5;
isz last_y = this.len() - 1;
if (y == last_y) return 5;
isz last_x = this.get(0).len - 1;
if (x == last_x) return 5;
String row = this.get(y);
char height = row[x];
int directions_seen_from = 0;
do FROM_WEST: {
for (isz fx = 0; fx < x; fx++)
{
if (row[fx] >= height) break FROM_WEST;
}
return 1;
};
do FROM_EAST: {
for (isz fx = last_x; fx > x; fx--)
{
if (row[fx] >= height) break FROM_EAST;
}
return 2;
};
do FROM_NORTH: {
for (isz fy = 0; fy < y; fy++)
{
if (this.get(fy)[x] >= height) break FROM_NORTH;
}
return 3;
};
do FROM_SOUTH: {
for (isz fy = last_y; fy > y; fy--)
{
if (this.get(fy)[x] >= height) break FROM_SOUTH;
}
return 4;
};
return 0;
}
fn int Forest.scenic_score(Forest* this, int x, int y)
{
isz last_y = this.len() - 1;
isz last_x = this.get(0).len - 1;
String row = this.get(y);
char height = row[x];
int scenic_score;
int score = 0;
for (int fx = x - 1; fx >= 0; fx--)
{
score++;
if (row[fx] >= height) break;
}
scenic_score = score;
score = 0;
for (isz fx = (isz)x + 1; fx <= last_x; fx++)
{
score++;
if (row[fx] >= height) break;
}
scenic_score *= score;
score = 0;
for (isz fy = (isz)y - 1; fy >= 0; fy--)
{
score++;
if (this.get(fy)[x] >= height) break;
}
scenic_score *= score;
score = 0;
for (isz fy = (isz)y + 1; fy <= last_y; fy++)
{
score++;
if (this.get(fy)[x] >= height) break;
}
return scenic_score * score;
}
fn int Forest.visible_from_outside(Forest* forest)
{
int width = forest.get(0).len;
int height = (int)forest.len();
int sum;
for (int y = 0; y < height; y++)
{
String line = forest.get(y);
assert(line.len == width);
for (int x = 0; x < width; x++)
{
int seen = forest.is_visible(x, y);
if (seen > 0) sum++;
}
}
return sum;
}
fn int Forest.best_scenic_score(Forest* forest)
{
int width = forest.get(0).len;
int height = (int)forest.len();
int best_scenic_score;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int score = forest.scenic_score(x, y);
if (score > best_scenic_score) best_scenic_score = score;
}
}
return best_scenic_score;
}
fn void part1(Forest* forest)
{
io::printfn("Visible from outside: %d", forest.visible_from_outside());
}
fn void part2(Forest* forest)
{
io::printfn("Best scenic score: %d", forest.best_scenic_score());
}
fn void main()
{
Forest* forest = parse_forest();
part1(forest);
part2(forest);
}