From 2eda69866422ca4b1cde46f47876e370a351d5ea Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sat, 14 Dec 2024 01:16:01 -0500 Subject: [PATCH] Oh tannenbaum --- c/2024/14/restroom_redoubt.c | 61 +++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 14 deletions(-) diff --git a/c/2024/14/restroom_redoubt.c b/c/2024/14/restroom_redoubt.c index a810886..e2d546e 100644 --- a/c/2024/14/restroom_redoubt.c +++ b/c/2024/14/restroom_redoubt.c @@ -9,32 +9,65 @@ #define HEIGHT 103 #define CYCLES 100 +typedef struct Robot { + int px; + int py; + int vx; + int vy; +} Robot; + int main() { char *line; int px, py, vx, vy; int q1 = 0, q2 = 0, q3 = 0, q4 = 0; + Robot robots[500]; + int robots_count = 0; while((line = aoc_read_line()) != NULL) { sscanf(line, "p=%d,%d v=%d,%d", &px, &py, &vx, &vy); - // printf("%s\n[%d, %d] [%d, %d]\n\n", line, px, py, vx, vy); - for (int i = 0; i < CYCLES; i++) { - px += vx; - px = (px + WIDTH) % WIDTH; - py += vy; - py = (py + HEIGHT) % HEIGHT; - } - printf("[%d, %d] [%d, %d]\n\n", px, py, vx, vy); - if (px < WIDTH / 2 && py < HEIGHT / 2) q1++; - if (px > WIDTH / 2 && py < HEIGHT / 2) q2++; - if (px < WIDTH / 2 && py > HEIGHT / 2) q3++; - if (px > WIDTH / 2 && py > HEIGHT / 2) q4++; + robots[robots_count].px = px; + robots[robots_count].py = py; + robots[robots_count].vx = vx; + robots[robots_count].vy = vy; + robots_count++; } - printf("%d %d %d %d\n", q1, q2, q3, q4); + char blank_map[((WIDTH + 1) * HEIGHT) + 1]; + for (int i = 0; i < (WIDTH + 1) * HEIGHT; i++) { + blank_map[i] = i % (WIDTH + 1) == WIDTH ? '\n' : '.'; + } + + char map[((WIDTH + 1) * HEIGHT) + 1]; + + int i; + for (i = 0; ; i++) { + strcpy(map, blank_map); + for (int j = 0; j < robots_count; j++) { + map[robots[j].px + (robots[j].py * (WIDTH + 1))] = 'X'; + robots[j].px += robots[j].vx; + robots[j].px = (robots[j].px + WIDTH) % WIDTH; + robots[j].py += robots[j].vy; + robots[j].py = (robots[j].py + HEIGHT) % HEIGHT; + } + if (i == 7502) printf("Cycle %d\n%s\n\n", i, map); + if (i == 7502) break; + + if (i == CYCLES - 1) { + for (int j = 0; j < robots_count; j++) { + Robot r = robots[j]; + if (r.px < WIDTH / 2 && r.py < HEIGHT / 2) q1++; + if (r.px > WIDTH / 2 && r.py < HEIGHT / 2) q2++; + if (r.px < WIDTH / 2 && r.py > HEIGHT / 2) q3++; + if (r.px > WIDTH / 2 && r.py > HEIGHT / 2) q4++; + } + } + } + + printf("Part 1: %lld\n", q1 * q2 * q3 * q4); - //printf("Part 2: %lld\n", cost_2); + printf("Part 2: %lld\n", i); aoc_free(); return 0;