diff options
author | Ian C <ianc@noddybox.co.uk> | 2021-12-11 10:09:47 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2021-12-11 10:09:47 +0000 |
commit | ba4d47000d0900365605c18b74ada1d92a56c0a3 (patch) | |
tree | 0a360fa16be86ebb44a79944d3548784c32de370 | |
parent | 4a00a5935086c964ac04263fa42ac81658de99fe (diff) |
Added day 10
-rw-r--r-- | 10.c | 166 | ||||
-rw-r--r-- | 10a.c | 194 | ||||
-rw-r--r-- | 8a.c | 4 | ||||
-rw-r--r-- | data.10a | 10 |
4 files changed, 372 insertions, 2 deletions
@@ -0,0 +1,166 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define MAX_STACK 1024 +typedef struct +{ + char data[MAX_STACK]; + int sp; +} stack; + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static char AnyOpen(int c) +{ + switch(c) + { + case '(': + return 1; + case '[': + return 1; + case '{': + return 1; + case '<': + return 1; + default: + return 0; + } +} + +static char AnyClose(int c) +{ + switch(c) + { + case ')': + return 1; + case ']': + return 1; + case '}': + return 1; + case '>': + return 1; + default: + return 0; + } +} + +static char Close(int fromopen) +{ + switch(fromopen) + { + case '(': + return ')'; + case '[': + return ']'; + case '{': + return '}'; + case '<': + return '>'; + default: + return 0; + } +} + +static void Push(stack *s, char c) +{ + if (s->sp == MAX_STACK) + { + printf("Stack too big!\n"); + exit(1); + } + + s->data[s->sp++] = c; +} + + +static char Pop(stack *s) +{ + if (s->sp == 0) + { + printf("Stack too small!\n"); + exit(1); + } + + return s->data[--s->sp]; +} + + +static int SyntaxError(char *p) +{ + char c = 0; + int score = 0; + stack s = {0}; + + while((c = *p++)) + { + if (AnyOpen(c)) + { + Push(&s, c); + } + + if (AnyClose(c)) + { + char close = Close(Pop(&s)); + + if (close != c) + { + if (c == ')') + { + return 3; + } + + if (c == ']') + { + return 57; + } + + if (c == '}') + { + return 1197; + } + + if (c == '>') + { + return 25137; + } + + printf("Unexpected character %c\n", c); + } + } + } + + return 0; +} + + +int main(void) +{ + char buff[0x8000]; + int sum = 0; + int score = 0; + int f = 0; + + while(ReadLine(buff, sizeof buff, stdin)) + { + score = SyntaxError(buff); + sum += score; + } + + printf("sum = %d\n", sum); + + return 0; +} @@ -0,0 +1,194 @@ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#define MAX_STACK 1024 +typedef struct +{ + char data[MAX_STACK]; + int sp; +} stack; + +static char *ReadLine(char *p, size_t size, FILE *fp) +{ + if ((p=fgets(p, size, fp))) + { + size_t l = strlen(p); + + while(l && p[l-1] == '\n') + { + p[--l] = 0; + } + } + + return p; +} + +static char AnyOpen(int c) +{ + switch(c) + { + case '(': + return 1; + case '[': + return 1; + case '{': + return 1; + case '<': + return 1; + default: + return 0; + } +} + +static char AnyClose(int c) +{ + switch(c) + { + case ')': + return 1; + case ']': + return 1; + case '}': + return 1; + case '>': + return 1; + default: + return 0; + } +} + +static char Close(int fromopen) +{ + switch(fromopen) + { + case '(': + return ')'; + case '[': + return ']'; + case '{': + return '}'; + case '<': + return '>'; + default: + return 0; + } +} + +static void Push(stack *s, char c) +{ + if (s->sp == MAX_STACK) + { + printf("Stack too big!\n"); + exit(1); + } + + s->data[s->sp++] = c; +} + + +static char Pop(stack *s) +{ + if (s->sp == 0) + { + printf("Stack too small!\n"); + exit(1); + } + + return s->data[--s->sp]; +} + + +static long CompleteLine(char *p) +{ + char c = 0; + long score = 0; + stack s = {0}; + + while((c = *p++)) + { + if (AnyOpen(c)) + { + Push(&s, c); + } + + if (AnyClose(c)) + { + char close = Close(Pop(&s)); + + if (close != c) + { + return 0; + } + } + } + + while(s.sp) + { + char close = Close(Pop(&s)); + + score *= 5; + switch(close) + { + case ')': + score += 1; + break; + case ']': + score += 2; + break; + case '}': + score += 3; + break; + case '>': + score += 4; + break; + } + } + + return score; +} + + +static int Compare(const void *a, const void *b) +{ + const long *pa = a; + const long *pb = b; + + if (*pa == *pb) + { + return 0; + } + else if (*pa < *pb) + { + return -1; + } + else + { + return 1; + } +} + + +int main(void) +{ + char buff[0x8000]; + long score[1204] = {0}; + int num_score = 0; + long f = 0; + + while(ReadLine(buff, sizeof buff, stdin)) + { + f = CompleteLine(buff); + + if (f) + { + score[num_score++] = f; + } + } + + qsort(score, num_score, sizeof score[0], Compare); + + printf("middle = %ld\n", score[num_score / 2]); + + return 0; +} @@ -26,8 +26,8 @@ static char *ReadLine(char *p, size_t size, FILE *fp) static int CompareLen(const void *a, const void *b) { - const char **pa = a; - const char **pb = b; + const char const **pa = a; + const char const **pb = b; int l1; int l2; diff --git a/data.10a b/data.10a new file mode 100644 index 0000000..b1518d9 --- /dev/null +++ b/data.10a @@ -0,0 +1,10 @@ +[({(<(())[]>[[{[]{<()<>> +[(()[<>])]({[<{<<[]>>( +{([(<{}[<>[]}>{[]{[(<()> +(((({<>}<{<{<>}{[]{[]{} +[[<[([]))<([[{}[[()]]] +[{[{({}]{}}([{[{{{}}([] +{<[[]]>}<{[{[{[]{()[[[] +[<(<(<(<{}))><([]([]() +<{([([[(<>()){}]>(<<{{ +<{([{{}}[<[[[<>{}]]]>[]] |