summaryrefslogtreecommitdiff
path: root/11.c
blob: 09894d807e919a91d795a949e2d451f5c4e21f83 (plain)
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
280
281
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>


enum worry_op {
	WO_NONE,
	WO_ADD,
	WO_MUL,
	WO_SQR,
};


struct monkey {
	unsigned long * items;
	int nitems;
	enum worry_op wo;
	int wparam;
	int test_div;
	int test_true;
	int test_false;
	long inspections;
};


void
monkey_init(struct monkey * m)
{
	m->items = NULL;
	m->nitems = 0;
	m->wo = WO_NONE;
	m->wparam = 0;
	m->test_div = 0;
	m->test_true = 0;
	m->test_false = 0;
	m->inspections = 0;
}


void
monkey_dtor(struct monkey * m)
{
	free(m->items);
}


int
monkey_isvalid(struct monkey const * m, int max)
{
	return m->wo != WO_NONE
		&& m->test_div != 0
		&& m->test_true >= 0 && m->test_true < max
		&& m->test_false >= 0 && m->test_false < max;
}


int
monkey_additem(struct monkey * m, int worry)
{
	void * p;

	++m->nitems;
	p = realloc(m->items, m->nitems * sizeof(unsigned long));
	if (!p)
		return 0;
	m->items = p;
	m->items[m->nitems - 1] = worry;
	return 1;
}


void
monkey_dump(struct monkey const * m, int n)
{
	unsigned long * item;

	fprintf(stderr, "Monkey %d: %d/%d/%d/%d/%d/%ld: ",
			n, m->wo, m->wparam, m->test_div, m->test_true, m->test_false, m->inspections);
	for (item = m->items; item < m->items + m->nitems; ++item) {
		fprintf(stderr, "%s%lu", item == m->items ? "" : ", ", *item);
	}
	fprintf(stderr, "\n");
}


