Skip to content

Commit db3c387

Browse files
committed
AoC 2022 D11: improve naming
The OOM issues from comptime seems to be caused just by looping 10k times. This seems to be a known issue: ziglang/zig#12493.
1 parent 1334f91 commit db3c387

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

advent-of-code/2022/day11/src/main.zig

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,66 @@ const std = @import("std");
33
const parsed_input = blk: {
44
@setEvalBranchQuota(10_000);
55
const input = @embedFile("input");
6-
const Items = std.BoundedArray(u64, 64);
6+
const ItemWorries = std.BoundedArray(u64, 64);
77
var monkeys = std.BoundedArray(struct {
8-
items: Items,
8+
item_worries: ItemWorries,
99
op: u8,
1010
operand: ?u8, // null == "old".
1111
div: u8, // assumed to always be a prime number.
1212
true_i: u8,
1313
false_i: u8,
1414
}, 16).init(0) catch unreachable;
15-
var max_worry: u64 = 1;
15+
var max_div: u64 = 1; // divisors are prime, so we only need to consider worry values less than this.
1616
var line_it = std.mem.tokenize(u8, input, std.cstr.line_sep);
1717
while (line_it.next()) |_| {
18-
var items = Items.init(0) catch unreachable;
19-
var items_it = std.mem.split(u8, line_it.next().?[" Starting items: ".len..], ", ");
20-
while (items_it.next()) |item_str| items.append(std.fmt.parseInt(u64, item_str, 10) catch unreachable) catch unreachable;
18+
var item_worries = ItemWorries.init(0) catch unreachable;
19+
var item_it = std.mem.split(u8, line_it.next().?[" Starting items: ".len..], ", ");
20+
while (item_it.next()) |str| item_worries.append(std.fmt.parseInt(u64, str, 10) catch unreachable) catch unreachable;
2121
const op_str = line_it.next().?[" Operation: new = old ".len..];
2222
const div = std.fmt.parseInt(u8, line_it.next().?[" Test: divisible by ".len..], 10) catch unreachable;
23+
max_div *= div;
2324
monkeys.append(.{
24-
.items = items,
25+
.item_worries = item_worries,
2526
.op = op_str[0],
2627
.operand = std.fmt.parseInt(u8, op_str[2..], 10) catch null,
2728
.div = div,
2829
.true_i = std.fmt.parseInt(u8, line_it.next().?[" If true: throw to monkey ".len..], 10) catch unreachable,
2930
.false_i = std.fmt.parseInt(u8, line_it.next().?[" If false: throw to monkey ".len..], 10) catch unreachable,
3031
}) catch unreachable;
31-
max_worry *= div;
3232
}
33-
break :blk .{ .monkeys = monkeys, .max_worry = max_worry };
33+
break :blk .{ .monkeys = monkeys, .max_div = max_div };
3434
};
3535

36-
fn computeAnswer(comptime p1: bool) usize {
36+
fn computeAnswer(comptime is_p1: bool) usize {
3737
var monkeys = parsed_input.monkeys;
38-
var inspect_counts = [_]usize{0} ** parsed_input.monkeys.len;
39-
var round: u64 = 0;
40-
while (round < if (p1) 20 else 10_000) : (round += 1) {
38+
var inspects = [_]usize{0} ** parsed_input.monkeys.len;
39+
var round: u32 = 0;
40+
while (round < if (is_p1) 20 else 10_000) : (round += 1) {
4141
for (monkeys.slice()) |*monkey, i| {
42-
for (monkey.items.slice()) |item| {
43-
const operand = monkey.operand orelse item;
44-
const pre_worry = if (monkey.op == '*') item * operand else item + operand;
45-
const worry = if (p1) pre_worry / 3 else pre_worry % parsed_input.max_worry;
42+
for (monkey.item_worries.slice()) |item_worry| {
43+
const operand = monkey.operand orelse item_worry;
44+
const op_worry = if (monkey.op == '*') item_worry * operand else item_worry + operand;
45+
const worry = if (is_p1) op_worry / 3 else op_worry % parsed_input.max_div;
4646
const throw_i = if (worry % monkey.div == 0) monkey.true_i else monkey.false_i;
47-
monkeys.slice()[throw_i].items.append(worry) catch unreachable;
47+
monkeys.slice()[throw_i].item_worries.append(worry) catch unreachable;
4848
}
49-
inspect_counts[i] += monkey.items.len;
50-
monkey.items.len = 0;
49+
inspects[i] += monkey.item_worries.len;
50+
monkey.item_worries.len = 0;
5151
}
5252
}
5353
var top2 = [_]usize{0} ** 2;
54-
for (inspect_counts) |count| {
54+
for (inspects) |count| {
5555
const min_i = std.mem.indexOfMin(usize, &top2);
5656
top2[min_i] = @max(top2[min_i], count);
5757
}
5858
return top2[0] * top2[1];
5959
}
60+
const p1 = blk: {
61+
@setEvalBranchQuota(100_000);
62+
break :blk computeAnswer(true);
63+
};
6064

6165
pub fn main() !void {
6266
const stdout = std.io.getStdOut().writer();
63-
const p1 = comptime blk: {
64-
@setEvalBranchQuota(100_000);
65-
break :blk computeAnswer(true);
66-
};
67-
const p2 = computeAnswer(false);
68-
try stdout.print("Day11 (P1 at comptime): P1: {}, P2: {}\n", .{ p1, p2 });
67+
try stdout.print("Day11 (P1 at comptime): P1: {}, P2: {}\n", .{ p1, computeAnswer(false) });
6968
}

0 commit comments

Comments
 (0)