This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolve10.c
82 lines (74 loc) · 1.79 KB
/
solve10.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <stdlib.h>
char state[1024];
unsigned depth = 0;
unsigned bad = 0;
unsigned line = 1;
unsigned long long total_bad = 0;
unsigned long long completions[1024];
unsigned ncompletions = 0;
static int mydiff(const void * a, const void * b)
{
unsigned long long aval = *(unsigned long long const *)a;
unsigned long long bval = *(unsigned long long const *)b;
return (aval < bval) ? -1 : (aval == bval) ? 0 : 1;
}
int main()
{
for(;;) {
int c = getchar();
if (c <= 0 || c=='\n') {
if (!bad && depth > 0) {
unsigned long long score = 0;
while (depth > 0) {
score *= 5;
switch (state[--depth]) {
case '(': score += 1; break;
case '[': score += 2; break;
case '{': score += 3; break;
case '<': score += 4; break;
default: assert(0);
}
}
assert(ncompletions < 1024);
completions[ncompletions++] = score;
}
depth = 0;
bad = 0;
if (c <= 0) break;
line++;
continue;
}
if (bad || isspace(c))
continue;
if (c=='(' || c=='[' || c=='{' || c=='<') {
assert(depth < 1024);
state[depth++] = c;
}
else {
unsigned char_score = 0;
char k = '\0';
switch (c) {
case ')': char_score = 3; k = '('; break;
case ']': char_score = 57; k = '['; break;
case '}': char_score = 1197; k = '{'; break;
case '>': char_score = 25137; k = '<'; break;
default: assert(0);
}
if (k == state[depth-1]) {
--depth;
} else {
bad = char_score;
total_bad += bad;
}
}
}
printf("Total badness: %llu\n", total_bad);
printf("Number to complete: %u\n", ncompletions);
assert(ncompletions & 1);
qsort(completions, ncompletions, sizeof(completions[0]), mydiff);
printf("Middle score: %llu\n", completions[ncompletions>>1]);
return 0;
}