diff options
| author | Mistivia <i@mistivia.com> | 2024-01-27 14:28:51 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2024-01-27 14:28:51 +0800 |
| commit | 087a111b3417cbda03a3453b3d16dd4d1cf54a9e (patch) | |
| tree | 000a15054865c3fb974970238568bb1d81a3f29e /advent-of-code/2022/10/2.c | |
| parent | 203658f4a5b8649d0142ab8ff6440eb0eefa48e9 (diff) | |
add aoc 2022
Diffstat (limited to 'advent-of-code/2022/10/2.c')
| -rw-r--r-- | advent-of-code/2022/10/2.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/advent-of-code/2022/10/2.c b/advent-of-code/2022/10/2.c new file mode 100644 index 0000000..1abcb7e --- /dev/null +++ b/advent-of-code/2022/10/2.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <stdbool.h> + +#include <str.h> + +char screen[6][40]; + +int regx = 0; +int time = 0; + +void process() { + if (time >= 240) return; + int delta = time % 40 - regx; + if (delta <= 2 && delta >= 0) { + screen[time / 40][time % 40] = '#'; + } +} + +void tick(int value) { + process(); + time++; + regx += value; +} + +int main() { + memset(screen, '.', 240); + FILE *fp = fopen("input", "r"); + while (true) { + char *rawline = fgetline(fp); + if (rawline == NULL) break; + char *line = str_strip(rawline); + free(rawline); + char** words = str_split(line, ' '); + if (words == NULL) { + free(line); + continue; + } + if (words[0] == NULL) { + free(line); free(words); + continue; + } + if (strcmp(words[0], "noop") == 0) { + tick(0); + } else if (strcmp(words[0], "addx") == 0) { + tick(0); + char *ptr; + int value = strtol(words[1], &ptr, 10); + tick(value); + } + str_list_free(words); + free(line); + } + for (int i = 0; i < 6; i++) { + for (int j = 0; j < 40; j++) { + printf("%c", screen[i][j]); + } + printf("\n"); + } + return 0; +} |
