summaryrefslogtreecommitdiff
path: root/2022/day12.c
diff options
context:
space:
mode:
authornekineki <nekineki@nekineki.net>2022-12-12 15:31:28 +0100
committernekineki <nekineki@nekineki.net>2022-12-12 15:31:28 +0100
commit45183abd7924f2d01aac0df10c012d564a94cf91 (patch)
treecce02dbd03d0acc1078148e3553e3feb57e0839e /2022/day12.c
parent1732724c991604703a3bf7811789a426f1bcb750 (diff)
day12
Diffstat (limited to '2022/day12.c')
-rw-r--r--2022/day12.c175
1 files changed, 175 insertions, 0 deletions
diff --git a/2022/day12.c b/2022/day12.c
new file mode 100644
index 0000000..14b92c8
--- /dev/null
+++ b/2022/day12.c
@@ -0,0 +1,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");
+}
+