diff options
| author | Mistivia <i@mistivia.com> | 2025-12-14 21:39:29 +0800 |
|---|---|---|
| committer | Mistivia <i@mistivia.com> | 2025-12-14 21:39:29 +0800 |
| commit | 68c9a7d1ba2f11897c15494b605d53791e07738f (patch) | |
| tree | 9f998f3804ad8aed72d4f37e79ab33241b5578c4 | |
day 1 part1
| -rw-r--r-- | .gitignore | 3 | ||||
| -rw-r--r-- | 01/Makefile | 5 | ||||
| -rw-r--r-- | 01/part1.c | 46 |
3 files changed, 54 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9e38fd5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +part1 +part2 +input
\ No newline at end of file diff --git a/01/Makefile b/01/Makefile new file mode 100644 index 0000000..7979cbd --- /dev/null +++ b/01/Makefile @@ -0,0 +1,5 @@ +part1: part1.c + gcc part1.c -o part1 + +1: part1 + cat input | ./part1
\ No newline at end of file diff --git a/01/part1.c b/01/part1.c new file mode 100644 index 0000000..9777645 --- /dev/null +++ b/01/part1.c @@ -0,0 +1,46 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +char buf[1024]; + +char * readline() { + int i = 0; + int c; + while (1) { + c = fgetc(stdin); + if (c == EOF) { + if (i == 0) { + return NULL; + } + goto end; + } + if (c == '\n') { + goto end; + } + buf[i] = c; + i++; + } +end: + buf[i] = '\0'; + return strdup(buf); +} + +int main() { + int dial = 50; + char *line; + int count = 0; + while ((line = readline()) != NULL) { + int negative = 0; + if (line[0] == 'L') negative = 1; + char *end; + long val = strtol(line+1, &end, 10); + if (negative) val = -val; + if (end == line+1) continue; + dial = (dial + val) % 100; + if (dial < 0) dial += 100; + if (dial == 0) count++; + } + printf("%d\n", count); + return 0; +}
\ No newline at end of file |
