summaryrefslogtreecommitdiff
path: root/2024/10/1.c
blob: 315dd55f7cdd19cd58201dec825658cc274b3c89 (plain)
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
#include <stdio.h>
#include <stdbool.h>
#include "d.h"
#include "map.h"
#include "graph.h"

bool debug = true;

/* This is the stuff of ideas.
typedef struct Stack {
	void*		value;
	struct Stack*	below;
} Stack;

Stack*
pop(Stack* s) {
	free(s->value);
	Stack* tmp = s->below;
	free(s);
	return tmp;
}
Stack*
push(Stack* s, void* value) {
	Stack* t = calloc(1, sizeof(Stack));
	t->below = s;
	t->value = value;
	return t;
} */

// This global will die.
int score = 0;
void
travel(Graph2D* g, Coordinates c)
{
	// Utility TBD
	// g->visited[c.y][c.x] = true;

	if (debug) {
		printf("%d,%d\n", c.x, c.y);
		printf("%c\n", g->e[c.y][c.x]);
	}

	Coordinates cardinal[DIRECTIONS];
	cardinal[SOUTH] = (Coordinates) {c.x, c.y + 1};
	cardinal[NORTH] = (Coordinates) {c.x, c.y - 1};
	cardinal[EAST] = (Coordinates) {c.x + 1, c.y};
	cardinal[WEST] = (Coordinates) {c.x - 1, c.y};

	if (g->e[c.y][c.x] == '9') {
		score++;
		return;
	}
	for (int i = 0; i < DIRECTIONS; i++) {
		if (graph_valid(g, cardinal[i])) {
			if (debug) {
				printf("%d,%d\n", cardinal[i].x, cardinal[i].y);
				printf("%d\n", g->e[cardinal[i].y][cardinal[i].x]);
				printf("-\n");
				printf("%d\n", g->e[c.y][c.x]);
				printf("%d\n", g->e[cardinal[i].y][cardinal[i].x] - g->e[c.y][c.x]);
			}

			if (g->e[cardinal[i].y][cardinal[i].x] - g->e[c.y][c.x] == 1) {
				if (debug) {
					printf("Valid: %d,%d\n", cardinal[i].x, cardinal[i].y);
				}
				travel(g, cardinal[i]);
			}
		}
	}
	return;
}

int
main()
{
	FILE* in = fopen("sample_small", "r");
	if (in == NULL) {
		exit(1);
	}

	Graph2D* g = graph_ingest(in);

	/* Hash map from Coordinates of each starting point to their scores.
		Pass to each travel, along with launch coordinates. */
	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') {
				travel(g, (Coordinates) {j, i});
				// Score is a global
				printf("%d\n", score);
			}
		}
	}

	return 0;
}