summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2022/aoc-12/main.c
blob: 1511059c9d650d081129df6ba11ff37906a24617 (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 1
  #define FILENAME "sample.txt"
  #define WIDTH  8
  #define HEIGHT 5
#else
  #define FILENAME "input.txt"
#endif

typedef struct node_t {
    char val;
    struct node_t *neighbours[4]; // up, down, left, right
} node_t;

node_t graph[WIDTH][HEIGHT];

void part1();
int char_index(char ch);
int in_range(char a, char b);
void print_graph();

void part1()
{
    print_graph();
}

void gen_graph(char table[WIDTH][HEIGHT])
{
    for(int i = 0; i < HEIGHT; i++)
        for(int j = 0; j < WIDTH; j++) {
            for(int k = 0; k < 4; k++) graph[j][i].neighbours[k] = NULL;
            graph[j][i].val = table[j][i];

            // up
            if(i > 0)
                if(in_range(table[j][i], table[j][i-1]))
                    graph[j][i].neighbours[0] = &(graph[j][i-1]);
            // down
            if(i < HEIGHT - 1) {
                if(in_range(table[j][i], table[j][i+1]))
                    graph[j][i].neighbours[1] = &(graph[j][i+1]);
            }
            // left
            if(j > 0) {
                if(in_range(table[i][j], table[j-1][i]))
                    graph[j][i].neighbours[2] = &(graph[j-1][i]);
            }
            // right
            if(j < WIDTH - 1) {
                if(in_range(table[j][i], table[j+1][i]))
                    graph[j][i].neighbours[3] = &(graph[j+1][i]);
            }
        }
}

void parse()
{
    FILE *fp = fopen(FILENAME, "r");
    if(!fp) {
        fprintf(stderr, "ERROR: Could not open file: %s\n", FILENAME);
        exit(1);
    }

    char table[WIDTH][HEIGHT] = {0};

    char line[64];
    for(int i = 0; fgets(line, sizeof(line), fp) != NULL && i < HEIGHT; i++)
        for(int j = 0; j < WIDTH; j++)
            table[j][i] = char_index(line[j]);

    gen_graph(table);
    fclose(fp);
}

int main(void)
{
    parse();
    part1();
    return 0;
}

void print_graph()
{
    for(int i = 0; i < HEIGHT; i++) {
        for(int j = 0; j < WIDTH; j++) {
            printf("(%2d %2d) %c -> ", j, i, graph[j][i].val + '`');
            for(int k = 0; k < 4; k++)
                printf("%c ", (graph[j][i].neighbours[k] == NULL) ? '_' :
                       graph[j][i].neighbours[k]->val + '`');

            printf("\n");
        }
        printf("\n");
    }
}

int char_index(char ch)
{
    if(ch == 'S') ch = '`';
    else if(ch == 'E') ch = '{';

    return ch - '`';
}

int in_range(char a, char b)
{
    if(a == b || a == b-1 || a == b+1)
        return 1;
    return 0;
}