summaryrefslogtreecommitdiff
path: root/Advent-of-Code-2021/AOC-13/main.c
blob: 876dae74d535a9c769592bf7471eae9297d4d3f7 (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
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#if 0
#define PART_1
#else
#define PART_2
#endif

#if 0
#define FILE_PATH "example.txt"
#define RULES  2
int WIDTH  = 11;
int HEIGHT = 15;
#else
#define FILE_PATH "input.txt"
#define RULES  12
int WIDTH  = 1311;
int HEIGHT = 895;
#endif

#define BOARD_SZ ((WIDTH)*(HEIGHT))

int *board = NULL;
int rules_val[RULES] = {0};
int rules_axis[RULES] = {0}; // 0 is x; 1 is y

void parse()
{
    FILE *fp = fopen(FILE_PATH, "r");
    if(!fp) {
        fprintf(stderr, "Could not open file: %s", FILE_PATH);
        return;
    }

    board = malloc(BOARD_SZ*sizeof(int));

    char line[256];
    char *tok;
    int read_rules = -1;
    while(fgets(line, sizeof(line), fp) != NULL)
    {
        if(line[0] == '\n') {
            read_rules = 0;
        } else if(read_rules < 0) {
            tok = strtok(line, ",");
            int x = atoi(tok);
            tok = strtok(NULL, ",");
            int y = atoi(tok);

            assert(strtok(NULL, ",") == NULL);
            assert(x >= 0 && x < WIDTH);
            assert(y >= 0 && y < HEIGHT);
            board[y * WIDTH + x] = 1;
        } else {
            tok = strtok(line, "=");
            rules_axis[read_rules] = (tok[11] == 'y');
            tok = strtok(NULL, "=");
            rules_val[read_rules] = atoi(tok);
            read_rules++;
            assert(strtok(NULL, "=") == NULL);
        }
    }

    fclose(fp);
}

void fold(int rules_i)
{
    int *board_cpy = malloc(BOARD_SZ*sizeof(int));

    memcpy(board_cpy, board, BOARD_SZ*sizeof(int));
    free(board);

    int old_width = WIDTH;
    if(rules_axis[rules_i] == 0) WIDTH = (WIDTH-1)/2;
    else                         HEIGHT = (HEIGHT-1)/2;

    board = malloc(BOARD_SZ*sizeof(int));

    for(int i = 0; i < HEIGHT; i++)
    {
        for(int j = 0; j < WIDTH; j++)
        {
            int index = i * WIDTH + j;
            int bc_index = i * old_width + j;
            int mirror;

            if(rules_axis[rules_i] == 0) {
                mirror = i * old_width + (2*rules_val[rules_i] - j);
            } else {
                mirror = ((2*rules_val[rules_i] - i)
                    * old_width) + j;
            }

            board[index] =
                board_cpy[bc_index] +
                board_cpy[mirror];
        }
    }

    free(board_cpy);
}

void print_board()
{
    for(int i = 0; i < HEIGHT; i++)
    {
        for(int j = 0; j < WIDTH; j++)
        {
            if(board[i * WIDTH + j] == 0)
                putc('.', stdout);
            else
                putc('#', stdout);
        }
        putc('\n', stdout);
    }
}

void print_rules()
{
    for(int i = 0; i < RULES; i++)
    {
        char axis;
        if(rules_axis[i] == 0)  axis = 'x';
        else                    axis = 'y';
        printf("fold along %c=%d\n", axis, rules_val[i]);
    }
}

void part_1()
{
    parse();
    fold(0);

    int visible = 0;
    for(int i = 0; i < BOARD_SZ; i++)
    {
        if(board[i] > 0) visible++;
    }

    printf("%d dots are visible\n", visible);
}

void part_2()
{
    parse();
    for(int i = 0; i < RULES; i++)
        fold(i);

    print_board();
}

int main(void)
{

#ifdef PART_1
    part_1();
#endif

#ifdef PART_2
    part_2();
#endif

    free(board);
    return 0;
}