-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenhance.c
152 lines (126 loc) · 3.5 KB
/
enhance.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ALGO_LEN 512
#define MAX_SIZE 256
struct Image {
int width;
int height;
char data[MAX_SIZE * MAX_SIZE];
char empty;
};
#define BORDER 1
struct Image *enhance(const char *algo, const struct Image *old) {
struct Image *new = malloc(sizeof(struct Image));
new->width = old->width + (BORDER * 2);
if(new->width > MAX_SIZE) {
fprintf(stderr, "Image width exceeds maximum size of %d\n", MAX_SIZE);
exit(EXIT_FAILURE);
}
new->height = old->height + (BORDER * 2);
if(new->height > MAX_SIZE) {
fprintf(stderr, "Image height exceeds maximum size of %d\n", MAX_SIZE);
exit(EXIT_FAILURE);
}
for(int y = 0; y < new->height; ++y) {
for(int x = 0; x < new->width; ++x) {
int value = 0;
for(int dy = -1; dy <= +1; ++dy) {
int oy = (y - BORDER) + dy;
if((oy < 0) || (oy >= old->height)) {
value = (value * 2) + (old->empty == '#');
value = (value * 2) + (old->empty == '#');
value = (value * 2) + (old->empty == '#');
continue;
}
for(int dx = -1; dx <= +1; ++dx) {
int ox = (x - BORDER) + dx;
if((ox < 0) || (ox >= old->width)) {
value = (value * 2) + (old->empty == '#');
continue;
}
int offset = (oy * old->width) + ox;
value = (value * 2) + (old->data[offset] == '#');
}
}
int offset = (y * new->width) + x;
new->data[offset] = algo[value];
}
}
// Determine whether infinite pixels in new image are lit or unlit
new->empty = algo[old->empty == '.' ? 0 : ALGO_LEN-1];
return new;
}
int count_pixels(const struct Image *img) {
int count = 0;
for(int offset = 0; offset < img->width * img->height; ++offset) {
count += (img->data[offset] == '#');
}
return count;
}
void read_input(char **algo, struct Image **image) {
char buffer[1024];
if(fgets(buffer, sizeof(buffer), stdin) == NULL) {
perror("Unexpected end of input");
exit(EXIT_FAILURE);
}
size_t buflen = strlen(buffer) - 1; // Remove trailing newline
if(buflen != ALGO_LEN) {
fprintf(
stderr,
"Invalid enhancement algo length: got %lu bytes, expected %d\n",
buflen, ALGO_LEN
);
exit(EXIT_FAILURE);
}
*algo = malloc(ALGO_LEN);
memcpy(*algo, buffer, ALGO_LEN);
// Skip empty line between algo and image
if(fgets(buffer, sizeof(buffer), stdin) == NULL) {
fprintf(stderr, "Unexpected end of input\n");
exit(EXIT_FAILURE);
}
struct Image *img = malloc(sizeof(struct Image));
*image = img;
memset(img, 0, sizeof(struct Image));
img->empty = '.'; // Infinite pixels of starting image are un-lit
while(fgets(buffer, sizeof(buffer), stdin) != NULL) {
size_t buflen = strlen(buffer) - 1;
if(img->width != 0) {
if((int)buflen != img->width) {
fprintf(
stderr,
"Inconsistent image width: expected %d chars, got %lu on line %d\n",
img->width, buflen, (img->height + 1)
);
exit(EXIT_FAILURE);
}
} else {
img->width = buflen;
}
memcpy(img->data + (img->width * img->height), buffer, img->width);
img->height += 1;
}
}
#define PART_ONE 2
#define PART_TWO 50
int main(void) {
char *algo;
struct Image *image;
read_input(&algo, &image);
for(int step = 0; step < PART_ONE; ++step) {
struct Image *enhanced = enhance(algo, image);
free(image);
image = enhanced;
}
printf("Part1: %d\n", count_pixels(image));
for(int step = PART_ONE; step < PART_TWO; ++step) {
struct Image *enhanced = enhance(algo, image);
free(image);
image = enhanced;
}
printf("Part2: %d\n", count_pixels(image));
free(image);
free(algo);
return 0;
}