summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkaa <kaa@disroot.org>2025-06-18 03:12:30 -0700
committerkaa <kaa@disroot.org>2025-06-18 03:12:30 -0700
commitfc33c79577cf7d6b93898968319fe6b98ec61986 (patch)
treedbb4c622528010f0bd173c8b8136d987d93f57a8
parent2d0a03f26e0ff72aede3165516023f396f018f8f (diff)
Generic 2D graph
-rw-r--r--2024/10/1.c75
-rw-r--r--graph.h73
2 files changed, 76 insertions, 72 deletions
diff --git a/2024/10/1.c b/2024/10/1.c
index a8112a6..e1aee04 100644
--- a/2024/10/1.c
+++ b/2024/10/1.c
@@ -1,78 +1,14 @@
#include <stdio.h>
#include <stdbool.h>
-#include "../../d.h"
+#include "d.h"
#include "map.h"
+#include "graph.h"
bool debug = true;
-typedef struct {
- int width;
- int height;
- char** e;
- bool** visited;
-} Graph2D;
-enum {
- NORTH = 0, EAST, SOUTH, WEST, DIRECTIONS,
-};
-
-/* Reads from the input until exhausted. Returns memory which must be
-freed by the caller. */
-Graph2D*
-ingest(FILE* in)
-{
- char *t = getall(in);
- char *s = t;
-
- /* Two passes. One to count width and height.
- Another to store to allocated memory. */
- int height = 0, width = -1, i = 0;
- while (s[i] != '\0') {
- if (s[i] == '\n') {
- if (width == -1) {
- width = i + 1;
- }
- height++;
- }
- i++;
- }
- char** buffer = calloc(height, sizeof(char*));
- bool** visited = calloc(height, sizeof(bool*));
-
- Graph2D* g = calloc(1, sizeof(Graph2D));
- g->width = width;
- g->height = height;
- g->e = buffer;
- g->visited = visited;
-
- i = height;
- while (i > 0) {
- buffer[i-1] = calloc(width, sizeof(char));
- visited[i-1] = calloc(width, sizeof(bool));
- i--;
- }
-
- i = 0;
- int j = 0;
- while (i < height) {
- if (j < width - 1) {
- buffer[i][j] = *s;
- j++;
- } else {
- i++;
- j = 0;
- }
- s++;
- }
- free(t);
- return g;
-}
-typedef struct {
- int x;
- int y;
-} Coordinates;
typedef struct Stack {
void* value;
@@ -94,12 +30,6 @@ push(Stack* s, void* value) {
return t;
}
-bool
-valid(Graph2D* g, Coordinates c)
-{
- return c.x >= 0 && c.x < g->width && c.y >= 0 && c.y < g->height;
-}
-
int score = 0;
void
travel(Graph2D* g, Coordinates c)
@@ -152,6 +82,7 @@ main()
Graph2D* g = ingest(in);
+ hashmap* map = hashmap_create();
for (int i = 0; i < g->height; i++) {
for (int j = 0; j < g->width; j++) {
if (g->e[i][j] == '0') {
diff --git a/graph.h b/graph.h
new file mode 100644
index 0000000..0b6c5c2
--- /dev/null
+++ b/graph.h
@@ -0,0 +1,73 @@
+typedef struct {
+ int width;
+ int height;
+ char** e;
+ bool** visited;
+} Graph2D;
+
+enum {
+ NORTH = 0, EAST, SOUTH, WEST, DIRECTIONS,
+};
+
+typedef struct {
+ int x;
+ int y;
+} Coordinates;
+
+bool
+valid(Graph2D* g, Coordinates c)
+{
+ return c.x >= 0 && c.x < g->width && c.y >= 0 && c.y < g->height;
+}
+
+/* Reads from the input until exhausted. Returns memory which must be
+freed by the caller. */
+Graph2D*
+ingest(FILE* in)
+{
+ char *t = getall(in);
+ char *s = t;
+
+ /* Two passes. One to count width and height.
+ Another to store to allocated memory. */
+ int height = 0, width = -1, i = 0;
+ while (s[i] != '\0') {
+ if (s[i] == '\n') {
+ if (width == -1) {
+ width = i + 1;
+ }
+ height++;
+ }
+ i++;
+ }
+ char** buffer = calloc(height, sizeof(char*));
+ bool** visited = calloc(height, sizeof(bool*));
+
+ Graph2D* g = calloc(1, sizeof(Graph2D));
+ g->width = width;
+ g->height = height;
+ g->e = buffer;
+ g->visited = visited;
+ i = height;
+ while (i > 0) {
+ buffer[i-1] = calloc(width, sizeof(char));
+ visited[i-1] = calloc(width, sizeof(bool));
+ i--;
+ }
+
+ i = 0;
+ int j = 0;
+ while (i < height) {
+ if (j < width - 1) {
+ buffer[i][j] = *s;
+ j++;
+ } else {
+ i++;
+ j = 0;
+ }
+ s++;
+ }
+
+ free(t);
+ return g;
+}