Oh tannenbaum
This commit is contained in:
parent
33c22748da
commit
2eda698664
@ -9,32 +9,65 @@
|
|||||||
#define HEIGHT 103
|
#define HEIGHT 103
|
||||||
#define CYCLES 100
|
#define CYCLES 100
|
||||||
|
|
||||||
|
typedef struct Robot {
|
||||||
|
int px;
|
||||||
|
int py;
|
||||||
|
int vx;
|
||||||
|
int vy;
|
||||||
|
} Robot;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char *line;
|
char *line;
|
||||||
|
|
||||||
int px, py, vx, vy;
|
int px, py, vx, vy;
|
||||||
|
|
||||||
int q1 = 0, q2 = 0, q3 = 0, q4 = 0;
|
int q1 = 0, q2 = 0, q3 = 0, q4 = 0;
|
||||||
|
Robot robots[500];
|
||||||
|
int robots_count = 0;
|
||||||
|
|
||||||
while((line = aoc_read_line()) != NULL) {
|
while((line = aoc_read_line()) != NULL) {
|
||||||
sscanf(line, "p=%d,%d v=%d,%d", &px, &py, &vx, &vy);
|
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);
|
robots[robots_count].px = px;
|
||||||
for (int i = 0; i < CYCLES; i++) {
|
robots[robots_count].py = py;
|
||||||
px += vx;
|
robots[robots_count].vx = vx;
|
||||||
px = (px + WIDTH) % WIDTH;
|
robots[robots_count].vy = vy;
|
||||||
py += vy;
|
robots_count++;
|
||||||
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++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 1: %lld\n", q1 * q2 * q3 * q4);
|
||||||
//printf("Part 2: %lld\n", cost_2);
|
printf("Part 2: %lld\n", i);
|
||||||
|
|
||||||
aoc_free();
|
aoc_free();
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user