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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#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/day12.ref";
// char filename[] = "in/day12.pzl";
#define BUF_LEN 10000
char file_buf[BUF_LEN];
s32 **grid;
s32 **visited;
s32 y_len;
s32 x_len;
s32 start_y;
s32 start_x;
s32 end_y;
s32 end_x;
s32 traverse(s32 steps,s32 y, s32 x)
{
if (PP(visited,y,x) < steps) {
return 42000;
}
PP(visited,y,x) = steps;
// if (PP(visited,y,x) == 1) {
// return 12345;
// }
// PP(visited,y,x) = 1;
if ((x==end_x) && (y==end_y)) {
return steps;
}
s32 min = 123456;
if ( (x+1 < x_len) && (PP(grid,y,x+1) <= (PP(grid,y,x)+1)) ) {
s32 a = traverse(steps+1, y, x+1);
if (a < min) {
min = a;
}
}
if ( (x > 0) && (PP(grid,y,x-1) <= (PP(grid,y,x)+1)) ) {
s32 a = traverse(steps+1, y, x-1);
if (a < min) {
min = a;
}
}
if ( (y+1 < y_len) && (PP(grid,y+1,x) <= (PP(grid,y,x)+1)) ) {
s32 a = traverse(steps+1, y+1, x);
if (a < min) {
min = a;
}
}
if ( (y > 0) && (PP(grid,y-1,x) <= (PP(grid,y,x)+1)) ) {
s32 a = traverse(steps+1, y-1, x);
if (a < min) {
min = a;
}
}
return min;
}
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';
y_len = count_char_in_string('\n', file_buf);
grid = malloc(y_len * sizeof(s32*));
visited = malloc(y_len * sizeof(s32*));
char *start = file_buf;
for (s32 y = 0; y < y_len; ++y) {
char *end = goto_char_in_string('\n', start);
x_len = end - start;
*(grid+y) = malloc(x_len * sizeof(s32*));
*(visited+y) = malloc(x_len * sizeof(s32*));
for (s32 x = 0; x < x_len; ++x) {
char c = *(start + x);
if (c == 'S') {
start_y = y;
start_x = x;
c = 'a';
} else if (c == 'E') {
end_y = y;
end_x = x;
c = 'z';
}
*(*(grid+y) + x) = c - 'a';
*(*(visited+y) + x) = 123456;
// *(*(visited+y) + x) = 0;
}
start = end+1;
}
printf("y_len: %d\n", y_len);
printf("x_len: %d\n", x_len);
printf("start y: %d\n", start_y);
printf("start x: %d\n", start_x);
printf("end y: %d\n", end_y);
printf("end x: %d\n", end_x);
s32 res1 = 0;
s32 res2 = 0;
res1 = traverse(0, start_y, start_x);
printf("res1: %d\n", res1);
printf("res2: %d\n", res2);
// asm("int3");
}
|