summaryrefslogtreecommitdiff
path: root/2020/11/part2.c
blob: 2443ddd4ff5171a56210bfb22c31e24f118c7df9 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#define MAX_INPUT_WIDTH 128
#define MAX_INPUT_HEIGTH 128
#define MAX_INPUT MAX_INPUT_WIDTH * MAX_INPUT_HEIGTH

int count_char(const char *source, char search)
{
    int oc = 0;
    do {
        oc += *source == search;
    } while (*++source);

    return oc;
}

void replace_char(char *destination, const char *source, char original, char replace)
{
    do {
        if (*source == original) {
            *destination = replace;
        }
        else {
            *destination = *source;
        }
    } while (++destination && *++source);
    *destination = 0;
}

char get_char(const char *source, size_t width, size_t y, size_t x) {
    return source[y * width + x];
}

int sur_oc_seats(const char *source, size_t width, size_t height, size_t index_y, size_t index_x)
{
    int num = 0;
    // Check upwards
    size_t y = index_y;
    size_t x = index_x;
    while (--y < height) {
        char c = get_char(source, width, y, x);
        if (c == '.') {
            continue;
        }

        num += c == '#';
        break;
    }

    // Check downwards
    y = index_y;
    while (++y < height) {
        char c = get_char(source, width, y, x);
        if (c == '.') {
            continue;
        }

        num += c == '#';
        break;
    }

    // Check left
    y = index_y;
    x = index_x;
    while (--x < width) {
        char c = get_char(source, width, y, x);
        if (c == '.') {
            continue;
        }

        num += c == '#';
        break;
    }

    // Check right
    x = index_x;
    while (++x < width) {
        char c = get_char(source, width, y, x);
        if (c == '.') {
            continue;
        }

        num += c == '#';
        break;
    }

    // Check left upwards
    y = index_y;
    x = index_x;
    while (--y < height && --x < width) {
        char c = get_char(source, width, y, x);
        if (c == '.') {
            continue;
        }

        num += c == '#';
        break;
    }

    // Check left downwards
    y = index_y;
    x = index_x;
    while (++y < height && --x < width) {
        char c = get_char(source, width, y, x);
        if (c == '.') {
            continue;
        }

        num += c == '#';
        break;
    }

    // Check right upwards
    y = index_y;
    x = index_x;
    while (--y < height && ++x < width) {
        char c = get_char(source, width, y, x);
        if (c == '.') {
            continue;
        }

        num += c == '#';
        break;
    }

    // Check right downwards
    y = index_y;
    x = index_x;
    while (++y < height && ++x < width ) {
        char c = get_char(source, width, y, x);
        if (c == '.') {
            continue;
        }

        num += c == '#';
        break;
    }

    return num;
}

int solve(const char *filename)
{
    FILE *file = fopen(filename, "r");

    // Include space for newline and string terminator
    char buffer[128] = { 0 };

    char input[MAX_INPUT] = { 0 };
    size_t height = 1;
    fgets(buffer, 128, file);
    size_t width = strchr(buffer, '\n') - buffer;
    replace_char(input, buffer, 'L', '#');

    while (fgets(buffer, 128, file)) {
        // Round 1
        replace_char(&input[height++ * width], buffer, 'L', '#');
    }

    fclose(file);

    size_t size = height * width;

    // Zero terminate
    input[size] = 0;

    char input2[MAX_INPUT] = { 0 };
    do {
        for (size_t i = 0; i < size; i++) {
            if (input[i] == '.') {
                input2[i] = '.';
            }
            else if (input[i] == 'L') {
                if (sur_oc_seats(input, width, height, i / width, i % width) == 0) {
                    input2[i] = '#';
                }
                else {
                    input2[i] = 'L';
                }
            }
            else if (input[i] == '#') {
                if (sur_oc_seats(input, width, height, i / width, i % width) >= 5) {
                    input2[i] = 'L';
                }
                else {
                    input2[i] = '#';
                }
            }
        }
    } while (memcmp(input, input2, size) && memcpy(input, input2, size));

    return count_char(input, '#');
}

int main(int argc, char *argv[])
{
    printf("%i\n", solve(argv[argc - 1]));
}