-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.c
56 lines (45 loc) · 1.62 KB
/
10.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_LENGTH 20
int main(int argc, char* argv[]) {
char line[MAX_LINE_LENGTH], crt_line[41];
int instruction_value, register_value = 1, signal_strength = 0;
size_t i, instruction_cycles, counter = 1, screen_position;
while (fgets(line, sizeof line, stdin) != NULL) {
line[strcspn(line, "\n")] = 0;
if (sscanf(line, "addx %d", &instruction_value)) {
instruction_cycles = 2;
} else if (strcmp(line, "noop") == 0) {
instruction_cycles = 1;
instruction_value = 0;
} else {
fprintf(
stderr,
"%s: unrecognized instruction %s\n",
argv[0],
line
);
return 1;
}
for (i = 0; i < instruction_cycles; i++) {
if (counter % 40 == 1)
strcpy(crt_line, "........................................");
else if (counter % 40 == 0)
printf("%s\n", crt_line);
screen_position = (counter - 1) % 40;
if (
screen_position == register_value - 1
|| screen_position == register_value
|| screen_position == register_value + 1
)
crt_line[screen_position] = '#';
if (counter % 40 == 20 && counter / 40 < 6)
signal_strength += counter * register_value;
counter++;
}
register_value += instruction_value;
}
printf("Signal strength: %d\n", signal_strength);
return 0;
}