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
|
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "util.h"
s32 reduce(s32 (*f)(s32 x, s32 y), s32 arr[], s32 len)
{
s32 ret = arr[0];
for (s32 i = 1; i < len; ++i) {
ret = f(ret, arr[i]);
}
return ret;
}
s32 mul(s32 x, s32 y)
{
return x * y;
}
s32 max(s32 x, s32 y)
{
return x > y ? x : y;
}
s32 count_char_in_string(char c, char *s)
{
s32 count = 0;
do
if (c == *s)
count++;
while (*s++);
return count;
}
char *goto_char_in_string(char c, char *s)
{
do
if (c == *s)
return s;
while (*s++);
return NULL;
}
#define PP(arr, y,x) *(*(arr+y)+x)
// char filename[] = "in/day08.ref";
char filename[] = "in/day08.pzl";
#define BUF_LEN 10000
char file_buf[BUF_LEN];
int main()
{
int fd = open(filename, O_RDONLY);
s32 data_len = read(fd, file_buf, LEN(file_buf));
if (data_len == -1) {
printf("error opening file\n");
exit(1);
} else if (data_len == LEN(file_buf)) {
printf("buffer probably not big enough\n");
exit(1);
}
file_buf[data_len] = '\0';
s32 y_len = count_char_in_string('\n', file_buf);
s32 x_len;
// printf("y_len: %d\n", y_len);
s32 **arr = malloc(y_len * sizeof(s32*));
s32 **flag = malloc(y_len * sizeof(s32*));
char *start = file_buf;
for (s32 i = 0; i < y_len; ++i) {
char *end = goto_char_in_string('\n', start);
x_len = end - start;
// printf("x_len: %d\n", x_len);
*(arr+i) = malloc(x_len * sizeof(s32*));
*(flag+i) = malloc(x_len * sizeof(s32*));
for (s32 j = 0; j < x_len; ++j) {
*(*(arr+i) + j) = *(start + j) - '0';
*(*(flag+i) + j) = 0;
}
start = end+1;
}
s32 res1 = 0;
s32 maxval;
#define RES1_CHECK() \
if (PP(arr, y, x) > maxval) { \
maxval = PP(arr, y, x); \
if (PP(flag, y, x) == 0) { \
PP(flag, y, x) = 1; \
res1 += 1; \
} \
}
for (s32 y = 0; y < y_len; ++y) {
maxval = -1;
for (s32 x = 0; x < x_len; ++x)
RES1_CHECK();
maxval = -1;
for (s32 x = x_len-1; x >= 0 ; --x)
RES1_CHECK();
}
for (s32 x = 0; x < x_len; ++x) {
maxval = -1;
for (s32 y = 0; y < y_len; ++y)
RES1_CHECK();
maxval = -1;
for (s32 y = y_len-1; y >= 0 ; --y)
RES1_CHECK();
}
s32 res2 = 0;
for (s32 y = 0; y < y_len; ++y) {
for (s32 x = 0; x < x_len; ++x) {
s32 rldu[4] = {0};
for (s32 r = x+1; r < x_len; ++r)
if ((PP(arr,y,r) >= PP(arr,y,x)) || (r==(x_len-1))) {
rldu[0] += r-x;
break;
}
for (s32 l = x-1; l >= 0; --l)
if ((PP(arr,y,l) >= PP(arr,y,x)) || (l==0)) {
rldu[1] += x-l;
break;
}
for (s32 d = y+1; d < y_len; ++d)
if ((PP(arr,d,x) >= PP(arr,y,x)) || (d==(y_len-1))) {
rldu[2] += d-y;
break;
}
for (s32 u = y-1; u >= 0; --u)
if ((PP(arr,u,x) >= PP(arr,y,x)) || (u==0)) {
rldu[3] += y-u;
break;
}
res2 = max(res2, reduce(mul, rldu, LEN(rldu)));
}
}
printf("res1: %d\n", res1);
printf("res2: %d\n", res2);
// asm("int3");
}
|