aboutsummaryrefslogtreecommitdiff
path: root/day12/day12.c
diff options
context:
space:
mode:
authorNæþ'n Lasseter <Næþ'n Lasseter nathan@bytemark.co.uk>2015-12-17 20:08:38 +0000
committerNæþ'n Lasseter <Næþ'n Lasseter nathan@bytemark.co.uk>2015-12-17 20:08:38 +0000
commitd79e5901299d277fab0d04d3f6b2b09cf1527da4 (patch)
tree635b69c83345057b588256838f40508fd11f7648 /day12/day12.c
parent598e6dea91e17b952af4f31263274fdc05ceee64 (diff)
Day 12, first half
Diffstat (limited to 'day12/day12.c')
-rw-r--r--day12/day12.c75
1 files changed, 75 insertions, 0 deletions
diff --git a/day12/day12.c b/day12/day12.c
new file mode 100644
index 0000000..2147c13
--- /dev/null
+++ b/day12/day12.c
@@ -0,0 +1,75 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ungetchar(ch) ungetc(ch, stdin)
+
+int getnumber(char* input, int* i) {
+ int ch, num = 0, neg = 0;
+
+ if(input[(*i)++] == '-') {
+ ch = input[(*i)++];
+
+ if(ch < '0' || ch > '9')
+ return 0;
+ else
+ neg = 1;
+ }
+ (*i)--;
+
+ while((ch = input[(*i)++]) >= '0' && ch <= '9')
+ num = (num * 10) + (ch - 48);
+ (*i)--;
+
+ if(neg)
+ return -num;
+ else
+ return num;
+}
+
+int sumnumbers(char* input) {
+ int tot = 0, ch, i = 0;
+
+ while((ch = input[i++]) != '\0') {
+ if((ch >= '0' && ch <= '9') || ch == '-') {
+ i--;
+ tot += getnumber(input, &i);
+ }
+ }
+
+ return tot;
+}
+
+int sumnonred(char* input) {
+ return 0;
+}
+
+char* readtheinput() {
+ char *theinput, ch;
+ int i = 0, max = 1024;
+
+ theinput = (char*) malloc(max * sizeof(char));
+
+ while((ch = getchar()) != EOF) {
+ theinput[i++] = ch;
+ if(i == max) {
+ max += 1024;
+ theinput = (char*) realloc((void*) theinput, max * sizeof(char));
+ }
+ }
+
+ theinput[i] = '\0';
+
+ return theinput;
+}
+
+int main() {
+ char* theinput;
+
+ theinput = readtheinput();
+
+ printf("Sum of all numbers: %d\n", sumnumbers(theinput));
+ printf("Sum of all non-red numbers: %d\n", sumnonred(theinput));
+
+ free(theinput);
+ return 0;
+}