summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--10.c166
-rw-r--r--10a.c194
-rw-r--r--8a.c4
-rw-r--r--data.10a10
4 files changed, 372 insertions, 2 deletions
diff --git a/10.c b/10.c
new file mode 100644
index 0000000..b79b8a7
--- /dev/null
+++ b/10.c
@@ -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;
+}
diff --git a/10a.c b/10a.c
new file mode 100644
index 0000000..8bf4e87
--- /dev/null
+++ b/10a.c
@@ -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;
+}
diff --git a/8a.c b/8a.c
index 9c747b2..4052987 100644
--- a/8a.c
+++ b/8a.c
@@ -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 @@
+[({(<(())[]>[[{[]{<()<>>
+[(()[<>])]({[<{<<[]>>(
+{([(<{}[<>[]}>{[]{[(<()>
+(((({<>}<{<{<>}{[]{[]{}
+[[<[([]))<([[{}[[()]]]
+[{[{({}]{}}([{[{{{}}([]
+{<[[]]>}<{[{[{[]{()[[[]
+[<(<(<(<{}))><([]([]()
+<{([([[(<>()){}]>(<<{{
+<{([{{}}[<[[[<>{}]]]>[]]