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
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM_MONKEYS 8
#define ROUNDS 10000
struct monkey {
size_t n;
long *items;
int test;
int true_to;
int false_to;
char op_op; /* like '*' or '+' */
int op_rhs; /* 0 means self */
long inspected;
};
void
push_item(struct monkey *m, long item) {
if (m == NULL)
return;
long *tmp = realloc(m->items, sizeof (long) * (m->n + 1));
if (tmp == NULL) {
perror("realloc");
exit(1);
}
tmp[m->n] = item;
m->items = tmp;
m->n++;
}
long
pop_item(struct monkey *m) {
if (m->n < 1) {
fprintf(stderr, "no items to pop!\n");
exit(1);
}
long d = m->items[0];
for (int i = 1; i < m->n; i++) {
m->items[i - 1] = m->items[i];
}
m->n--;
return d;
}
int
inspect(struct monkey *m, long *n, long lcm) {
long new;
long old = pop_item(m);
long rhs = m->op_rhs;
if (rhs == 0)
rhs = old;
if (m->op_op == '+')
new = old + rhs;
else if (m->op_op == '*')
new = old * rhs;
else {
fprintf(stderr, "unknown operation '%c'\n", m->op_op);
return 0;
}
m->inspected++;
/* UNCOMMENT FOR PART A */
/* new /= 3; */
new = new % lcm;
*n = new;
if (new < 0) {
fprintf(stderr, "! %ld %c %ld -> %ld\n", old, m->op_op, rhs, new);
}
if (new % m->test == 0)
return 1;
else
return -1;
}
int
main(int argc, char **argv) {
char *buf = NULL;
size_t buflen = 0;
ssize_t n;
FILE *f;
struct monkey monkeys[NUM_MONKEYS] = {0};
if (argc != 2) {
fprintf(stderr, "usage: %s input\n", argv[0]);
return 1;
}
f = fopen(argv[1], "r");
if (f == NULL) {
perror(argv[1]);
return 1;
}
int mn = -1;
long d;
char *s = NULL;
while ((n = getline(&buf, &buflen, f)) != -1) {
if (buf[n - 1] == '\n') {
buf[n - 1] = '\0';
n--;
}
if (n == 0)
continue;
if (buf[0] == 'M') {
/* next monkey init. not necessary to parse number since they are in order. */
mn++;
assert(mn < NUM_MONKEYS);
continue;
}
switch (buf[2]) {
case 'S':
/* starting items */
s = buf+17;
while (s != NULL) {
s++;
if (sscanf(s, "%ld", &d) != 1) {
perror("sscanf");
return 1;
}
push_item(&monkeys[mn], d);
s = strchr(s, ',');
}
break;
case 'O':
/* operation */
assert(n > 25);
monkeys[mn].op_op = buf[23];
if (buf[25] == 'o') {
monkeys[mn].op_rhs = 0; /* old OP old */
} else {
if (sscanf(buf+25, "%ld", &d) != 1) {
perror("sscanf");
return 1;
}
monkeys[mn].op_rhs = d;
}
break;
case 'T':
/* test */
if (sscanf(buf+2, "Test: divisible by %ld", &d) != 1) {
perror("sscanf");
return 1;
}
monkeys[mn].test = d;
break;
default:
if (buf[2] == ' ') {
assert(n > 29);
if (buf[7] == 't') {
/* true */
/* don't sscanf because it's a single digit (NUM_MONKEYS < 10) */
monkeys[mn].true_to = buf[29] - '0';
} else {
/* false */
monkeys[mn].false_to = buf[30] - '0';
}
}
break;
}
}
free(buf);
fclose(f);
/* I cheated and looked up what to do for part 2. I modulus the new worry
* levels against the LCM of all the divisors - this way, the remainders
* will all be the same, but the numbers don't get stupidly big.
*/
long lcm = 1;
for (mn = 0; mn < NUM_MONKEYS; mn++)
lcm *= monkeys[mn].test;
for (int round = 0; round < ROUNDS; round++) {
for (mn = 0; mn < NUM_MONKEYS; mn++) {
size_t todo = monkeys[mn].n; /* store because it decreases for each inspect() */
for (size_t i = 0; i < todo; i++) {
long new = 0;
int test;
test = inspect(&monkeys[mn], &new, lcm);
if (test == 1) {
/* true */
push_item(&monkeys[monkeys[mn].true_to], new);
} else if (test == -1) {
/* false */
push_item(&monkeys[monkeys[mn].false_to], new);
} else {
/* error */
exit(1);
}
}
}
/*
fprintf(stderr, "After round %d:\n", round);
for (int i = 0; i < NUM_MONKEYS; i++) {
fprintf(stderr, "Monkey %d:", i);
for (size_t j = 0; j < monkeys[i].n; j++) {
fprintf(stderr, " %ld", monkeys[i].items[j]);
}
fprintf(stderr, "\n");
}
*/
}
long max1, max2;
max1 = max2 = 0;
for (int i = 0; i < NUM_MONKEYS; i++) {
long me = monkeys[i].inspected;
if (me > max1) {
max2 = max1;
max1 = me;
} else if (me > max2) {
max2 = me;
}
}
printf("%ld\n", max1 * max2);
for (int i = 0; i < NUM_MONKEYS; i++) {
printf("%d inspected %ld\n", i, monkeys[i].inspected);
free(monkeys[i].items);
}
return 0;
}
|