aboutsummaryrefslogtreecommitdiff
path: root/01
diff options
context:
space:
mode:
authorMistivia <i@mistivia.com>2025-12-14 21:48:47 +0800
committerMistivia <i@mistivia.com>2025-12-14 21:48:47 +0800
commit99e608427f86453bd51399ce88ca79a69a146860 (patch)
treec558245adc3f57b8220abcc296ee1c68053d9c04 /01
parent68c9a7d1ba2f11897c15494b605d53791e07738f (diff)
day 1 part2
Diffstat (limited to '01')
-rw-r--r--01/Makefile8
-rw-r--r--01/part2.c57
2 files changed, 64 insertions, 1 deletions
diff --git a/01/Makefile b/01/Makefile
index 7979cbd..cfcbd94 100644
--- a/01/Makefile
+++ b/01/Makefile
@@ -1,5 +1,11 @@
part1: part1.c
gcc part1.c -o part1
+part2: part2.c
+ gcc part2.c -o part2
+
1: part1
- cat input | ./part1 \ No newline at end of file
+ cat input | ./part1
+
+2: part2
+ cat input | ./part2 \ No newline at end of file
diff --git a/01/part2.c b/01/part2.c
new file mode 100644
index 0000000..211b1bb
--- /dev/null
+++ b/01/part2.c
@@ -0,0 +1,57 @@
+#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 (val >= 100) {
+ count += val / 100;
+ val = val % 100;
+ }
+ if (negative) val = -val;
+ if (end == line+1) continue;
+ int olddial = dial;
+ dial = dial + val;
+ if (dial >= 100) {
+ count++;
+ }
+ if (olddial > 0 && dial <= 0) {
+ count++;
+ }
+ dial = dial % 100;
+ if (dial < 0) dial += 100;
+ }
+ printf("%d\n", count);
+ return 0;
+} \ No newline at end of file