-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.c
157 lines (136 loc) · 4.23 KB
/
day08.c
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
#include "advent_of_code.h"
void solve_day08(AdventOfCode* aoc, Stream* file_stream) {
FuriString* line = furi_string_alloc();
int p1 = 0;
int p2 = 0;
FuriString* forest = furi_string_alloc();
size_t w = 0;
size_t h = 0;
while(true) {
if(!stream_read_line(file_stream, line)) break;
furi_string_trim(line);
w = furi_string_size(line);
h++;
furi_string_cat(forest, line);
}
size_t area = w * h;
bitset_t visible;
bitset_init(visible);
bitset_resize(visible, area);
array_int_t scores;
array_int_init(scores);
array_int_resize(scores, area);
for(size_t i = 0; i < area; i++) array_int_set_at(scores, i, 1);
char a, b, c;
int last_a[10];
int last_b[10];
for(size_t y = 0; y < h; y++) {
a = '0' - 1;
b = '0' - 1;
for(size_t i = 0; i < 10; i++) {
last_a[i] = 0;
last_b[i] = w - 1;
}
for(size_t x = 0; x < w; x++) {
size_t i = x + (y * w);
size_t j = (w - 1 - x) + (y * w);
c = furi_string_get_char(forest, i);
if(c > a) {
a = c;
if(!bitset_get(visible, i)) {
p1++;
bitset_set_at(visible, i, true);
}
}
int closest = w;
for(size_t k = c - '0'; k < 10; k++) {
int dst = x - last_a[k];
if(dst < closest) {
closest = dst;
if(dst <= 1) break;
}
}
last_a[c - '0'] = x;
*array_int_get(scores, i) *= closest;
c = furi_string_get_char(forest, j);
if(c > b) {
b = c;
if(!bitset_get(visible, j)) {
p1++;
bitset_set_at(visible, j, true);
}
}
closest = w;
for(size_t k = c - '0'; k < 10; k++) {
int dst = last_b[k] + 1 + x - w;
if(dst < closest) {
closest = dst;
if(dst <= 1) break;
}
}
last_b[c - '0'] = w - 1 - x;
*array_int_get(scores, j) *= closest;
}
}
for(size_t x = 0; x < w; x++) {
a = '0' - 1;
b = '0' - 1;
for(size_t i = 0; i < 10; i++) {
last_a[i] = 0;
last_b[i] = h - 1;
}
for(size_t y = 0; y < h; y++) {
size_t i = x + (y * w);
size_t j = x + ((h - 1 - y) * w);
c = furi_string_get_char(forest, i);
if(c > a) {
a = c;
if(!bitset_get(visible, i)) {
p1++;
bitset_set_at(visible, i, true);
}
}
int closest = h;
for(size_t k = c - '0'; k < 10; k++) {
int dst = y - last_a[k];
if(dst < closest) {
closest = dst;
if(dst <= 1) break;
}
}
last_a[c - '0'] = y;
*array_int_get(scores, i) *= closest;
c = furi_string_get_char(forest, j);
if(c > b) {
b = c;
if(!bitset_get(visible, j)) {
p1++;
bitset_set_at(visible, j, true);
}
}
closest = h;
for(size_t k = c - '0'; k < 10; k++) {
int dst = last_b[k] + 1 + y - h;
if(dst < closest) {
closest = dst;
if(dst <= 1) break;
}
}
last_b[c - '0'] = h - 1 - y;
*array_int_get(scores, j) *= closest;
}
}
for(size_t i = 0; i < area; i++) {
int score = *array_int_get(scores, i);
if(score > p2) p2 = score;
}
array_int_clear(scores);
bitset_clear(visible);
furi_string_free(forest);
FuriString* part_1 = furi_string_alloc_printf("%d", p1);
FuriString* part_2 = furi_string_alloc_printf("%d", p2);
update_results(aoc, part_1, part_2);
furi_string_free(part_2);
furi_string_free(part_1);
furi_string_free(line);
}