63 lines
1.5 KiB
C
63 lines
1.5 KiB
C
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#define BUFFER_SIZE 64
|
||
|
|
#define INTERESTING_CYCLES 20, 60, 100, 140, 180, 220
|
||
|
|
#define CRT_WIDTH 40
|
||
|
|
#define CRT_HEIGHT 6
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
char buf[BUFFER_SIZE], *p, c;
|
||
|
|
memset(buf, 0, BUFFER_SIZE);
|
||
|
|
p = buf;
|
||
|
|
|
||
|
|
int X = 1, cycle = 0, sum = 0, add, delay = 0;
|
||
|
|
int interesting_cycles[] = {INTERESTING_CYCLES};
|
||
|
|
int row =0, col = 0;
|
||
|
|
|
||
|
|
while ((c = getchar()) != EOF) {
|
||
|
|
*p++ = c;
|
||
|
|
if (c == '\n') {
|
||
|
|
|
||
|
|
|
||
|
|
if (sscanf(buf, "addx %i", &add)) {
|
||
|
|
delay = 2;
|
||
|
|
} else {
|
||
|
|
add = 0;
|
||
|
|
delay = 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
while (delay > 0) {
|
||
|
|
cycle++;
|
||
|
|
if (col == CRT_WIDTH) {
|
||
|
|
printf("\n");
|
||
|
|
col = 0;
|
||
|
|
row++;
|
||
|
|
}
|
||
|
|
if (X - 1 == col || X == col || X + 1 == col) {
|
||
|
|
printf("#");
|
||
|
|
} else {
|
||
|
|
printf(".");
|
||
|
|
}
|
||
|
|
// Update sum if we are in the right cycle
|
||
|
|
for (int i = 0; i < sizeof(interesting_cycles)/sizeof(int); i++) {
|
||
|
|
if (interesting_cycles[i] == cycle) {
|
||
|
|
sum += X * cycle;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
col++;
|
||
|
|
delay--;
|
||
|
|
}
|
||
|
|
|
||
|
|
X += add;
|
||
|
|
|
||
|
|
memset(buf, 0, BUFFER_SIZE);
|
||
|
|
p = buf;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
printf("\n%i\n", sum);
|
||
|
|
}
|