-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay10.c
147 lines (116 loc) · 3.23 KB
/
Day10.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "Helpers.c"
#define CAP 100
#define SYNTAX_CAP 200
typedef struct {
int position;
char open[SYNTAX_CAP];
int nOpen;
} SyntaxResult;
static int parse(const char *input, char lines[CAP][SYNTAX_CAP]) {
int charsRead;
int n = 0;
while (sscanf(input, "%s\n%n", lines[n], &charsRead) != EOF) {
n++;
assert(n < CAP);
input += charsRead;
}
return n;
}
static char openCharFromClosing(char c) {
switch (c) {
case ')': return '(';
case ']': return '[';
case '}': return '{';
case '>': return '<';
default: assert(false);
}
}
static int scoreFromUnexpectedClosingChar(char c) {
switch (c) {
case ')': return 3;
case ']': return 57;
case '}': return 1197;
case '>': return 25137;
default: assert(false);
}
}
static int scoreFromMissingClosingChar(char c) {
switch (c) {
case '(': return 1;
case '[': return 2;
case '{': return 3;
case '<': return 4;
default: assert(false);
}
}
static void validateSyntax(const char *line, SyntaxResult *result) {
const char *lineBegin = line;
result->nOpen = 0;
result->position = 0;
for (char c = *line; c != 0; c = *++line) {
switch (c) {
case '(':
case '[':
case '{':
case '<':
result->open[result->nOpen++] = c;
break;
case ')':
case ']':
case '}':
case '>':
if (result->nOpen > 0 && result->open[result->nOpen - 1] == openCharFromClosing(c)) {
--result->nOpen;
break;
} else {
result->position = (int)(line - lineBegin);
return;
}
}
}
}
static int compareInt64(const void *a, const void *b) {
return (*(const int64_t *)a > *(const int64_t *)b)
? 1
: -1;
}
static int partOne(int n, const char lines[n][SYNTAX_CAP]) {
int score = 0;
SyntaxResult result = {0};
for (int i = 0; i < n; ++i) {
validateSyntax(lines[i], &result);
if (result.nOpen > 0 && result.position > 0) {
score += scoreFromUnexpectedClosingChar(lines[i][result.position]);
}
}
return score;
}
static int64_t partTwo(int n, const char lines[n][SYNTAX_CAP]) {
int64_t scores[n];
int nScores = 0;
SyntaxResult result = {0};
for (int i = 0; i < n; ++i) {
validateSyntax(lines[i], &result);
if (result.nOpen > 0 && result.position == 0) {
int64_t score = 0;
for (int j = result.nOpen - 1; j >= 0; --j) {
score = (5 * score) + scoreFromMissingClosingChar(result.open[j]);
}
scores[nScores++] = score;
}
}
qsort(scores, (uint32_t)nScores, sizeof(scores[0]), compareInt64); // O(n * log n)
return scores[nScores / 2];
}
int main() {
const char *input = Helpers_readInputFile(__FILE__);
char lines[CAP][SYNTAX_CAP] = {0};
int n = parse(input, lines);
Helpers_assert(PART1, Helpers_clock(),
partOne(n, lines),
26397, 364389);
Helpers_assert(PART2, Helpers_clock(),
partTwo(n, lines),
288957, 2870201088);
return 0;
}