-
Notifications
You must be signed in to change notification settings - Fork 0
/
day16.c
279 lines (251 loc) · 6.79 KB
/
day16.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include <malloc.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "lines.h"
#include "htab.h"
typedef struct {
char * name;
int range[4]; // first-second third-fourth
list_t ls;
bool found;
} field_t;
void field_print(field_t *f) {
printf("%s: %d-%d, %d-%d\n", f->name, f->range[0], f->range[1], f->range[2], f->range[3]);
}
bool in_range(field_t * f, int val) {
if (!f) return false;
// field_print(f);
if (val < f->range[0] || val > f->range[1]) { // outside first range
if (val < f->range[2] || val > f->range[3]) {
return false || in_range(list_next(f, field_t), val);
}
}
return true;
}
bool in_range2(field_t * f, int val) {
if (!f) return false;
// field_print(f);
if (val < f->range[0] || val > f->range[1]) { // outside first range
if (val < f->range[2] || val > f->range[3]) {
return false;
}
}
return true;
}
// clear fields
void field_free(field_t * field) {
if(!field) return;
free(field->name);
field_free(list_next(field, field_t));
free(field);
}
field_t * read_field(lines_t * line) {
if (!line) return NULL;
field_t * f= calloc(sizeof(field_t), 1);
char * l = line->line;
assert(f);
char * start = strsep(&l, ":");
f->name = strdup(start);
assert(f->name);
// get the first character.
char * c = strsep(&l, "-");
f->range[0] = atoi(c);
f->range[1] = atoi(l);
c = strsep(&l, "r");
f->range[2] = atoi(l);
strsep(&l, "-");
f->range[3] = atoi(l);
return f;
}
bool is_your_ticket(char * s) {
return (strncmp(s, "your ticket", strlen("your ticket")) ==0);
}
bool is_nearby_ticket(char *s) {
return (strncmp(s, "nearby ticket", strlen("nearby ticket")) ==0);
}
bool is_field(char *s) {
if (strlen(s) > 0 && isalpha(s[0])) return true;
else return false;
}
int invalid_ticket(char * line, field_t * f) {
// check every item.
char tempbuf[strlen(line)];
strncpy(tempbuf, line, strlen(line));
char * needle = tempbuf;
do {
int val = atoi(needle);
if (!in_range(f, val)) {
return val;
}
strsep(&needle, ",");// move it to the next item.
} while(needle != NULL);
return 0;
}
bool error_rate(lines_t *line, field_t * f) {
// we have a list of fields, check if a ticket is valid or not.
int error_sum = 0;
for (lines_t * l = line; l != NULL; l = lines_next(l)) {
int error = invalid_ticket(l->line, f);
// printf("%d: %s\n", error, l->line);
error_sum += error;
}
printf("%d\n", error_sum);
return (error_sum != 0); // returns true if there is an error
}
void process_ticket(lines_t * lines) {
field_t * fields = NULL;
for (lines_t * l= lines ; l != NULL; l = lines_next(l)) {
if (is_your_ticket(l->line)) {
l = lines_next(l); // skip this one.
} else if (is_nearby_ticket(l->line)) {
error_rate(lines_next(l), fields);
printf("nt\n");
break;
} else if (is_field(l->line)) {
printf("if\n");
if (fields == NULL) {
fields = read_field(l);
} else {
field_t * f = read_field(l);
list_insert(&(fields->ls), &(f->ls));
}
}
}
field_free(fields);
}
// part 2:
// get a list of valid tickets
// do BFS
typedef struct {
int * ints;
int ncol; /* number of items in a row */
int nrow; /* number of rows */
} ticket_block;
void ticket_block_free(ticket_block *tb) {
if (!tb) return;
free(tb->ints);
free(tb);
}
ticket_block * ticket_block_create(lines_t * lines) {
if (!lines) return NULL;
int ncols = 0;
int nrows = 0;
for (int i = 0; lines->line[i] != '\0';i++ ) {
if (lines->line[i] == ',') ncols++;
}
ncols++;
for(lines_t * li = lines ; li != NULL; li = lines_next(li), nrows++);
int * ints = calloc(sizeof(int) , ncols * nrows);
// lets parse them
int i = 0;
for(lines_t * li = lines; li != NULL; li = lines_next(li)) {
char * l = li->line;
do {
ints[i++] = atoi(l);
strsep(&l, ",");// move it to the next item.
} while(l != NULL);
}
ticket_block * tb = calloc(sizeof(ticket_block), 1);
tb->ints = ints;
tb->nrow = nrows;
tb->ncol = ncols;
return tb;
}
field_t * field_for_col(field_t * f, ticket_block * tb, int col) {
bool is_valid = true;
for (int j = 0; j < tb->nrow; j++) {
if (!in_range2(f, tb->ints[col + tb->ncol *j])) {
is_valid = false;
break;
}
}
if (is_valid) return f;
return NULL;
}
bool fields_matching_columns(field_t * f, ticket_block * tb, int ncol,
char ** fields) {
if (f == NULL) return false;
if (ncol >= tb->ncol) {
return true;
}
if (ncol == 0) { // set found = false
for(field_t * ff = f; ff != NULL; ff = list_next(ff, field_t)) {
ff->found = false;
}
}
// we look for fields that can match the first column,
// second column and so on.
for(field_t * ff = f; ff != NULL; ff = list_next(ff, field_t)) {
if (ff->found) continue;
if (field_for_col(ff, tb, ncol)) { // we found something
ff->found = true;
fields[ncol] = ff->name;
bool connected = fields_matching_columns(f, tb, ++ncol, fields);
// do i need to backtrack?
if (!connected) { // rollback, this is not what we want.
ff->found = false;
fields[--ncol] = NULL;
} else {
printf("%s -> %d\n", f->name, ncol);
return true;
}
}
}
return false;
}
void process_ticket2(lines_t * lines) {
field_t * fields = NULL;
lines_t * valid_tickets = NULL;
int nfields =0;
for (lines_t * l= lines ; l != NULL; l = lines_next(l)) {
if (is_your_ticket(l->line)) {
l = lines_next(l); // skip this one.
} else if (is_nearby_ticket(l->line)) {
l = lines_next(l); // advance once
while (l != NULL) {
if (!invalid_ticket(l->line, fields)) {
if (!valid_tickets) {
valid_tickets = calloc(sizeof(lines_t), 1);
valid_tickets->line = strdup(l->line);
} else {
lines_t * tmp = calloc(sizeof(lines_t), 1);
tmp->line = strdup(l->line);
list_insert(&(valid_tickets->ls), &(tmp->ls));
}
}
l = lines_next(l);
}
break;
} else if (is_field(l->line)) {
nfields++;
if (fields == NULL) {
fields = read_field(l);
} else {
field_t * f = read_field(l);
list_insert(&(fields->ls), &(f->ls));
}
}
}
ticket_block * tb = ticket_block_create(valid_tickets);
int found[tb->ncol];
memset(found, 0, sizeof(int) * tb->ncol);
char * field_names[tb->ncol];
memset(field_names, 0, sizeof(char *) * tb->ncol);
fields_matching_columns(fields, tb, 0, field_names);
for (int i = 0 ; i < tb->ncol; i++) {
printf("%s => %d\n", field_names[i], i );
}
// all the valid tickets are in lines.
ticket_block_free(tb);
free_lines(valid_tickets);
field_free(fields);
}
int main() {
char * filename = "day16_input.txt";
lines_t * lines = read_lines(filename);
process_ticket2(lines);
free_lines(lines);
return 0;
}