int
main(int argc, char ** argv)
{
	char buf[BUFSIZ];
	struct monkey * monkeys = NULL, * m = NULL;
	int nmonkeys = 0;
	long i, j;
	int calming = 3, rounds = 20;

	while ((i = getopt(argc, argv, "p:c:r:")) != -1) {
		switch (i) {
		case 'p':
			switch (atol(optarg)) {
			case 1: calming = 3; rounds = 20; break;
			case 2: calming = 1; rounds = 10000; break;
			default:
				fprintf(stderr, "Unexpected part %d\n", atoi(optarg));
				return -1;
			}
			break;

		case 'c':
			calming = atoi(optarg);
			if (calming == 0) {
				fprintf(stderr, "Unexpected calming factor %d\n", calming);
				return -1;
			}
			break;

		case 'r':
			rounds = atoi(optarg);
			break;

		default:
			return -1;
		}
	}

	// Read monkey data
	while (fgets(buf, sizeof(buf), stdin)) {
		if (buf[0] == '\n') {
			// Ignore blank line
		}
		else if (strncmp(buf, "Monkey ", 7) == 0 && isdigit(buf[7])) {
			void * p;

			++nmonkeys;

			if ((i = atoi(buf + 7)) != nmonkeys - 1) {
				fprintf(stderr, "Bad monkey %ld/%d!\n", i, nmonkeys);
				free(monkeys);
				return -1;
			}
			p = realloc(monkeys, nmonkeys * sizeof(struct monkey));
			if (!p) {
				fprintf(stderr, "realloc(%zd) fail\n", nmonkeys * sizeof(struct monkey));
				free(monkeys);
				return -1;
			}
			monkeys = p;
			m = monkeys + nmonkeys - 1;
			monkey_init(m);
		}
		else if (strncmp(buf, "  Starting items: ", 18) == 0 && isdigit(buf[18])) {
			char * cur, * next;
			int worry;

			cur = buf + 18;
			worry = strtol(cur, &next, 10);
			while (next != cur) {
				monkey_additem(m, worry);
				cur = next + 1;
				worry = strtol(cur, &next, 10);
			}
		}
		else if (strncmp(buf, "  Operation: new = old ", 23) == 0) {
			switch (buf[23]) {
			case '+': m->wo = WO_ADD; break;
			case '*': m->wo = WO_MUL; break;
			default:
				  fprintf(stderr, "Unexpected op %c\n", buf[23]);
				  free(monkeys);
				  return -1;
			}
			if (strncmp(buf + 25, "old", 3) == 0 && m->wo == WO_MUL) {
				m->wo = WO_SQR;
			}
			else {
				m->wparam = atoi(buf + 25);
			}
		}
		else if (strncmp(buf, "  Test: divisible by ", 21) == 0 && isdigit(buf[21])) {
			m->test_div = atoi(buf + 21);
		}
		else if (strncmp(buf, "    If true: throw to monkey ", 29) == 0 && isdigit(buf[29])) {
			m->test_true = atoi(buf + 29);
		}
		else if (strncmp(buf, "    If false: throw to monkey ", 30) == 0 && isdigit(buf[30])) {
			m->test_false = atoi(buf + 30);
		}
		else {
			fprintf(stderr, "Unexpected monkey: %s\n", buf);
			free(monkeys);
			return -1;
		}
	}

	// Check monkeys are valid
	for (m = monkeys; m < monkeys + nmonkeys; ++m) {
#if 0
		monkey_dump(m, m - monkeys);
#endif
		if (!monkey_isvalid(m, nmonkeys)) {
			fprintf(stderr, "Bad monkey %d\n", (int) (m - monkeys));
			monkey_dump(m, m - monkeys);
			free(monkeys);
			return -1;
		}
	}

	// Perform monkey inspection rounds
	for (i = 0; i < rounds; ++i) {
		for (m = monkeys; m < monkeys + nmonkeys; ++m) {
			unsigned long * item;

			for (item = m->items; item < m->items + m->nitems; ++item) {
				int catcher;

				switch (m->wo) {
				case WO_NONE:
					break; /* shouldn't happen */
				case WO_ADD:
					if (*item > ULONG_MAX - m->wparam)
						fprintf(stderr, "%ld/%d/%d: Overflow: %lu > %lu - %d\n",
								i, (int) (monkeys - m), (int) (item - m->items),
								*item, ULONG_MAX, m->wparam);
					*item += m->wparam;
					break;
				case WO_MUL:
					if (*item >= ULONG_MAX / m->wparam)
						fprintf(stderr, "%ld/%d/%d: Overflow: %lu >= %lu / %d\n",
								i, (int) (monkeys - m), (int) (item - m->items),
								*item, ULONG_MAX, m->wparam);
					*item *= m->wparam;
					break;
				case WO_SQR:
					if (*item >= ULONG_MAX / *item)
						fprintf(stderr, "%ld/%d/%d: Overflow: %lu >= %lu / %lu\n",
								i, (int) (monkeys - m), (int) (item - m->items),
								*item, ULONG_MAX, *item);
					*item *= *item;
					break;
				}
				*item /= calming;

				catcher = *item % m->test_div ? m->test_false : m->test_true;
				monkey_additem(monkeys + catcher, *item);

				++m->inspections;
			}
			m->nitems = 0;
		}

#if 0
		for (m = monkeys; m < monkeys + nmonkeys; ++m) {
			monkey_dump(m, m - monkeys);
		}
#endif
	}

	// Calculate monkey business
	i = j = 0;
	for (m = monkeys; m < monkeys + nmonkeys; ++m) {
		if (m->inspections > i) {
			j = i;
			i = m->inspections;
		}
		else if (m->inspections > j) {
			j = m->inspections;
		}
	}
	printf("Monkey business: %ld (%ld * %ld)\n", i * j, i, j);

	// Done.
	for (m = monkeys; m < monkeys + nmonkeys; ++m) {
		monkey_dtor(m);
	}
	free(monkeys);

	return 0;
}