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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
typedef struct Monkey_ Monkey;
typedef struct Monkey_ {
__uint128_t item[64];
__uint128_t items, is_multiply, opertant_is_old, operant, test, test_true, test_false, inspections;
} Monkey;
Monkey* newMonkey()
{
char *line = NULL;
size_t size;
size_t len;
Monkey *monkey = malloc(sizeof(Monkey));
monkey->inspections = 0;
// items
len = getline(&line, &size, stdin);
char *begin = line+18;
char *token = NULL;
for (int j = 0; (token = strsep(&begin, ",")); j++)
{
// printf("%u: %s\n", j, token);
monkey->item[j] = atoi(token);
monkey->items = j+1;
}
// operation
len = getline(&line, &size, stdin);
monkey->is_multiply = line[23] == '*';
monkey->opertant_is_old = line[25] == 'o';
if (!monkey->opertant_is_old)
{
monkey->operant = atoi(line+25);
}
// test
len = getline(&line, &size, stdin);
monkey->test = atoi(line+21);
// test true
len = getline(&line, &size, stdin);
monkey->test_true = atoi(line+29);
// test true
len = getline(&line, &size, stdin);
monkey->test_false = atoi(line+30);
len = getline(&line, &size, stdin);
return monkey;
}
// int priminice(int n)
// {
// int isPrime;
// int num = 1;
// for (int i = 2; i <= n; i++)
// {
// if(n % i == 0)
// {
// isPrime = 1;
// for (int j = 2; j <= i/2; j++)
// {
// if(i % j == 0)
// {
// isPrime = 0;
// break;
// }
// }
// if(isPrime == 1)
// {
// // printf("\n %d is a Prime Factor ", i);
// if ( num % i != 0 )
// {
// num *= i;
// }
// }
// }
// }
// return num;
// }
__uint128_t priminice(__uint128_t n)
{
__uint128_t num = 1;
while (n%2 == 0){
// cout<<"2\t";
if ( num % 2 != 0)
num *= 2;
n = n/2;
}
for (int i = 3; i*i <= n; i = i+2){
while (n%i == 0){
// cout<<i<<"\t";
if (num % i != 0)
num *= i;
n = n/i;
}
}
if (n > 2)
{
if (num % n != 0)
num *= n;
// cout<<n<<"\t";
}
return num;
}
void inspect(Monkey *monkey, Monkey *monkeys[])
{
monkey->inspections += monkey->items;
for (int i = 0; i < monkey->items; i++)
{
// set operant
__uint128_t operant = monkey->operant;
if (monkey->opertant_is_old)
operant = monkey->item[i];
// set operation
if (monkey->is_multiply )
{
monkey->item[i] *= operant;
} else {
monkey->item[i] += operant;
}
// monkey->item[i] /= 3;
// throw item based on test
int throw = monkey->test_true;
if (monkey->item[i] % monkey->test != 0)
{
throw = monkey->test_false;
}
monkeys[throw]->item[monkeys[throw]->items] = priminice(monkey->item[i]);
monkeys[throw]->items++;
}
monkey->items = 0;
}
int main(int argc, char *argv[])
{
int monkeys = 0;
Monkey *monkey[10] = { };
char *line = NULL;
size_t size;
size_t len;
__uint128_t active1 = 0, active2 = 0;
while ((len = getline(&line, &size, stdin)) != -1)
{
// create monkeys
Monkey *new = newMonkey();
monkey[monkeys] = new;
monkeys++;
// print monkey attributes
printf("Items:");
for (int i = 0; i < new->items; i++)
{
printf("%lud ", new->item[i]);
}
printf("\n %d %d %d %d %d %d\n\n",
new->is_multiply,
new->opertant_is_old,
new->operant,
new->test,
new->test_true,
new->test_false
);
}
for (int i = 0; i < atoi(argv[1]); i++)
{
// do inspection
for (int ii = 0; ii < monkeys; ii++)
{
inspect(monkey[ii], monkey);
}
// output rounds
printf("Round %d\n", i);
for (int ii = 0; ii < monkeys; ii++)
{
printf("Monkey %d: ", ii);
for (int iii = 0; iii < monkey[ii]->items; iii++)
{
printf("%lu ", monkey[ii]->item[iii]);
}
printf("\n");
}
}
// get two most active monkeys
for (int i = 0; i < monkeys; i++)
{
if (monkey[i]->inspections > active1)
{
active2 = active1;
active1 = monkey[i]->inspections;
}
printf("Monkey %d inspected items %d times. \n", i, monkey[i]->inspections);
}
printf("Monkey biz: \n%lu %lu %lu\n", active1, active2, active1 * active2);
}
|