commit 5a036344c64394553c3a3383fc09808ba02aa44b
parent b2215616c2ba2f386f48f8714beeaa034072cd6a
Author: bsandro <brian.drosan@gmail.com>
Date: Tue, 14 Dec 2021 00:05:02 +0200
Day 13, puzzle 2
Diffstat:
2 files changed, 36 insertions(+), 16 deletions(-)
diff --git a/day13/main.c b/day13/main.c
@@ -2,7 +2,7 @@
#include <time.h>
#include <string.h>
-void puzzle(const char *filename, long long *res1, long long *res2);
+void puzzle(const char *filename);
int main(int argc, char *argv[]) {
printf("Advent of Code: day 13\n");
@@ -14,13 +14,7 @@ int main(int argc, char *argv[]) {
}
const char *filename = argv[1];
- long long counter1 = -1;
- long long counter2 = -1;
-
- puzzle(filename, &counter1, &counter2);
-
- printf("Puzzle #1: %lld\n", counter1);
- printf("Puzzle #2: %lld\n", counter2);
+ puzzle(filename);
double elapsed = clock() - time_start;
printf("Elapsed: %f\n", elapsed / CLOCKS_PER_SEC);
diff --git a/day13/puzzle.c b/day13/puzzle.c
@@ -31,7 +31,7 @@ struct point_t flip_point(const struct point_t point, const struct point_t fold)
void add_point(struct array_t *points, struct point_t point);
void draw_points(const struct array_t *points);
-void puzzle(const char *filename, long long *result1, long long *result2) {
+void puzzle(const char *filename) {
FILE *infile = fopen(filename, "r");
if (infile == NULL) {
fprintf(stderr, "fopen() error: %s\n", strerror(errno));
@@ -41,9 +41,6 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
char buf[STR_LEN] = {0};
unsigned int line_num = 0;
- *result1 = 0;
- *result2 = 0;
-
struct array_t points = { .data = NULL };
array_init(&points, sizeof(struct point_t), 10);
struct array_t folds = { .data = NULL };
@@ -71,16 +68,14 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
array_init(&points_folded, sizeof(struct point_t), 10);
struct point_t fold = ((struct point_t *)folds.data)[f];
- //printf("fold x: %d, y: %d\n", fold.x, fold.y);
for (size_t i = 0; i < points.count; ++i) {
const struct point_t *data = points.data;
add_point(&points_folded, flip_point(data[i], fold));
}
- //printf("points: %zu\n", points_folded.count);
if (f == 0) {
- *result1 = points_folded.count;
+ printf("Puzzle #1: %zu\n", points_folded.count);
}
// copy folded points over old ones
@@ -90,6 +85,9 @@ void puzzle(const char *filename, long long *result1, long long *result2) {
points.count = points_folded.count;
}
+ printf("Puzzle #2:\n");
+ draw_points(&points);
+
// mutiny! ignoring feof/ferror.
fclose(infile);
}
@@ -101,7 +99,6 @@ struct point_t flip_point(const struct point_t point, const struct point_t fold)
} else if (fold.y > 0 && point.y > fold.y) {
p1.y = 2 * fold.y - point.y;
}
- //printf("%2d,%2d -> %2d,%2d\n", point.x, point.y, p1.x, p1.y);
return p1;
}
@@ -189,3 +186,32 @@ void print_points(struct array_t *points) {
printf("x: %2d, y: %2d\n", data[i].x, data[i].y);
}
}
+
+void draw_points(const struct array_t *points) {
+ assert(points != NULL);
+ const struct point_t *data = points->data;
+ int x_max = 0;
+ int y_max = 0;
+ for (size_t i = 0; i < points->count; ++i) {
+ x_max = data[i].x > x_max ? data[i].x : x_max;
+ y_max = data[i].y > y_max ? data[i].y : y_max;
+ }
+ x_max += 1;
+ y_max += 1;
+
+ char *buffer = malloc(x_max * y_max);
+ memset(buffer, ' ', x_max * y_max);
+
+ for (size_t i = 0; i < points->count; ++i) {
+ buffer[data[i].y * x_max + data[i].x] = '#';
+ }
+
+ for (int y = 0; y < y_max; ++y) {
+ for (int x = 0; x < x_max; ++x) {
+ printf("%c", buffer[y*x_max + x]);
+ }
+ printf("\n");
+ }
+
+ free(buffer);
+}