#include #include #include #include #include "../../lib/aoc.h" #define WIDTH 101 #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); robots[robots_count].px = px; robots[robots_count].py = py; robots[robots_count].vx = vx; robots[robots_count].vy = vy; robots_count++; } 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]; // Isn't it lovely? char *christmas_tree = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"; 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 (strstr(map, christmas_tree) != NULL) 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++; } } } // Uncomment this to print the pretty christmas tree // printf("%s\n", map); printf("Part 1: %d\n", q1 * q2 * q3 * q4); printf("Part 2: %d\n", i); aoc_free(); return 0; }