-
Notifications
You must be signed in to change notification settings - Fork 0
/
day11.c3
140 lines (132 loc) · 3.62 KB
/
day11.c3
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
module day11;
import std::io;
import std::math;
struct Monkey
{
long[100] items;
int item_count;
bool op_is_mult;
int other_op;
int test;
int on_true;
int on_false;
long inspections;
}
// For help
fn void print_monkeys(Monkey[] monkeys)
{
foreach (i, &m : monkeys)
{
io::printfn("%d: %s, inspect %d, new = old %c %d, / by %d -> %d | %d", i, m.items[:m.item_count], m.inspections, m.op_is_mult ? '*' : '+', m.other_op,
m.test, m.on_true, m.on_false);
}
}
fn Monkey[] load_monkeys(Monkey[] monkeys)
{
File f = file::open("monkey.txt", "rb")!!;
defer (void)f.close();
int max_monkey = 0;
while (!f.eof())
{
@pool()
{
// Very lazy parsing, assuming a lot about the format.
String line = io::treadline(&f)!!;
if (line.len == 0) continue;
String[] commands = line.tsplit(" ");
int monkey_num = commands[1][..^2].to_int()!!;
if (monkey_num > max_monkey) max_monkey = monkey_num;
Monkey* active_monkey = &monkeys[monkey_num];
line = io::treadline(&f)!!;
String[] items = line.tsplit(": ")[1].tsplit(", ");
foreach (String item : items)
{
active_monkey.items[active_monkey.item_count++] = item.to_int()!!;
}
line = io::treadline(&f)!!;
String[] op = line.tsplit("= old ")[1].tsplit(" ");
active_monkey.op_is_mult = op[0][0] == '*';
active_monkey.other_op = op[1] == "old" ? -1 : op[1].to_int()!!;
line = io::treadline(&f)!!;
active_monkey.test = line.tsplit("by ")[1].to_int()!!;
line = io::treadline(&f)!!;
active_monkey.on_true = line.tsplit("key ")[1].to_int()!!;
line = io::treadline(&f)!!;
active_monkey.on_false = line.tsplit("key ")[1].to_int()!!;
};
}
return monkeys[..max_monkey];
}
fn long monkey_business(Monkey[] monkeys)
{
long max;
long next_max;
foreach (&m : monkeys)
{
if (m.inspections > max)
{
next_max = max;
max = m.inspections;
continue;
}
if (m.inspections > next_max)
{
next_max = m.inspections;
}
}
return max * next_max;
}
fn void part1()
{
Monkey[] monkeys = load_monkeys(&&Monkey[10]{});
for (int round = 0; round < 20; round++)
{
foreach (i, &monkey : monkeys)
{
for (int item_id = 0; item_id < monkey.item_count; item_id++)
{
monkey.inspections++;
long level = monkey.items[item_id];
long other_op = monkey.other_op == -1 ? level : monkey.other_op;
level = monkey.op_is_mult ? level * other_op : level + other_op;
level /= 3;
int to_monkey_id = level % monkey.test ? monkey.on_false : monkey.on_true;
Monkey* to_monkey = &monkeys[to_monkey_id];
to_monkey.items[to_monkey.item_count++] = level;
}
monkey.item_count = 0;
}
}
io::printfn("Monkey business was: %d", monkey_business(monkeys));
}
fn void part2()
{
Monkey[] monkeys = load_monkeys(&&Monkey[10]{});
long divisor = 1;
// We are only interested of values MOD <common divisor>!
foreach (&monkey : monkeys) divisor *= monkey.test;
for (int round = 0; round < 10000; round++)
{
foreach (i, &monkey : monkeys)
{
for (int item_id = 0; item_id < monkey.item_count; item_id++)
{
monkey.inspections++;
long level = monkey.items[item_id];
long other_op = monkey.other_op == -1 ? level : monkey.other_op;
level = level % divisor;
level = monkey.op_is_mult ? level * other_op : level + other_op;
int to_monkey_id = level % monkey.test ? monkey.on_false : monkey.on_true;
Monkey* to_monkey = &monkeys[to_monkey_id];
to_monkey.items[to_monkey.item_count++] = level;
}
monkey.item_count = 0;
}
}
io::printfn("Monkey business was: %d", monkey_business(monkeys));
}
fn void main()
{
part1();
part2();
}